py.test: how to get the current test’s name from the setup method?
You can also do this using the Request Fixture like this: def test_name1(request): testname = request.node.name assert testname == ‘test_name1’
You can also do this using the Request Fixture like this: def test_name1(request): testname = request.node.name assert testname == ‘test_name1’
You need to monkeypatch datetime.now function. In example below, I’m creating fixture which I can re-use later in other tests: import datetime import pytest FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 5, 55) @pytest.fixture def patch_datetime_now(monkeypatch): class mydatetime: @classmethod def now(cls): return FAKE_TIME monkeypatch.setattr(datetime, ‘datetime’, mydatetime) def test_patch_datetime(patch_datetime_now): assert datetime.datetime.now() == FAKE_TIME
coverage (used by pytest-cov) needs the tests folder to contain an __init__.py before it will collect any data. I added __init__.py to the tests folder and then coverage collected the data as expected. Refer to http://thomas-cokelaer.info/blog/2017/01/pytest-cov-collects-no-data-on-travis/
I found a workaround: Installed python3-pip using aptitude, which created /usr/bin/pip-3.2. Next pip-3.2 install pytest which re-installed pytest, but under a python3.2 path. Then I was able to use python3 -m pytest somedir/sometest.py. Not as convenient as running py.test directly, but workable.
When using pytest fixture with mock.patch, test parameter order is crucial. If you place a fixture parameter before a mocked one: from unittest import mock @mock.patch(‘my.module.my.class’) def test_my_code(my_fixture, mocked_class): then the mock object will be in my_fixture and mocked_class will be search as a fixture: fixture ‘mocked_class’ not found But, if you reverse the order, … Read more
What about the __init__.py in the apple directory… correct? Empty or what should be inside? Yes, correct. Most frequently empty. If you put foo = 42 in it you can later do from apple import foo while you’ll need to do from apple.apple import foo if you put it in apple.py. While it might seem … Read more
When you run py.test, you can pass -rsx to report skipped tests. From py.test –help: -r chars show extra test summary info as specified by chars (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed. Also see this part of the documentation about skipping: http://doc.pytest.org/en/latest/skipping.html
using python -m pytest will work for you. Or if you using virtual environment and installed pytest on virtualenv you should then run py.test alongside your virtual environment. Check this website can be useful:http://pythontesting.net/framework/pytest/pytest-introduction/
Yes, you can mark tests with the pytest.mark decorator. Example: def unit_test_1(): # assert here def unit_test_2(): # assert here @pytest.mark.integtest def integration_test(): # assert here Now, from the command line, you can run pytest -m “not integtest” for only the unit tests, pytest -m integtest for only the integration test and plain pytest for … Read more
While Ronny’s answer works it forces you to change application code. In general you should not do this for the sake of testing. Instead you can explicitly patch the object in the second package. This is mentioned in the docs for the unittest module. monkeypatch.setattr(‘another_package.bar’, lambda: print(‘patched’))