How do I configure PyCharm to run py.test tests?
Please go to File| Settings | Tools | Python Integrated Tools and change the default test runner to py.test. Then you’ll get the py.test option to create tests instead of the unittest one.
Please go to File| Settings | Tools | Python Integrated Tools and change the default test runner to py.test. Then you’ll get the py.test option to create tests instead of the unittest one.
I’m not sure this will solve your problem, but you can pass –durations=N to print the slowest N tests after the test suite finishes. Use –durations=0 to print all.
Since version 3.3, pytest supports live logging, meaning that all the log records emitted in tests will be printed to the terminal immediately. The feature is documented under Live Logs section. Live logging is disabled by default; to enable it, set log_cli = 1 in the pyproject.toml1 or pytest.ini2 config. Live logging supports emitting to … Read more
According to Fixture finalization / executing teardown code, the current best practice for setup and teardown is to use yield instead of return: import pytest @pytest.fixture() def resource(): print(“setup”) yield “resource” print(“teardown”) class TestResource: def test_that_depends_on_resource(self, resource): print(“testing {}”.format(resource)) Running it results in $ py.test –capture=no pytest_yield.py === test session starts === platform darwin — … Read more
This is actually supported natively in py.test via indirect parametrization. In your case, you would have: @pytest.fixture def tester(request): “””Create tester object””” return MyTester(request.param) class TestIt: @pytest.mark.parametrize(‘tester’, [[‘var1’, ‘var2’]], indirect=True) def test_tc1(self, tester): tester.dothis() assert 1
Found the answer: DO NOT put a __init__.py file in a folder containing TESTS if you plan on using pytest. I had one such file, deleting it solved the problem. This was actually buried in the comments to the second answer of PATH issue with pytest ‘ImportError: No module named YadaYadaYada’ so I did not … Read more
I noticed that this question specifically asked about py.test. py.test 3.0 includes an approx() function (well, really class) that is very useful for this purpose. import pytest assert 2.2 == pytest.approx(2.3) # fails, default is ± 2.3e-06 assert 2.2 == pytest.approx(2.3, 0.1) # passes # also works the other way, in case you were worried: … Read more
I’m not sure why py.test does not add the current directory in the PYTHONPATH itself, but here’s a workaround (to be executed from the root of your repository): python -m pytest tests/ It works because Python adds the current directory in the PYTHONPATH for you.
By default, py.test captures the result of standard out so that it can control how it prints it out. If it didn’t do this, it would spew out a lot of text without the context of what test printed that text. However, if a test fails, it will include a section in the resulting report … Read more
Is this the correct use of conftest.py? Yes it is. Fixtures are a potential and common use of conftest.py. The fixtures that you will define will be shared among all tests in your test suite. However, defining fixtures in the root conftest.py might be useless and it would slow down testing if such fixtures are … Read more