首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >报告pytest-html中每个迭代的压力结果。

报告pytest-html中每个迭代的压力结果。
EN

Stack Overflow用户
提问于 2022-07-16 00:59:06
回答 1查看 182关注 0票数 0

我需要使用热应激来运行一段时间的测试。以前,我使用的是基于迭代次数的吡喃-重复。我使用pytest-html来报告结果。

当我从“重复”切换到“重音”时,测试的所有迭代都会出现在一个测试行中。重复能够将测试分离成不同的迭代。我试图让压力只在pytest-html的单独行中列出测试和当前的迭代计数。

我知道pytest压力是覆盖pytest_runtestloop的,所以它应该在会话范围级别运行。我确实尝试过从pytest_generate_tests中添加一些pytest-重复重写功能,因为它在函数作用域级别运行。

我想让结果分别报告每一次迭代(我在这里删除了前面的可读性路径)。

例:

test_report.py::TestReport::test_fail[1]

test_report.py::TestReport::test_fail[2]

test_report.py::TestReport::test_fail[3]

pytest-repeat.png pytest-stress.png

示例代码:

代码语言:javascript
复制
import logging
class TestReport:
    def test_pass(self):
        logging.info("assert(True)")
        assert(True)

    def test_fail(self):
        logging.info("assert(False)")
        assert(False)

Conftest.py

代码语言:javascript
复制
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th("Time", class_="sortable time", col="time"))
    cells.pop()


def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(datetime.utcnow(), class_="col-time"))
    cells.pop()


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    setattr(report, "duration_formatter", "%H:%M:%S.%f")

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
    if not os.path.exists('results'):
        os.makedirs('results')
    config.option.htmlpath = 'results/results_'+datetime.now().strftime("%Y-%m-%d_T%H-%M-%S")+".html"

#change name based on iteration
def pytest_itemcollected(item):
    cls = item.getparent(pytest.Class)
    iter = 0
    if hasattr(cls.obj, "iteration"):
        iter = int(getattr(cls.obj, "iteration"))
    item._nodeid = item._nodeid + f"[{iter}]"

我不知道这是否可能通过对conftest.py的小编辑,或者我是否需要创建我自己的插件,以便在一段时间内运行pytest。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-18 22:47:29

创建了我自己的pytest插件,叫做pytest-循环。它合并了pytest-压力和pytest-重复与修复,以清除以前的报告。

https://github.com/anogowski/pytest-loop

https://pypi.org/project/pytest-loop/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73000833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档