CIsForCookies
CIsForCookies

Reputation: 12837

running a pytest hook only once on the start of the tests

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?

Upvotes: 3

Views: 2749

Answers (1)

Alderven
Alderven

Reputation: 8270

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

Upvotes: 1

Related Questions