Python测试框架pytest(20)插件 - 生成html报告、重复执行用例、用例执行顺序、多重断言

目录

1、pytest-html(生成html报告)

1.1、安装

1.2、操作参数

1.3、报告优化(报错截图) 

1.4、报告优化(用例描述+报错截图) 

2、pytest-repeat(重复执行用例) 

2.1、安装 

2.2、操作参数

2.3、兼容性 

3、pytest-ordering(用例执行顺序) 

3.1、安装 

3.2、pytest默认执行

3.3、pytest-ordering自定义用例顺序 

4、pytest-assume(多重断言)

4.1、安装

4.2、assert多重断言

4.3、pytest-assume多重断言 

4.4、上下文管理器 


1、pytest-html(生成html报告)

1.1、安装

在命令行中运行以下命令进行安装:

pip install pytest-html

或者(使用国内的豆瓣源,数据会定期同步国外官网,速度快。)

pip install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

1.2、操作参数

命令行参数:

  • --html=report.html(普通HTML报告,CSS是独立的,分享报告的时候样式会丢。)

  • --html=report.html --self-contained-html(合并CSS的HTML报告,分享报告样式不丢失,如:分享发邮件展示报告。)

创建test_html.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def func(abc):
    return abc + 1

def test_case3():
    assert func(3) == 5

class TestClass:
    def test_case1(self):
        x = "AllTests"
        assert "t" in x

    def test_case2(self):
        y = "hello"
        assert "h" in y

1.2.1、HTML报告(普通)

打开命令行,在该脚本目录下,输入执行命令:

pytest test_html.py --html=report.html

执行完成后,在当前目录下自动创建一个report.html的测试报告。

打开测试报告:

1.2.2、HTML报告(合并CSS)

打开命令行,在该脚本目录下,输入执行命令:

pytest test_html.py --html=report.html --self-contained-html

执行完成后,在当前目录下自动创建一个report.html的测试报告。

打开测试报告:

1.3、报告优化(报错截图) 

在执行web自动化的时候,希望测试用例失败时,以截图的方式展示在html报告里。

1、修改conftest.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
from selenium import webdriver
import pytest

driver = None

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
    当测试失败的时候,自动截图,展示到html报告中
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            screen_img = _capture_screenshot()
            if file_name:
                html = '<div><img src="https://img-blog.csdnimg.cn/2022010621343227311.png" alt="screenshot" style="width:600px;height:300px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % screen_img
                extra.append(pytest_html.extras.html(html))
        report.extra = extra

def _capture_screenshot():
    """
    截图保存为base64,展示到html中
    """
    return driver.get_screenshot_as_base64()

@pytest.fixture(scope='session', autouse=True)
def browser(request):
    global driver
    if driver is None:
        driver = webdriver.Chrome()

    def end():
        driver.quit()
    request.addfinalizer(end)
    return driver

2、创建test_html_screenshot.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

from selenium import webdriver
import time

def test_case(browser):
    browser.get("https://www.baidu.com/")
    time.sleep(2)
    t = browser.title
    assert t == "AllTests软件测试"

3、打开命令行,在该脚本目录下,输入执行命令:

pytest test_html_screenshot.py --html=report.html --self-contained-html

4、运行结果:

将失败截图展示在报告里

1.4、报告优化(用例描述+报错截图) 

pytest-html 测试报告默认是不展示用例描述 Description 内容,可以修改生成的报告内容,添加或删除 html 报告的 table 内容。

1、修改conftest.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
from selenium import webdriver
from py.xml import html
import pytest

driver = None

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
    当测试失败的时候,自动截图,展示到html报告中
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            screen_img = _capture_screenshot()
            if file_name:
                html = '<div><img src="https://img-blog.csdnimg.cn/2022010621343227311.png" alt="screenshot" style="width:600px;height:300px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % screen_img
                extra.append(pytest_html.extras.html(html))
        report.extra = extra
    report.description = str(item.function.__doc__)

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Description'))

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))

def _capture_screenshot():
    """
    截图保存为base64,展示到html中
    """
    return driver.get_screenshot_as_base64()

@pytest.fixture(scope='session', autouse=True)
def browser(request):
    global driver
    if driver is None:
        driver = webdriver.Chrome()

    def end():
        driver.quit()
    request.addfinalizer(end)
    return driver

2、创建test_html_screenshot_description.py文件

测试用例下三个引号里面的注释(docstring)内容就是测试报告展示的Description内容;如果没有注释内容,报告Description内容显示None。

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

from selenium import webdriver
import time

def test_case01(browser):
    """
    测试用例1 失败用例
    """
    browser.get("https://www.baidu.com/")
    time.sleep(2)
    t = browser.title
    assert t == "AllTests软件测试"

def test_case02(browser):
    """
    测试用例2 成功用例
    """
    browser.get("https://www.baidu.com/")
    time.sleep(2)
    t = browser.title
    assert t == "百度一下,你就知道"

3、打开命令行,在该脚本目录下,输入执行命令:

pytest test_html_screenshot_description.py --html=report.html --self-contained-html

4、运行结果:

用例描述显示在报告的Description内,并且将失败截图展示在报告里。

2、pytest-repeat(重复执行用例) 

2.1、安装 

在命令行中运行以下命令进行安装:

pip install pytest-repeat

或者(使用国内的豆瓣源,数据会定期同步国外官网,速度快。)

pip install pytest-repeat -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2.2、操作参数

1、命令行参数:

--count=n(运行n次一个或多个测试用例)

或者(两种方式皆可,等号或空格)

--count n

--repeat-scope(可以覆盖默认的测试用例执行顺序,类似 fixture 的 scope 参数)

(1)function:默认值,范围针对每个用例重复执行,再执行下一个用例。

(2)class:以类为用例集合单位,重复执行类里面的用例,再执行下一个。

(3)module:以模块为单位,重复执行模块里面的用例,再执行下一个。

(4)session:重复整个测试会话,即所有测试用例执行一次,然后再次执行所有此类测试。

-x(如果您尝试诊断间歇性故障,则一次又一次地运行相同的测试直到失败,将很有用。您可以将 pytest 的 -x 选项与 pytest-repeat 结合使用,以强制测试运行器在第一次失败时停止。)

例如:

pytest --count=1000 -x test_file.py

这将尝试运行test_file.py 1000次,但将在发生故障后立即停止。

2、装饰器参数(如果要在代码中将某些测试用例标记为执行重复多次,可以使用此装饰器。):

@pytest.mark.repeat(count)

2.2.1、重复执行(命令行)

1、创建test_repeat.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def test_case3():
    assert 123 == 123

class TestClass:
    def test_case1(self):
        x = "AllTests"
        assert "t" in x

    def test_case2(self):
        y = "hello"
        assert "h" in y

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest --count=2 test_repeat.py

3条用例,每条用例执行2次,所以总共执行6次用例。

2.2.2、重复执行(装饰器@pytest.mark.repeat(count))

如果要在代码中标记要重复多次的测试,可以使用 @pytest.mark.repeat(count) 装饰器。

1、创建test_repeat2.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.mark.repeat(5)
def test_case():
    print("\n执行测试用例")

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest -s test_repeat2.py

用例执行5次。

2.2.3、重复执行(执行顺序-class)

1、创建test_repeat3.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

class Test_class1:
    def test_case1(self):
        print("执行测试用例test_case1")

class Test_class2:
    def test_case2(self):
        print("执行测试用例test_case2")

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest -s --count=2 --repeat-scope=class test_repeat3.py

重复执行类里面的用例,再执行下一个类里面的用例。

2.2.4、重复执行(执行顺序-module)

1、创建test_repeat4.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def test_case1():
    print("执行测试用例test_case1")

def test_case2():
    print("执行测试用例test_case2")

class Test_class1:
    def test_case3(self):
        print("执行测试用例test_case3")

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest -s --count=2 --repeat-scope=module test_repeat4.py

2.2.5、重复执行直到失败

1、创建test_repeat5.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import random

def test_case():
    flag = random.choice([True, False])
    print(flag)
    assert flag

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest -s --count=10 -x test_repeat5.py

尝试执行10次,运行第4次失败,立即停止。

2.3、兼容性 

pytest-repeat 不能与 unittest.TestCase 测试类一起使用。无论 --count 设置多少,这些测试始终仅运行一次,并显示警告。 

3、pytest-ordering(用例执行顺序) 

pytest-ordering 插件可以控制用例的执行顺序。

3.1、安装 

在命令行中运行以下命令进行安装:

pip install pytest-ordering

或者(使用国内的豆瓣源,数据会定期同步国外官网,速度快。)

pip install pytest-ordering -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

3.2、pytest默认执行

1、创建test_ordering.py文件

pytest默认的执行顺序(用例先后顺序执行)

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def test_case2():
    print("用例test_case2")
    assert True

def test_case1():
    print("用例test_case1")
    assert True

def test_case3():
    print("用例test_case3")
    assert True

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest -s test_ordering.py

按照用例顺序,先执行test_case2,之后执行test_case1,最后执行test_case3。

3.3、pytest-ordering自定义用例顺序 

1、修改test_ordering.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.mark.run(order=2)
def test_case2():
    print("用例test_case2")
    assert True

@pytest.mark.run(order=1)
def test_case1():
    print("用例test_case1")
    assert True

@pytest.mark.run(order=3)
def test_case3():
    print("用例test_case3")
    assert True

2、运行结果:

打开命令行,在该脚本目录下,输入执行命令:

pytest -s test_ordering.py

使用pytest-ordering自定义用例顺序,先执行test_case1,之后执行test_case2,最后执行test_case3。

4、pytest-assume(多重断言)

pytest 中可以用 python 的 assert 断言,也可以写多个断言,但是如果一个失败,那么后面的断言将不再执行。

此时可以用 pytest-assume 插件,每个测试允许多次失败。

4.1、安装

在命令行中运行以下命令进行安装:

pip install pytest-assume

或者(使用国内的豆瓣源,数据会定期同步国外官网,速度快。)

pip install pytest-assume -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

4.2、assert多重断言

1、创建test_assert.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

def test_addcase():
    assert 1 + 2 == 3
    assert 1 + 3 == 4
    assert 1 + 4 == 6
    assert 1 + 5 == 6
    print("测试完成")

2、运行结果:

第三个断言(assert 1 + 4 == 6)失败之后,后面的断言也不会执行,包括正常的代码。

4.3、pytest-assume多重断言 

1、创建test_assume.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

def test_addcase():
    pytest.assume(1 + 2 == 3)
    pytest.assume(1 + 3 == 4)
    pytest.assume(1 + 4 == 6)
    pytest.assume(1 + 5 == 6)
    print("测试完成")

2、运行结果:

第三个断言(pytest.assume(1 + 4 == 6))失败之后,后面的断言还是会执行的。

4.4、上下文管理器 

pytest.assume 也可以用作围绕普通断言的上下文管理器。

1、创建test_assume2.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest
from pytest import assume

@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
    with assume: assert x == y
    with assume: assert True
    with assume: assert False

2、运行结果:

注意:每个 with 块只能有一个断言。如果有多个断言,当第一个失败了,则不会完全验证之后的所有断言。

1、创建test_assume3.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.mark.parametrize(('x', 'y'), [(1, 1)])
def test_simple_assume(x, y):
    with pytest.assume:
        assert x != y
        assert x == y

2、运行结果:

<think>好的,我现在需要帮用户解决如何生成HTML格式测试报告的问题。首先,用户提到了参考信息中的网站安全测试报告模板,其中提到测试人员根据结果编写报告,所以可能需要结合这个思路。 用户可能是在做软件测试或者安全测试,需要将结果以HTML形式展示,方便查看或分享。常见的生成HTML报告的方法有哪些呢?首先想到的是使用现有的测试框架,比如Pythonpytest-html插件,或者Java中的ExtentReports库。这些工具可以自动生成格式美观的HTML报告,用户可能只需要添加相应的依赖和配置。 另外,用户可能需要自定义模板。这时候需要了解HTML和CSS的基础知识,设计报告的结构和样式。比如,使用Jinja2模板引擎将数据填充到HTML中,这样灵活性更高。可能还需要用JavaScript来增强交互性,比如折叠部分内容或动态图表。 用户也可能需要模板示。我应该提供一个简单的HTML模板结构,包含标题、概述、测试结果、总结等部分,并说明如何将数据填入。比如,使用表格展示测试用,用不同的颜色标记通过、失败的情况。同时,提醒用户替换示数据为实际结果。 还需要考虑用户的技术背景。如果他们熟悉Python,推荐pytest-html会更合适;如果是Java项目,ExtentReports可能更适合。对于手动生成的情况,可能需要指导他们如何用模板引擎和基本的HTML编写。 最后,确保回答结构清晰,分点说明不同方法,并给出示代码和模板,这样用户可以根据自己的情况选择合适的方式。同时,注意引用用户提到的报告模板部分,确保信息相关。</think>### 如何生成HTML测试报告 #### 一、使用测试框架自带功能 1. **Python pytest-html** 安装插件后可直接生成HTML报告: ```bash pip install pytest-html pytest --html=report.html ``` 报告包含测试用状态、执行时间和错误详情,支持自定义CSS样式[^1]。 2. **Java ExtentReports** 通过API构建交互式报告: ```java ExtentReports extent = new ExtentReports(); ExtentTest test = extent.createTest("LoginTest"); test.pass("Authentication passed"); extent.flush(); ``` 生成包含图表分析的多层级报告。 #### 二、手动构建HTML模板 1. **基础模板结构** ```html <!DOCTYPE html> <html> <head> <title>安全测试报告</title> <style> .passed {background: #dfd} .failed {background: #fdd} </style> </head> <body> <h2>测试概述</h2> <table> <tr><th>用ID</th><th>状态</th></tr> <tr><td>TC001</td><td class="passed">通过</td></tr> </table> </body> </html> ``` 2. **动态数据注入(Python)** 使用Jinja2模板引擎: ```python from jinja2 import Template data = {"cases": [{"id":"TC002", "status":"failed"}]} with open('template.html') as f: html = Template(f.read()).render(data) with open('report.html', 'w') as f: f.write(html) ``` #### 三、企业级解决方案 企业测试平台(如TestRail、Allure)可自动生成含以下要素的报告- 测试环境配置 - 缺陷分布热力图 - 通过率历史趋势图 - 附件日志关联
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangmcn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值