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

问题描述 投票:65回答:2

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

有没有办法让py.test打印出(慢)测试的执行时间,所以固定有问题的测试会变得更容易?

python pytest
2个回答
103
投票

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


0
投票

你可以用--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

© www.soinside.com 2019 - 2024. All rights reserved.