PyTest - Apply mock to all tests - python-3.x

I would like to apply a mock/patch that will apply to all tests, how can I do this?
I had tried putting it in a fixture, and using the fixture everywhere, but, the reapplication of the mock/patch on each test was leading to inconsistent id(my_mock) values.

You can make a fixture applied only once for all the test suite execution by scoping the fixture to 'session', and using it in all your tests:
import pytest
from unittest import mock
#pytest.fixture(scope='session', autouse=True)
def my_thing_mock():
with mock.patch.object(TheThingYouWantToMock, 'some_attribute') as _fixture:
yield _fixture

Related

Monkeypatch setenv value persist across test cases in python unittest

I have to set different environment variables for my test cases.
My assumption was once the test case is completed, monkeypatch will remove the env variables from os.environ. But it is not. How to set and revert the environment variables for each test?
Here is my simplified test case code with monkeypatch lib.
import os
import unittest
import time
from _pytest.monkeypatch import MonkeyPatch
class Test_Monkey_Patch_Env(unittest.TestCase):
def setUp(self):
print("Setup")
def test_1(self):
monkeypatch = MonkeyPatch()
monkeypatch.setenv("TESTVAR1", "This env value is persistent")
def test_2(self):
# I was expected the env TESTVAR1 set in test_1 using monkeypatch
# should not persist across test cases. But it is.
print(os.environ["TESTVAR1"])
def tearDown(self):
print("tearDown")
if __name__ == '__main__':
unittest.main()
Output:
Setup
tearDown
.Setup
This env value is persistent
tearDown
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
This expands a bit on the (correct) answer given by #MaNKuR.
The reason why it does not work as expected is that in pytest, it is not designed to be used this way - instead the monkeypatch fixture is used, which does the cleanup on leaving the test scope. To do the cleanup in unittest, you can do that cleanup in tearDown, as shown in the answer - though I would use the less specific undo for this:
from _pytest.monkeypatch import MonkeyPatch
class Test_Monkey_Patch_Env(unittest.TestCase):
def setUp(self):
self.monkeypatch = MonkeyPatch()
def tearDown(self):
self.monkeypatch.undo()
This reverts any changes made by using the MonkeyPatch instance, not just the specific setenv call shown above, and is therefore more secure.
Additionally there is the possibility to use MonkeyPatch as a context manager. This comes in handy if you want to use it just in one or a couple of tests. In this case you can write:
...
def test_1(self):
with MonkeyPatch() as mp:
mp.setenv("TESTVAR1", "This env value is not persistent")
do_something()
The cleanup in this case is done on exiting the context manager.
To remove env variable set by monkeypatch module, seems you have to call delenv method.
You can call this delenv after you are done with setting & testing env variable using setenv method but I think tearDown would be the right place to put that delenv call.
def tearDown(self):
print("tearDown")
monkeypatch.delenv('TESTVAR1', raising=False)
I have not tested the code however should give you fair amount of idea whats needs to be done.
EDIT: Improved code to use setup * tearDown more efficiently. test_2 should raise OS Error since env variable has been deleted
import os
import unittest
import time
from _pytest.monkeypatch import MonkeyPatch
class Test_Monkey_Patch_Env(unittest.TestCase):
def setUp(self):
self.monkeypatch = MonkeyPatch()
print("Setup")
def test_1(self):
self.monkeypatch.setenv("TESTVAR1", "This env value is persistent")
def test_2(self):
# I was expected the env TESTVAR1 set in test_1 using monkeypatch
# should not persist across test cases. But it is.
print(os.environ["TESTVAR1"]) # Should raise OS error
def tearDown(self):
print("tearDown")
self.monkeypatch.delenv("TESTVAR1", raising=False)
if __name__ == '__main__':
unittest.main()
Cheers!

Testing Flask microservices using unittest module

I have developed a very basic microservice using Flask framework.
A method in the application looks like this
#app.route('/add, methods=['POST'])
def add_info():
final = []
try:
info_obj.append(json.loads(request.data))
...
return jsonify(final)
Now I am attempting to write Unittest for this method and other method in this microservice. I am using import unittest to write my test.
Now here I am confused is how can I write tests to test the functionality of these http functions, which don't take regular argument of return regular results but rather fetch arguments from request data and return json based on that.
Is my approach correct? and if yes how can I test Microservices-like functionality using unittest module?
If you absolutely want unittesting, follow Patrick's guide here. But I suggest using PyTest. It's a breeze to get started. First you need a conftest.py. Then add your testfiles named test_... .py . The where your conftest is do $ pytest
Patrick has yet another PyTest + Flask guide here. You can view a demo of a conftest in a project here on how to set up db etc and a test file here

Add property to junit xml pytest

I am trying to create a junit xml output file in pytest with custom attributes.
I searched for answers and found about xml_record_attribute and record_attribute.
def test(record_attribute):
record_attribute('index', '15')
...
At first I thought it would fit to my problem but then I realized it needs to be specified in each test.
I tried to do it with pytest_runtest_call hook so it would add attributes in each test run without the need to explicitly add the attributes in each test. But then it turned out that you can't use fixtures in hook (which makes sense).
Any idea how can I add an attribute to the junit xml output file without duplicating the code?
EDIT:
I have another idea of having a decorator which does that.
def xml_decorator(test):
def runner(xml_record_attribute):
xml_record_attribute('index', '15')
test()
reutrn runner
I am trying hook it with pytest_collection_modifyitems and decorate each test but it doesn't work.
def pytest_collection_modifyitems(session, config, items):
for item in items:
item.obj = xml_decorator(item.obj)
You could define a fixture that is automatically supplied to each test case:
import pytest
#pytest.fixture(autouse=True)
def record_index(record_xml_attribute):
record_xml_attribute('index', '15')
Note record_xml_attribute is not supported for xunit2, the default junit_family for pytest v6. To use record_xml_attribute with pytest v6, in pytest.ini set
[pytest]
junit_family = xunit1

Meaning of yield within fixture in pytest

I read the documentation at docs.pytest.org
I'm not sure about the meaning of the statement: yield smtp_connection
Can please someone explain what yield does, and if it's mandatory?
First of all it's not mandatory!!!
Yield execute test body, for example, you can set up your test with pre-condition and post condition. For this thing we can use conftest.py:
import pytest
#pytest.fixture
def set_up_pre_and_post_conditions():
print("Pre condition")
yield # this will be executed our test
print("Post condition")
Our test, for example store in test.py:
def test(set_up_pre_and_post_conditions):
print("Body of test")
So, let's launch it: pytest test.py -v -s
Output:
test.py::test Pre condition
Body of test
PASSEDPost condition
It's not full functionality of yield, just example, I hope it will be helpful.

Mocking print but allow using it in tests

Print can be mocked in the following way:
import unittest
import builtin
class TestSomething(unittest.TestCase):
#mock.patch('builtins.print')
def test_method(self, print_):
some_other_module.print_something()
However this means that in the python debug console (pydev debugger) and in the unit test method itself print cannot be used. This is rather inconvenient.
Is there a way to only mock the print method in some_other_module instead of in the testing module as well?
A way to sidestep this is to swap the use of print in the test module with some other function which just calls print, which I can do if there turns out to be no better solution.
#michele's "final solution" has an even cleaner alternative which works in my case:
from unittest import TestCase
from unittest.mock import patch
import module_under_test
class MyTestCase(TestCase):
#patch('module_under_test.print', create=True)
def test_something(self, print_):
module_under_test.print_something()
print_.assert_called_with("print something")
Yes you can! ... But just because you are using Python 3. In Python 3 print is a function and you can rewrite it without change the name. To understand the final solution I'll describe it step by step to have a final flexible and non intrusive solution.
Instrument Module
The trick is add at the top of your module that you will test a line like:
print = print
And now you can patch just print of your module. I wrote a test case where the mock_print_module.py is:
print = print
def print_something():
print("print something")
And the test module (I'm using autospec=True just to avoid errors like mock_print.asser_called_with):
from unittest import TestCase
from unittest.mock import patch
import mock_print_module
class MyTestCase(TestCase):
#patch("mock_print_module.print",autospec=True)
def test_something(self,mock_print):
mock_print_module.print_something()
mock_print.assert_called_with("print something")
I don't want to change my module but just patch print without lose functionalities
You can use patch on "builtins.print" without lose print functionality just by use side_effect patch's attribute:
#patch("builtins.print",autospec=True,side_effect=print)
def test_somethingelse(self,mock_print):
mock_print_module.print_something()
mock_print.assert_called_with("print something")
Now you can trace your prints call without lose the logging and pydev debugger. The drawback of that approach is that you must fight against lot of noise to check your interested the print calls. Moreover you cannot chose what modules will be patched and what not.
Both modes don't work together
You cannot use both way together because if you use print=print in your module you save builtins.print in print variable at the load module time. Now when you patch builtins.print the module still use the original saved one.
If you would have a chance to use both you must wrap original print and not just record it. A way to implement it is use following instead of print=print:
import builtins
print = lambda *args,**kwargs:builtins.print(*args,**kwargs)
The Final Solution
Do we really need to modify the original module to have a chance to patch all print calls in it? No, we can do it without change the module to test anyway. The only thing that we need is injecting a local print function in the module to override the builtins's one: we can do it in the test module instead of the the module to test. My example will become:
from unittest import TestCase
from unittest.mock import patch
import mock_print_module
import builtins
mock_print_module.print = lambda *args,**kwargs:builtins.print(*args,**kwargs)
class MyTestCase(TestCase):
#patch("mock_print_module.print",autospec=True)
def test_something(self,mock_print):
mock_print_module.print_something()
mock_print.assert_called_with("print something")
#patch("builtins.print",autospec=True,side_effect=print)
def test_somethingelse(self,mock_print):
mock_print_module.print_something()
mock_print.assert_called_with("print something")
and mock_print_module.py can be the clean original version with just:
def print_something():
print("print something")

Resources