I'm having a pytest run that invokes N tests. I want all tests to output into the same log, and I want to clear the log before pytest is triggered, so different runs would not contiminate each other. So far, I tried adding my clear_log() to some fixtures or hooks, but none has achieved what In wanted - or they were called each time (pytest_runtest_makereport for example), or they were not called at all (some function that was decorated with pytest.fixture()
I also tried having a global flag that would indicate if this is the first run, but it didn't work either...
Where can I put the function so it would be invoked only once?
Following decorator allows you to execute your fixture only once (even if there are multiple number of tests are running):
#pytest.fixture(scope="session")
More details here
Related
There are several tests in the tests folder: test_first.py , test_second.py .
Each test checks the operation of the site by creating and modifying projects. I need that, regardless of how I run the tests tests/test_first.py or tests/ upon completion of all tests, the delete_prj function is executed, which deletes the created project after the tests have run. I'm not asking for the task execution code, the answer to the question is enough
Use a session scoped fixture. Something like this:
import pytest
#pytest.fixture(autouse=True, scope="session")
def do_stuff_after_everything_is_done():
yield
delete_prj()
(untested)
I am using pytest in a way in which there are two execution paths , one to configure the test environment (create some buildable folders, such as a c application that needs to be cross compiled and executed later in a distant target), and the second path that will execute the test.
I am having this separation because we need to separate the build and the execution processes so that they can be run separately by different machines in a CI environment.
so in the Configuration phase (controlled using a pytest cli argument), I am simply skipping the tests in a fixture that checks if this is the configuration stage or not, using:
pytest.skip("configuration stage")
What I want to do is to make the test appear as PASSED during the configuration stage, from the fixture context (without going to the test), putting an assert will not suffice as the test function will be executed anyway, and I don't want to pollute all the tests with a check like this:
def test_function(config_fixture):
if config_fixture:
assert True
else:
#run the test
I have a test suite that used to be executed with pytest and I used the method before_all_tests(request) in each test file to initialize the db mockups for those tests.
I wanted to use pytest-xdist to run them parallelly, but before_all_tests(request) is not being executed if I run pytest -n X (parallelly).
Now I don't know how to re-write all my code so that I can only initialize the mocks of the tests being executed :(
I use markers to categorize tests, so sometimes I only run tests under a single category or just a bunch of them. I don't want to initialize the mocks of all the tests files because not all of them will be executed at that time.
Can anybody help me? I can post some code if necessary.
Thanks in advance!!
I have a file used to run different unit tests on my application; I am using pytest in a python module to run those tests. All seems fine, except that I get the output only at the end.
As comparison, if I run pytest from terminal via command line, I am able to get some of the output from each tests (minus the print statements, which are printed at the end no matter what); but when I run pytest as library inside a python module, everything stops until all the tests are done.
import pytest
#...setup the parameters for the test, prepare everything before call main
pytest.main(parameters)
Basically as soon as the pytest.main is called, I have to wait for all the tests (in my case 18 tests), to be completed, before I see anything in the output. Why there is such a difference between the command line execution of the same python test module, compared to run pytest via main function? And is there a way to have pytest to print the output of each test (including the eventual print statements in the test or functions called by the test), one by one, as they are executed, instead of waiting until the end?
Thanks
Following is the structure of my tests in a file.
Class
setup
test01
test02
test03
teardown
I have a requirement to run specific code before and after each test.
For before, I could invoke that code from the setup.
But for after the test, I am not able to figure how to do it.
Obviously invoking the code from teardown would work for the last test, but how can I have it run for the tests in between?
Assuming that you're properly using a class descended from unittest.TestCase, then the setUp method is run before each test, and the tearDown method is run after each test. Check the documentation. So it's completely feasible to put your code in those two methods.