我需要使用热应激来运行一段时间的测试。以前,我使用的是基于迭代次数的吡喃-重复。我使用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
示例代码:
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
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。
发布于 2022-07-18 22:47:29
创建了我自己的pytest插件,叫做pytest-循环。它合并了pytest-压力和pytest-重复与修复,以清除以前的报告。
https://stackoverflow.com/questions/73000833
复制相似问题