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

Printing test execution times and pinning down slow tests with py.test

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

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


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


您可以使用--durations传递数字

1
2
3
4
5
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