[TIP] Identifying slowest py.test fixtures

Bruno Oliveira 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[1] 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
finalizer.

A plugin could use this to measure fixture setup time:

@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(fixturedef, request):
    started = time.time()
    yield
    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,
Bruno.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20160723/e3c4f553/attachment.html>


More information about the testing-in-python mailing list