Python 在数据帧中收集pytest结果

Python 在数据帧中收集pytest结果,python,pytest,Python,Pytest,我想在pandas dataframe中解析和收集pytest结果。 有没有办法解析它 我只找到了这个参考资料,不知道如何使用它。 您可以在conftest.py中实现自定义makereport钩子。一个简单的例子: import pytest import pandas as pd df = pd.DataFrame(columns=('failed', 'nodeid', )) @pytest.hookimpl(tryfirst=True, hookwrapper=True) def

我想在pandas dataframe中解析和收集pytest结果。 有没有办法解析它

我只找到了这个参考资料,不知道如何使用它。

您可以在
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)

您可以在
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)
你可以用这个。只需安装,即可直接使用预定义的固定装置:

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”fixture开始,该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”fixture开始,该fixture包含有关设置/拆卸时间的更多详细信息,并使用提供的帮助器方法将其转换为数据帧。 有关详细信息,请参阅文档

最后,如果您还希望使用参数、装置、步骤。。。你可能想看看这个

顺便说一句,我是作者;)