Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Nested spec_set in mock

pt12lol:

I have two following files:

testcase_module.py


import boto3


ec2 = boto3.resource('ec2')


def f():
return ec2.instances.all()

testcase_test.py


import testcase_module
import unittest.mock


class MainTest(unittest.TestCase):
@unittest.mock.patch('testcase_module.ec2', spec_set=['instances'])
def test_f(self, ec2_mock):
ec2_mock.instances.spec_set = ['all']
testcase_module.f()


if __name__ == '__main__':
unittest.main()

I added spec_test parameter to the patch because I would like to assert if any other function than instances.all() has been called, but changing string 'all' to 'allx' doesn't make test fail while changing 'instances' to 'instancesx' does. I tried the following changes (git diff testcase_test.py and python testcase_test.py results below):

Attempt 1:


diff --git a/testcase_test.py b/testcase_test.py
index d6d6e59..ae274c8 100644
--- a/testcase_test.py
+++ b/testcase_test.py
@@ -3,9 +3,8 @@ import unittest.mock


class MainTest(unittest.TestCase):
- @unittest.mock.patch('testcase_module.ec2', spec_set=['instances'])
- def test_f(self, ec2_mock):
- ec2_mock.instances.spec_set = ['all']
+ @unittest.mock.patch('testcase_module.ec2', spec_set=['instances.all'])
+ def test_f(self, _):
testcase_module.f()

Produces:


E
======================================================================
ERROR: test_f (__main__.MainTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/mock.py", line 1157, in patched
return func(*args, **keywargs)
File "testcase_test.py", line 8, in test_f
testcase_module.f()
File "/path/to/project/testcase_module.py", line 8, in f
return ec2.instances.all()
File "/usr/lib/python3.5/unittest/mock.py", line 578, in __getattr__
raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'instances'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Attempt 2:


diff --git a/testcase_test.py b/testcase_test.py
index d6d6e59..d93abd1 100644
--- a/testcase_test.py
+++ b/testcase_test.py
@@ -3,9 +3,8 @@ import unittest.mock


class MainTest(unittest.TestCase):
- @unittest.mock.patch('testcase_module.ec2', spec_set=['instances'])
- def test_f(self, ec2_mock):
- ec2_mock.instances.spec_set = ['all']
+ @unittest.mock.patch('testcase_module.ec2.instances', spec_set=['all'])
+ def test_f(self):
testcase_module.f()

Produces:


E
======================================================================
ERROR: test_f (__main__.MainTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/mock.py", line 1149, in patched
arg = patching.__enter__()
File "/usr/lib/python3.5/unittest/mock.py", line 1312, in __enter__
setattr(self.target, self.attribute, new_attr)
AttributeError: can't set attribute

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.5/unittest/mock.py", line 1170, in patched
patching.__exit__(*exc_info)
File "/usr/lib/python3.5/unittest/mock.py", line 1334, in __exit__
delattr(self.target, self.attribute)
AttributeError: can't delete attribute

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

How can I make it failing when other method than instances.all has been called?



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Nested spec_set in mock

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×