打印测试执行时间并使用py.test固定慢速测试

问题:打印测试执行时间并使用py.test固定慢速测试

我正在使用py.test在CI服务器上运行单元测试。测试使用通过网络获取的外部资源。有时测试跑步者花费的时间太长,导致测试跑步者被中止。我不能在本地重复这些问题。

有没有一种方法可以使py.test打印出(缓慢)测试的执行时间,因此固定有问题的测试变得更加容易?

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?


回答 0

我不确定这是否可以解决您的问题,但是您可以在测试套件完成后通过--durations=N以打印最慢的N测试。

使用--durations=0打印所有。

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.


回答 1

您可以使用 --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

请参阅:https : //medium.com/@brianokken/pytest-durations-0-show-all-times-for-tests-and-setup-and-teardown-848dccac85db

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

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