[TIP] Identifying slowest py.test fixtures
nicoddemus at gmail.com
Sat Jul 23 07:04:09 PDT 2016
On Fri, Jul 22, 2016 at 1:23 PM André Caron <andre.l.caron at gmail.com> wrote:
> Greetings from Canada :-)
Hi André, greetings from Brazil. :)
> I'm trying to speed up the CI cycle on one project I'm working on and I'm
> trying to to figure out where time is spent by the test suite.
> I just discovered the excellent --durations option which prints a neat
> report containing the slowest tests.
> This has been of much help to get started (I've already spotted a few
> tests to optimize), but it doesn't seem to track time spent in test
> fixtures. I'd like to track metrics for test fixtures, including number of
> calls, total time spent in order to optimize the slower fixtures.
pytest 3.0 (which should be released soon) introduces two new hooks:
* pytest_fixture_setup(fixturedef, request): executes fixture setup;
* pytest_fixture_post_finalizer(fixturedef): called after the fixture's
A plugin could use this to measure fixture setup time:
def pytest_fixture_setup(fixturedef, request):
started = time.time()
elapsed = time.time() - started
print('fixture', fixturedef.name, 'setup took %.3fs' % elapsed)
In current pytest I think one would have to resort to printing elapsed time
in the fixture functions manually.
Hope this helps,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python