pytest 钩子来访问持续时间

问题描述 投票:1回答:1

当我使用xdist和durations进行测试时 pytest -n 3 --durations=10pytest给我的控制台输出是这样的。

=========================== slowest 10 test durations ===========================
409.46s setup    test/cases/test_one.py::TestOne::test_one
189.82s call     test/cases/test_two.py::TestTwo::test_two   
...
...

后台: 我想通过编程访问这些结果,以便我们能够对慢速测试进行精细的跟踪记录,特别是识别慢速测试& 固定装置。

问题:我希望能够通过程序访问这些结果,以便我们能够对慢速测试进行精细的跟踪记录,特别是识别慢速测试和固定装置。: 有没有办法通过pytest钩子来获取这些信息?

pytest python-3.7
1个回答
1
投票

时间被存储在 duration 的领域 TestReport 对象,所以它可以在所有的报告钩子中访问。例子。

def pytest_report_teststatus(report, config):
    if report.when == "call":
        print("duration reported immediately after test execution:", report.duration)

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    for reps in terminalreporter.stats.values():
        for rep in reps:
            if rep.when == "call":
                print("duration reported after all tests passed:", rep.nodeid, rep.duration)
© www.soinside.com 2019 - 2024. All rights reserved.