【发布时间】:2018-12-20 00:57:30
【问题描述】:
我想在 pandas 数据框中解析和收集 pytest 结果。 有什么办法解析吗?
我只找到了这个参考,不知道如何使用它。 Collecting and Reporting pytest Results
【问题讨论】:
我想在 pandas 数据框中解析和收集 pytest 结果。 有什么办法解析吗?
我只找到了这个参考,不知道如何使用它。 Collecting and Reporting pytest Results
【问题讨论】:
您可以在您的conftest.py 中实现自定义makereport 挂钩。一个简单的例子:
import pytest
import pandas as pd
df = pd.DataFrame(columns=('failed', 'nodeid', ))
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
global df
outcome = yield
rep = outcome.get_result()
if rep.when == 'call':
df = df.append({'failed': rep.failed, 'nodeid': rep.nodeid}, ignore_index=True)
【讨论】:
您可以为此使用pytest-harvest。只需安装它,就可以直接使用预定义的fixture:
import pytest
import time
@pytest.mark.parametrize('p', ['world', 'self'], ids=str)
def test_foo(p):
"""
A dummy test, parametrized so that it is executed twice
"""
print('\n hello, ' + p + ' !')
time.sleep(len(p) / 10)
def test_synthesis(module_results_df):
"""
Shows that the `module_results_df` fixture already contains what you need
"""
# drop the 'pytest_obj' column
module_results_df.drop('pytest_obj', axis=1, inplace=True)
print("\n `module_results_df` dataframe:\n")
print(module_results_df)
产量
>>> pytest -s -v
============================= test session starts =============================
...
collecting ... collected 3 items
test_basic.py::test_foo[world]
hello, world !
PASSED
test_basic.py::test_foo[self]
hello, self !
PASSED
test_basic.py::test_synthesis
`module_results_df` dataframe:
status duration_ms p
test_id
test_foo[world] passed 500.028610 world
test_foo[self] passed 400.022745 self
PASSED
========================== 3 passed in 0.05 seconds ===========================
您还可以从 'dict' 夹具开始,其中包含有关设置/拆卸时间的更多详细信息,并使用提供的辅助方法将其转换为数据帧。 有关详细信息,请参阅文档。
最后,如果你还想使用参数、夹具、步骤……你不妨看看这个datascience benchmark example
顺便说一句,我是作者;)
【讨论】: