218

I am running unit tests on a CI server using py.test. Tests use external resources fetched over network. Sometimes test runner takes too long, causing test runner to be aborted. I cannot repeat the issues locally.

Is there a way to make py.test print out execution times of (slow) test, so pinning down problematic tests become easier?

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435

2 Answers2

329

I'm not sure this will solve your problem, but you can pass --durations=N to print the slowest N tests after the test suite finishes.

Use --durations=0 to print all.

ankostis
  • 8,579
  • 3
  • 47
  • 61
Bruno Oliveira
  • 13,694
  • 5
  • 43
  • 41
  • Do you know if there is a possibility to add it to the generated HTML coverage report? Similar like adding `.coveragerc` file with the contents `[run] branch = True` adds branching coverage information? – Martin Thoma Aug 30 '18 at 11:03
  • You will need to add that information yourself, pytest-html has support to insert additional contents. – Bruno Oliveira Aug 31 '18 at 12:07
  • 8
    @oLas: That's not true: If tests are "too fast", the measured time can apparently become 0 and they will still be filtered out. A negative threshold also doesn't help in this case. Another annoyance with this approach is that pytest will always print `(0.00 durations hidden. Use -vv to show these durations.)` which does not make any sense. – bluenote10 Nov 28 '18 at 16:57
  • 3
    @bluenote10 not sure if this is something has been added later as now is 2021 but with `--durations-min=N` you can set the minimal duration in seconds for inclusion in slowest list. Default is 0.005 so even --durations=0 it will not show any under 0.005 unless you set a value for `durations-min` – javrd Oct 18 '21 at 15:36
74

You can pass the number with --durations

pytest --durations=0 — Show all times for tests and setup and teardown

pytest --durations=1 — Just show me the slowest

pytest --durations=50 — Slowest 50, with times, … etc

Take refer in: https://medium.com/@brianokken/pytest-durations-0-show-all-times-for-tests-and-setup-and-teardown-848dccac85db

Or: https://docs.pytest.org/en/latest/usage.html#profiling-test-execution-duration

Phuong
  • 1,153
  • 1
  • 10
  • 17