I want to mock all test methods of a TestCase.
My first try to use TestCase.setUp() did not work, since setUp() finishes before the test methods gets executed.
I can't mock the real test method with this inside setUp():
with mock.patch(...):
do_something()
I guess I am missing something.
How to use mock.patch() for all methods of a test case?
with mock.patch() is a context manager, the patch is unapplied when the context ends, and the context ends at the end of the block of code.
That means that the patches are unapplied again when setUp() ends.
Your options are to either use #mock.patch() as a class decorator or to use the start and stop methods on the patchers.
Using #mock.patch() as a class decorator has the same effect as applying it as a decorator to each and every test method:
#mock.patch('module.ClassName')
class TestFoo(unittest.TestCase):
def setUp(self):
# ...
def test_one(self, class_mock):
# ...
def test_two(self, class_mock):
# ...
Here both test_one and test_two are passed in a mock object because the #mock.patch() class decorator has found all test methods and decorated them.
Using the start and stop methods lets you apply and unapply patches in the setUp and tearDown methods:
class TestFoo(unittest.TestCase):
def setUp(self):
self.patch1 = mock.patch(...)
self.patch1.start()
def tearDown(self):
self.patch1.stop()
Here patch1 is started on set-up, and stopped again when the test is torn down. This acts just like the context manager, but instead hooks into the test boundaries.
Instead of using a tearDown, you can also register the patch.stop() as a cleanup function with TestCase.addCleanup():
class TestFoo(unittest.TestCase):
def setUp(self):
patch1 = mock.patch(...)
patch1.start()
self.addCleanup(patch1.stop)
You can use mock.patch.start, mock.patch.stop. (See patch methods: start and stop).
For example:
class MyTest(TestCase):
def setUp(self):
self.patcher = mock.patch('...')
self.MockClass = self.patcher.start()
def tearDown(self):
self.patcher.stop()
def test_something(self):
....
The most general solution, which works for all context managers is:
import unittest
class TCase(unittest.TestCase):
def setUp(self):
self.cm = mock.path(...)
self.cm.__enter__()
def test1(self):
...
def tearDown(self):
self.cm.__exit__(None, None, None)
Related
As can be seen in the following example, I've defined a BaseClass for all tests, each test case class inherits the base class.
Both classes needs to perform a one time initialization, when test_vehicles.py is executed, I need to make sure that setUpClass of the base class is invoked as well, not sure how to achieve that where #classmethod is in play.
# base.py
import unittest
class BaseClass(unittest.TestCase):
#classmethod
def setUpClass(cls):
# initialize stuff
cls.app = app
# test_vehicles.py
class VehiclesTestCase(BaseClass):
#classmethod
def setUpClass(cls):
# initialize stuff
cls.vehicle_id = '123'
def test_get_vehicle(self):
resp = self.app.get(self.vehicle_id)
self.assertEqual(resp, True)
if __name__ == '__main__':
unittest.main()
Similar question Using super with a class method. More information yoou can get also from https://docs.python.org/3/library/functions.html#super.
Solution: use super function and bound to the class
# test_vehicles.py
class VehiclesTestCase(BaseClass):
#classmethod
def setUpClass(cls):
super(VehiclesTestCase, cls).setUpClass()
cls.vehicle_id = '123'
if __name__ == '__main__':
unittest.main()
You can use the super method in the inherited classes' setUpClass to access the setUpClass of BaseClass:
super().setUpClass()
If you don't want to call super in each child class, just create an abstract method in BaseClass and call it in setUpClass of BaseClass. VehiclesTestCase now has to implement this abstract method:
class BaseClass(unittest.TestCase):
#classmethod
def setUpClass(cls):
# initialize stuff
cls.app = app
#classmethod
def _setUpChild(cls):
raise NotImplementedError
class VehiclesTestCase(BaseClass):
#classmethod
def _setUpChild(cls):
# initialize stuff
cls.vehicle_id = '123'
def test_get_vehicle(self):
resp = self.app.get(self.vehicle_id)
self.assertEqual(resp, True)
I would also recommend that BaseClass is not a TestCase if it cannot run by itself. It would always show up in your test report although it has no tests. You can instead use multi-inheritance:
class BaseClass:
# Stuff
class VehiclesTestCase(BaseClass, unittest.TestCase):
# Stuff
The order of inheritance is important. Method lookup is done from left to right. This means that BaseClass.setUpClass overrides the setUpClass of TestCase.
The unittest docs state:
patch() acts as a function decorator, class decorator or a context
manager
I found the following way to also allow patching via a method call. You can then patch something by calling this method. For example, this is helpful for patching something in a setup method:
class MyTestCase(TestCase):
def patch(self, target, new):
p = patch(target, new)
p.start()
self.addCleanup(p.stop)
# ...
def setUp(self):
self.a = Mock()
self.patch("whatever.I.want.to.patch", self.a)
def test(self):
...
self.a.assert_called_once()
Is there a cleaner way of doing this? What is the reasoning for not providing this functionality out of the box?
I have a test class with few test methods and I want to patch some app classes and methods from the test methods.
In pytest docs I found an example of how to use monkeypatch module for tests. It that example all tests are just functions, not testclass methods.
But I have a class with test methods:
class MyTest(TestCase):
def setUp():
pass
def test_classmethod(self, monkeypatch):
# here I want to use monkeypatch.setattr()
pass
And just passing monkeypatch as method param is obviously doesn't work. So looks like py.test magic doesn't work this way.
So the question is simple and maybe stupid: how can I use monkeypatch.setattr() for pytest inside from the test class method?
It can't work in this form
While pytest supports receiving fixtures via test function arguments
for non-unittest test methods, unittest.TestCase methods cannot
directly receive fixture function arguments as implementing that is
likely to inflict on the ability to run general unittest.TestCase test
suites.
You might create monkeypatch directly
from _pytest.monkeypatch import MonkeyPatch
class MyTest(TestCase):
def setUp():
self.monkeypatch = MonkeyPatch()
def test_classmethod(self):
self.monkeypatch.setattr ...
...
or create own fixture, which will add monkeypatch to your class, and use #pytest.mark.usefixtures
#pytest.fixture(scope="class")
def monkeypatch_for_class(request):
request.cls.monkeypatch = MonkeyPatch()
#pytest.mark.usefixtures("monkeypatch_for_class")
class MyTest(TestCase):
def setUp():
pass
def test_classmethod(self):
self.monkeypatch.setattr ...
...
I had exactly the same problem.
This works perfectly
import unittest
import pandas as pd
from _pytest.monkeypatch import MonkeyPatch
from src.geipan_data import loadLongitudeLatitudeDateTestimony
class TestGeipanData(unittest.TestCase):
def setUp(self):
self.monkeypatch = MonkeyPatch()
def test_loadLongitudeLatitudeDateTestimony(self):
def read_csv(*args, **kwargs):
return pd.DataFrame({
'obs_date_heure': ['2010-05-21', '1926-05-21'],
'obs_1_lon': [45.123, 78.4564],
'obs_1_lat': [32.123, 98.4564],
})
self.monkeypatch.setattr(pd, 'read_csv', read_csv)
df = loadLongitudeLatitudeDateTestimony()
self.assertListEqual(
df.columns.values.tolist(),
['obs_date_heure', 'obs_1_lon', 'obs_1_lat']
)
In this example I do mock the pd.read_csv method with monkey patch and I uses asserListEqual that extends from unittest.TestCase
Is there a way to somehow use pytest fixtures from conftest.py in a test class's setup?
I need to initialize an object when the session starts and use it in some test classes' setup.
Something like this:
# conftest.py:
import pytest
#pytest.fixture(scope="session", autouse=True)
def myfixture(request):
return "myfixture"
# test_aaa.py
class TestAAA(object):
def setup(self, method, myfixture):
print("setup myfixture: {}".format(myfixture))
...
I don't think you can do it directly. However, you can decorate the whole class with pytest.mark.usefixtures, if that helps:
#pytest.mark.usefixtures(['myfixture'])
class TestAAA(object):
...
IIRC, setup_method will be called before any of the automatically applied fixtures.
You can also utilize autouse for class-level fixtures like so:
class TestAAA(object):
#pytest.fixture(autouse=True)
def init_aaa(self, myfixture):
...
I used this kind of a setup for a test class with pytest<=3.7.0 (it stopped working with pytest 3.7.1 release):
# conftest.py:
import pytest
# autouse=True does not work for fixtures that return value
# request param for fixture function is only required if the fixture uses it:
# e.g. for a teardown or parametrization. Otherwise don't use it.
#pytest.fixture(scope="session")
def myfixture():
return "myfixture"
# test_aaa.py
import pytest
class TestAAA(object):
#classmethod
#pytest.fixture(scope="class", autouse=True)
def setup(self, myfixture):
self.myfixture = myfixture
def test_function1(self):
# now you can use myfixture without passing it as param to each test function
assert self.myfixture == "myfixture"
For the example provided above, the object returned from the fixture can be set as an attribute of the test class. Any methods of this class will have access.
# test_fixture.py
import pytest
class TestAAA():
#pytest.fixture(scope="class", autouse=True)
def setup(self, myfixture):
TestAAA.myfixture = myfixture
def test_function1(self):
assert self.myfixture == "myfixture"
or if you inherit from unittest.Testcase you can do the following
# test_fixture.py
import pytest
from unittest import TestCase
class TestAAA(TestCase):
#pytest.fixture(scope="class", autouse=True)
def setup(self, myfixture):
self.myfixture = myfixture
def test_function1(self):
self.assertEqual(self.myfixture, "myfixture")
I am writing an Unit-Test for a Django class-based view.
class ExampleView(ListView):
def get_context_data(self, **kwargs):
context = super(EampleView, self).get_context_data(**kwargs)
## do something else
def get_queryset(self, **kwargs):
return self.get_data()
def get_data(self):
call_external_API()
## do something else
The key issue is that call_external_API() in get_data().
When I am writing Unit-test, I don't really want to call external API to get data. First, that will cost my money; second, I can easily test that API in another test file.
I also can easily test this get_data() method by having an unit-test only for it and mock the output of call_external_API().
However, when I test this whole class-based view, I simply will do
self.client.get('/example/url/')
and check the status code and context data to verify it.
In this case, how do I mock this call_external_API() when I am testing the whole class-based view?
What your are looking for is patch from unittest.mock. You can patch call_external_api() by a MagicMock() object.
Maybe you want to patch call_external_api() for all your tests in class. patch give to you essentialy two way to do it
decorate the test class
use start() and stop() in setUp() and tearDown() respectively
Decorate a class by patch decorator is like decorate all test methods (see documentation for details) and the implementation will be very neat. Follow example assume that your view is in my_view module.
#patch("my_view.call_external_api", autospec=True)
class MyTest(unittest.TestCase):
def setUp(self):
self.client = Client()
def test_get_data(self, mock_call_external_api):
self.client.get('/example/url/')
self.assertTrue(mock_call_external_api.called)
More sophisticate examples can be build and you can check how you call mock_call_external_api and set return value or side effects for your API.
I don't give any example about start and stop way to do it (I don't really like it) but I would like to spend some time on two details:
I assumed that in your my_view module you define call_external_api or you import it by from my_API_module import call_external_api otherwise you should pay some attention on Where to patch
I used autospec=True: IMHO it should be used in every patch call and documentation explain why very well
You can mock the call_external_api() method when testing the classed based view with something like this:
import modulea
import unittest
from mock import Mock
class ExampleTestCase(unittest.TestCase):
def setUp(self):
self.call_external_api = modulea.call_external_api
def tearDown(self):
modulea.call_external_api = self.call_external_api
def get_data(self):
modulea.call_external_api = Mock(return_value="foobar")
modulea.call_external_api()
## do something else