【问题标题】:Printing test execution times and pinning down slow tests with py.test使用 py.test 打印测试执行时间并锁定慢速测试
【发布时间】:2015-03-09 04:21:08
【问题描述】:

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

有没有办法让 py.test 打印出(慢)测试的执行时间,从而更容易确定有问题的测试?

【问题讨论】:

    标签: python pytest


    【解决方案1】:

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

    使用--durations=0打印全部。

    【讨论】:

    • 如果您通过--durations=0 将报告所有测试的执行时间。
    • 您知道是否有可能将其添加到生成的 HTML 覆盖率报告中?类似于添加带有内容[run] branch = True.coveragerc 文件添加分支覆盖信息?
    • 您需要自己添加该信息,pytest-html 支持插入其他内容。
    • @oLas:这不是真的:如果测试“太快”,测量的时间显然会变成 0,它们仍然会被过滤掉。在这种情况下,负阈值也无济于事。这种方法的另一个烦恼是 pytest 将始终打印 (0.00 durations hidden. Use -vv to show these durations.) 这没有任何意义。
    • @bluenote10 不确定这是否是后来添加的,因为现在是 2021 年,但使用 --durations-min=N 您可以设置最短持续时间(以秒为单位)以包含在最慢列表中。默认值为 0.005,因此即使 --durations=0 也不会显示任何低于 0.005 的值,除非您为 durations-min 设置值
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      相关资源
      最近更新 更多