繁体   English   中英

更改py.test测试的名称

[英]Change the name of py.test tests

我有兴趣将我的测试套件从使用nose迁移到使用py.test。 我不喜欢测试失败摘要中py.test打印的测试名称,因为它们不包含测试的文件名。 我非常喜欢py.test用于以详细模式打印进度的测试名称。

例如:

[dbw tests]$ py.test -sv r_group/test_meta_analysis/test_meta_analysis.py::_TestPanel::testDisplaySummary
==================================== test session starts ===============================
collected 3 items

r_group/test_meta_analysis/test_meta_analysis.py::_TestPanel::testDisplaySummary FAILED

========================================= FAILURES =====================================
_______________________________ _TestPanel.testDisplaySummary __________________________

这对我来说非常重要,因为我有~40K测试,短名称“_TestPanel.testDisplaySummary”对我快速找到我想要的测试没有帮助。 我假设有一个内置的py.test钩子可以做到这一点,但我还没有找到它。

_pytest.terminal.TerminalReporter的方法summary_failures()是打印该行的方法(我通过搜索字符串“FAILURES”找到它)。 它使用_getfailureheadline()来获取测试名称(并丢弃文件名和行号)。

我建议_getfailureheadline() TerminalReporter并覆盖_getfailureheadline()

例如:

def _getfailureheadline(self, rep):
    if hasattr(rep, 'location'):
        fspath, lineno, domain = rep.location
        return '::'.join((fspath, domain))
    else:
        return super()._getfailureheadline(rep)

产生以下输出:

test.py::test_x FAILED

================ FAILURES ================
____________ test.py::test_x _____________

您可以使用以下内容编写新插件来覆盖默认报告者:

class MyOwnReporter(TerminalReporter):
    ...

def pytest_configure(config):
    reporter = MyOwnReporter(config, sys.stdout)
    config.pluginmanager.register(reporter, 'terminalreporter')

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM