简体   繁体   English

如何从pytest中获取通过、失败和跳过的测试总数

[英]How to get the total number of tests passed, failed and skipped from pytest

How can I get to the statistics information of a test session in pytest?如何在pytest中获取测试会话的统计信息?

I've tried to define pytest_sessionfinish in the conftest.py file, but I only see testsfailed and testscollected attributes on there.我试图在conftest.py文件中定义pytest_sessionfinish ,但我只在那里看到testsfailedtestscollected属性。

I also need to know the number of tests passed, skipped and the total time it took.我还需要知道通过、跳过的测试数量以及花费的总时间。 Since pytest prints that info at the end of each session, I assume there's a programmatic way to retrieve that.由于 pytest 在每个会话结束时打印该信息,我假设有一种编程方式来检索该信息。

Use the pytest_terminal_summary hook.使用pytest_terminal_summary钩子。 The stats are provided by the terminalreporter object.统计信息由terminalreporter对象提供。 Example:例子:

# conftest.py

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    print('passed amount:', len(terminalreporter.stats['passed']))
    print('failed amount:', len(terminalreporter.stats['failed']))
    print('xfailed amount:', len(terminalreporter.stats['xfailed']))
    print('skipped amount:', len(terminalreporter.stats['skipped']))

    duration = time.time() - terminalreporter._sessionstarttime
    print('duration:', duration, 'seconds')

Unfortunately, _pytest.terminal.TerminalReporter isn't part of the public API yet, so it's best to inspect its code directly.不幸的是, _pytest.terminal.TerminalReporter公共 API 的一部分,因此最好直接检查其代码


If you need to access the stats in another hook like pytest_sessionfinish , use the plugin manager, eg:如果您需要访问另一个钩子(如pytest_sessionfinish的统计信息,请使用插件管理器,例如:

def pytest_sessionfinish(session, exitstatus):
    reporter = session.config.pluginmanager.get_plugin('terminalreporter')
    print('passed amount:', len(reporter.stats['passed']))
    ...

However, depending on what hook you are in, you may not get the complete stats/correct duration, so caution advised.但是,根据您所处的钩子,您可能无法获得完整的统计数据/正确的持续时间,因此建议谨慎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pytest-4.xx:如何报告像XFAILED这样的SKIPPED测试? - pytest-4.x.x: How to report SKIPPED tests like XFAILED? pytest 不承认基类中的 PASSED 依赖关系导致派生类中的 SKIPPED 测试 - pytest not acknowledging PASSED dependency in base class results in SKIPPED tests in derived class 仅返回通过的测试数量和失败的测试数量 - Returning only number of tests passed and number of tests failed 如何在 pytest bdd 中包含 selenium 屏幕截图以进行测试? - How to include selenium screenshot in pytest bdd for passed tests? pytest:如何在会话结束时获取所有失败测试的列表? (并且在使用 xdist 时) - pytest: How to get a list of all failed tests at the end of the session? (and while using xdist) 使用 --collect-only 运行 pytest 时如何报告跳过测试的原因? - How to report reasons for skipped tests when running pytest with --collect-only? Pytest报告测试跳过unittest.skip传递 - Pytest reports test skipped with unittest.skip as passed 如何获得总数 - How to get total number Pytest - 使用 VSCode 进行调试时会跳过测试中的断点 - Pytest - Breakpoints in tests are being skipped when debugging with VSCode 在 pytest 中显示通过测试的详尽信息 - Show exhaustive information for passed tests in pytest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM