Say I have a class:
class A():
def f(self):
self._v = 1
Tried:
m=Mocker()
A.f._v = m.mock()
...
but didn't work. Not sure how...
Did you mean Mock library?
from mock import Mock
real = ProductionClass()
real.method = Mock(return_value=3)
real.method(3, 4, 5, key='value')
edit:
You are trying to access A.f._v before mocking which is impossible.
Not sure what are you trying to do, but this will work
>>>A.f = Mock()
>>>a = A()
>>>a.f._v
<Mock name='mock._v' id='42076240'>
The class definition shows an instance variable to set it from outside this class, do something like this:
class A:
def f(self):
self._v = 1
a = A()
a._v = Mock()
If you actually wanted a real class variable, try this:
class A():
_v = None
def f(self):
self.__class__._v = 1
A._v = Mock()
I tried above solutions and it still does not solve my purpose which is exactly what is asked originally. The above approach would update the mocked attribute of my class to have a value of .
My requirement is to set the attribute value from the mocked value I provide in my unit test class.
I could finally resolved this with the help of following approach. Let me know if its not a correct way:
Actual Class:
class ActualClass(object):
name=''
def some_method(self):
name=get_name_from_external_source() #Say, returned name='ActualValue'
print name
Unit Test Class:
from mock import PropertyMock
import unittest
class TestActualClass(unittest.TestCase):
def test_some_method(self):
actual_class=ActualClass()
p=PropertyMock(return_value='Mocked_Name')
type(actual_class).name=p
actual_class.some_method()
When you run some_method in ActualClass through normal execution, the output:
ActualValue
When you run TestActualClass, the output:
Mocked_Name
This implies that class attributes are mocked with a mocked value using PropertyType and you can test the method with mocked value and without worrying about external source method call.
Related
I have a python method like
import external_object
from external_lib1 import ExternalClass1
from external_lib2 import Hook
class MyClass(self):
def my_method(self):
ExternalClass.get('arg1') #should be mocked and return a specific value with this arg1
ExternalClass.get('arg2') #should be mocked and return a specific value with this arg2
def get_hook(self):
return Hook() # return a mock object with mocked method on it
def my_method(self):
object_1 = external_object.instance_type_1('args') # those are two different object instanciate from the same lib.
object_2 = external_object.instance_type_2('args')
object_1.method_1('arg') # should return what I want when object_1 mocked
object_2.method_2 ('arg') # should return what I want when object_2 mocked
In my test I would like to realise what I put in comments.
I could manage to do it, but every time it gets really messy.
I use to call flexmock for some stuff (by example ExternalClass.get('arg1') would be mock with a flexmock(ExternalClass).should_return('arg').with_args('arg') # etc...) but I'm tired of using different test libs to mock.
I would like to use only the mock library but I struggle to find a consistent way of doing it.
I like to use python's unittest lib. Concretely the unittest.mock which is a great lib to customize side effects and return value in unit tested functions.
They can be used as follows:
class Some(object):
"""
You want to test this class
external_lib is an external component we cannot test
"""
def __init__(self, external_lib):
self.lib = external_lib
def create_index(self, unique_index):
"""
Create an index.
"""
try:
self.lib.create(index=unique_index) # mock this
return True
except MyException as e:
self.logger.error(e.__dict__, color="red")
return False
class MockLib():
pass
class TestSome(unittest.TestCase):
def setUp(self):
self.lib = MockLib()
self.some = Some(self.lib)
def test_create_index(self):
# This will test the method returns True if everything went fine
self.some.create_index = MagicMock(return_value={})
self.assertTrue(self.some.create_index("test-index"))
def test_create_index_fail(self):
# This will test the exception is handled and return False
self.some.create_index = MagicMock(side_effect=MyException("error create"))
self.assertFalse(self.some.create_index("test-index"))
Put the TestSome() class file somewhere like your-codebase-path/tests and run:
python -m unittest -v
I hope it's useful.
I have a Python module as follows:
# src/exec.py
class A:
def run(self, stuff):
b = B(stuff.x)
class B:
def __init__(self, x):
self.obj = self.create_some_obj()
I'm trying to test a part of class A independently, for which I need to replace the obj in B with a fake object. I'm doing this as follows:
# test/test_execs.py
import exec as ex
class FakeObjForB:
def __init__(self):
# some init
class TestClass:
#patch.object(ex.B, 'obj', FakeObjForB())
def test_with_fake_obj(self):
a = ex.A()
a.run()
# assert something about the state of a that depends on the b inside its run method
Running this test gives me the error: AttributeError: <class 'B'> does not have the attribute 'obj'. I tried replacing the line with the #patch decorator with #patch.object(ex.B, 'obj', FakeObjForB(), create=True). This, however, results in b.obj using the actual definition, and not FakeObjForB, which in turn leads to a false-failure in the assertion in test_with_fake_obj. Any clues about what I'm doing incorrectly here?
In your example you're patching the B class, that's the object passed as the first argument. That class doesn't declare obj attribute on the class level and so AttributeError is raised. When you provide create=True it won't complain as that argument allows the obj attribute to be dynamically created when needed/accessed. But, that won't ever happen as the very first "access" of that attribute is its actual creation - no dynamic mocking ever happened.
A solution is to actually patch the method whose returned value would be assigned to the obj attribute, like:
#patch.object(ex.B, 'create_some_obj', FakeObjForB())
I am trying to understand the mock/patch framework, but have a problem. Here are my simplified codes:
file_a.py
class A:
def f(self):
b = B()
b.g()
b.h()
file_b.py
class B:
def g(self):
return network_requests(...)
def h(self):
return "This is should not be mocked."
file_test.py
class SomeTests:
def setUp(self):
with patch('file_b.B', autospec=True) as mock:
mock.g.return_value = "Mocked value"
mock.side_effect = lambda : B()
self.a = A()
def test(self):
self.a.f()
Essentially I want to mock only B.g inside the test, but not B.h. I got some idea from https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking, but B.g is still not mocked.
Thank you!
In the example that you linked the key problem is
Unfortunately datetime.date is written in C
That is why you need to mock the module and wrap what you don't want to mock (You cannot patch C methods directly).
Is all other cases (patch python objects) you can use just :
with patch('file_b.B.g', autospec=True) as mock_g:
mock_g.return_value = "Mocked value"
Anyway take care that your patch will be active just in the with context, out of it you will find the original reference. To have a better control of the context it you can use also decorators, start() and stop().
I strongly encourage you read carefully patch and where to patch.
I have a situation where I'm trying to modify the arguments passed to a decorator on one of my class methods. The code looks something like this:
class MyClass(object):
#tryagain(retries=3)
def mymethod(self, arg):
... do stuff ...
My problem is I'd like to alter the "retries" variable to something less than 3 when running my unit tests, but keep it at "3" for the production code. Unfortunately, it doesn't look like I can do something like this:
#tryagain(retries=self.retries)
def mymethod(self, arg):
... do stuff ...
or
#tryagain(retries=MyClass.retries)
def mymethod(self, arg):
... do stuff ...
because the class isn't defined at the point the arguments are passed to the decorator (as near as I can tell).
I also tried to add the variable within the module like so:
retries = 1
def MyClass(object):
#tryagain(retries=retries)
def mymethod(self, arg):
... do stuff ...
but then I can't seem to modify the value of "retries" from within my unit tests. Is there another way to accomplish what I'm trying to do?
I assume you try to reduce the number of retrials to increase test speed.
If so, modifying the number of retries variable doesn't seem to be the best approach. Instead, you could unit test the function mymethod without decorator first, and then create a mock function of mymethod. Let's call it mock_mymethod, decorate it with #tryagain and test if the logic of `tryagain actually works.
Check the mock module to see how to create a mock instance, this article about mock is also worth reading.
You could use an environment variable, set from your calling code (it might be good to put a default in here
import os
# ...
class MyClass(object):
#tryagain(retries=int(os.environ['project_num_retries']))
def mymethod(self, arg):
print("mymethod")
Or use a "globals"-type module, for example: project_settings.py containing:
num_retries = 3
Then
import project_settings
class MyClass(object):
#tryagain(retries=project_settings.num_retries)
def mymethod(self, arg):
print("mymethod")
But I'm not sure decorating your code with test information is how you really should go about it -- what about:
class MyClass(object):
def mymethod(self, arg):
print("mymethod")
Then in something like unittests.py:
DEV_TESTS = True # Change to False for production
num_retries = 3 if not DEV_TESTS else 1
import <your class>
class UnitTests():
def __init__(self):
self.c = <your_class>.MyClass()
#tryagain(retries=num_retries)
def test_mymethod(self):
self.c.mymethod("Foo")
t = UnitTests()
t.test_mymethod()
If you were so inclined, this unittests.py could be used with something like python's unittest package with:
DEV_TESTS = True # Change to False for production
num_retries = 3 if not DEV_TESTS else 1
import unittest
import <your class>
class UnitTests(unittest.TestCase):
def setUp(self):
self.c = <your class>.MyClass()
#tryagain(retries=num_retries)
def test_mymethod(self):
self.c.mymethod("Foo")
Note, I used the following simple example of a #tryagain decorator, yours may be more complicated and require some tuning of the examples:
def tryagain(retries):
def wrap(f):
def wrapped_f(*args,**kwargs):
for _ in xrange(retries):
f(*args,**kwargs)
return wrapped_f
return wrap
Here's an example class that simplifies what I have:
class.py
class MyClass(object):
#staticmethod
def getDictionary():
#some calculations, returns a dictionary
def checkConfiguration(self):
#some code
self.getDictionary()
#some other code
return value
And now I am making a unit test for checkConfiguration:
classTest.py
import class
import unittest
class TestMyClass(unittest.TestCase):
def setUp(self):
self.classTest = class.MyClass()
def test_CheckConfiguration(self):
#What to put here?
The original CheckConfiguration calls getDictionary. Is there a way to tell the test_CheckConfiguration(self) that if getDictionary is called, it should automatically return a dictionary I can type in?
Something like:
def test_CheckConfiguration(self):
if method getDictionary is called:
return {'a':123, 'b':22}
checkValue = self.classTest.checkConfiguration()
I know this is possible in Java, though I don't have enough experience in that nor this.
Thank you.
I think you need a mocking framework. I suggest PyMock.
Here's how you could use it:
classTest.py
import class
import pymock
import unittest
class TestMyClass(pymock.PyMockTestCase):
def setUp(self):
self.classTest = class.MyClass()
def test_CheckConfiguration(self):
self.override(self.classTest, 'getDictionary')
pymock.expectAndReturn(self.classTest.getDictionary(), {'a':123, 'b':22})
self.replay()
checkValue = self.classTest.checkConfiguration()
self.verify()
https://groups.google.com/forum/?fromgroups#!topic/comp.lang.python/WBhc1xAc8Hw suggests subclassing your class under test and overriding __getattribute__ to record each call in whatever manner you need. Not sure what else would work...