Pytest: Are Pytest_Sessionstart() and Pytest_Sessionfinish() Valid Hooks?

Pytest: Are Pytest_Sessionstart() and Pytest_Sessionfinish() Valid Hooks?

are pytest_sessionstart(session) and pytest_sessionfinish(session) valid hooks? They are not described in dev hook docs or latest hook docs

What is the difference between them and pytest_configure(config)/pytest_unconfigure(config)?

In docs it is said:

pytest_configure(config)called after command line options have been parsed. and all plugins and initial conftest files been loaded.

and

pytest_unconfigure(config) called before test process is exited.

Session is the same, right?

Thanks!

1 Answer

The bad news is that the situation with sessionstart/configure is not very well specified. Sessionstart in particular is not much documented because the semantics differ if one is in the xdist/distribution case or not. One can distinguish these situations but it's all a bit too complicated.

The good news is that pytest-2.3 should make things easier. If you define a @fixture with scope="session" you can implement a fixture that is called once per process within which test execute.
For distributed testing, this means once per test slave. For single-process testing, it means once for the whole test run. In either case, if you do a "--collectonly" run, or "-h" or other options that do not involve the running of tests, then fixture functions will not execute at all.

Hope this clarifies.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.