我知道有一些用于py.test的性能测试和性能分析的插件,但是有没有一种方法可以生成任意值,这些值可以在测试后报告或以某种方式访问?
想象一下我有这样的测试
def test_minimum_learning_rate():
"""Make some fancy stuff and generate a learning performance value"""
learning_rate = fancy_learning_function().rate
pytest.report("rate", learning_rate)
assert learning_rate > 0.5
pytest.report(..)
行是我希望拥有的(但不在那里,是吗?)
现在我想将类似minimum_learning_rate[rate]
的内容与实际测试结果一起写入报告中(或至少在屏幕上)。
真的 nice将是Jenkins的一个插件,该插件可以根据该数据创建一个美观的图表。
是否有一个典型的措词?我一直在寻找kpi
,arbitrary values
,user defined values
,但是还没有运气。
答案 0 :(得分:2)
如果您只想输出一些调试值,则将print
参数与-s
组合起来就可以了:
def test_spam():
print('debug')
assert True
运行pytest -s
:
collected 1 item
test_spam.py debug
.
如果您正在寻找一种更好地集成到pytest
执行流程中的解决方案,请编写自定义钩子。下面的示例应给您一些想法。
# conftest.py
def pytest_report_teststatus(report, config):
if report.when == 'teardown': # you may e.g. also check the outcome here to filter passed or failed tests only
rate = getattr(config, '_rate', None)
if rate is not None:
terminalreporter = config.pluginmanager.get_plugin('terminalreporter')
terminalreporter.ensure_newline()
terminalreporter.write_line(f'test {report.nodeid}, rate: {rate}', red=True, bold=True)
测试:
def report(rate, request):
request.config._rate = rate
def test_spam(request):
report(123, request)
def test_eggs(request):
report(456, request)
输出:
collected 2 items
test_spam.py .
test test_spam.py::test_spam, rate: 123
test_spam.py .
test test_spam.py::test_eggs, rate: 456
===================================================== 2 passed in 0.01 seconds =====================================================
# conftest.py
def pytest_configure(config):
config._rates = dict()
def pytest_terminal_summary(terminalreporter, exitstatus, config):
terminalreporter.ensure_newline()
for testid, rate in config._rates.items():
terminalreporter.write_line(f'test {testid}, rate: {rate}', yellow=True, bold=True)
测试:
def report(rate, request):
request.config._rates[request.node.nodeid] = rate
def test_spam(request):
report(123, request)
def test_eggs(request):
report(456, request)
输出:
collected 2 items
test_spam.py ..
test test_spam.py::test_spam, rate: 123
test test_spam.py::test_eggs, rate: 456
===================================================== 2 passed in 0.01 seconds =====================================================
使用record_property
固定装置:
def test_spam(record_property):
record_property('rate', 123)
def test_eggs(record_property):
record_property('rate', 456)
结果报告:
$ pytest --junit-xml=report.xml
...
$ xmllint --format report.xml
<testsuite errors="0" failures="0" name="pytest" skipped="0" tests="2" time="0.056">
<testcase classname="test_spam" file="test_spam.py" line="12" name="test_spam" time="0.001">
<properties>
<property name="rate" value="123"/>
</properties>
</testcase>
<testcase classname="test_spam" file="test_spam.py" line="15" name="test_eggs" time="0.001">
<properties>
<property name="rate" value="456"/>
</properties>
</testcase>
</testsuite>