更改 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 挂钩可以执行此操作,但我还没有找到它。
方法summary_failures()
of _pytest.terminal.TerminalReporter
is the one that prints that line (I found it by searching for the string "FAILURES"). It uses _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 _____________
您可以通过 writing a new plugin 使用您自己的默认报告器覆盖以下内容:
class MyOwnReporter(TerminalReporter):
...
def pytest_configure(config):
reporter = MyOwnReporter(config, sys.stdout)
config.pluginmanager.register(reporter, 'terminalreporter')
我有兴趣将我的测试套件从使用 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 挂钩可以执行此操作,但我还没有找到它。
方法summary_failures()
of _pytest.terminal.TerminalReporter
is the one that prints that line (I found it by searching for the string "FAILURES"). It uses _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 _____________
您可以通过 writing a new plugin 使用您自己的默认报告器覆盖以下内容:
class MyOwnReporter(TerminalReporter):
...
def pytest_configure(config):
reporter = MyOwnReporter(config, sys.stdout)
config.pluginmanager.register(reporter, 'terminalreporter')