[TIP] Identifying slowest py.test fixtures

André Caron andre.l.caron at gmail.com
Sat Jul 23 07:29:36 PDT 2016


Hi Bruno,

Thanks for the quick reply!

pytest 3.0 (which should be released soon) introduces two new hooks:
>

These two new hooks look like what was looking for, so guess that yes, this
will help a lot :-)  Do you have a ballpark-figure of how soon is "soon"?

In current pytest I think one would have to resort to printing elapsed time
> in the fixture functions manually.


Unfortunately, printing is subject to log capture plug-ins, so it's very
inconvenient.  I see your example plug-in uses printing as well.  How would
I go about printing a report at the end like the `--durations` argument
does?  I assume I also need to use the `pytest_terminal_summary()` hook,
but I'm not sure how to pass info from your example
`pytest_fixture_setup()` to the reporting function.  Any hints?

Thanks again,

André

On Sat, Jul 23, 2016 at 10:04 AM, Bruno Oliveira <nicoddemus at gmail.com>
wrote:

> 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/8247da73/attachment.htm>


More information about the testing-in-python mailing list