Reputation: 535
There is the overview in pytest after running the test:
============= 7 passed in 1.11 seconds ============
I like to access the time (here the 1.11) in the end of pytest.
some helpful information I found already here:
How can I access the overall test result of a pytest test run during runtime?
But did not match the overall test run time.
I tried to use the def pytest_sessionfinish(session, exitstatus)
I did not find the overall duration there.
It looks like the value is inside the RunResult object. https://docs.pytest.org/en/latest/_modules/_pytest/pytester.html#RunResult
How to access?
Upvotes: 1
Views: 2780
Reputation: 66171
You can access execution start timestamp in the TerminalReporter
plugin instance, then just calculate the execution time yourself (this is also what pytest
itself does). Example:
# conftest.py
import time
def pytest_sessionfinish(session, exitstatus):
reporter = session.config.pluginmanager.get_plugin('terminalreporter')
duration = time.time() - reporter._sessionstarttime
reporter.write_sep('=', 'duration: {} seconds'.format(duration), yellow=True, bold=True)
As noted by looki in the comments, this will not return the overall execution time when running the tests distributed with pytest-xdist
as each process becomes its own session instance. Switch to the pytest_unconfigure
hook (the implementation is almost the same):
def pytest_unconfigure(config):
reporter = config.pluginmanager.get_plugin('terminalreporter')
duration = time.time() - reporter._sessionstarttime
reporter.write_sep('=', 'duration: {} seconds'.format(duration), yellow=True, bold=True)
Upvotes: 4