python pytest测试框架如何在测试报告中显示用例描述信息

405 篇文章 4 订阅
74 篇文章 0 订阅

首先,我们要知道pytest-html测试报告默认是不展示用例描述Description内容,可以修改生成的报告内容的,可以自己添加和删除html报告的table内容。

下面是我自己生成测试报告的效果图。

如何修改测试报告内容:

请详细看下面我的步骤

我们要知道conftest.py文件是干什么用的

具体来说,`conftest.py`文件应该放置在包含测试用例的目录中,以便pytest能够自动识别并加载其中的配置和fixture函数。当pytest运行时,它会自动查找并加载所有的`conftest.py`文件。

如果你的测试目录结构如下所示:

tests/
    conftest.py
    test_module1.py
    test_module2.py

那么`conftest.py`文件将被应用于整个`tests`目录及其子目录中的所有测试模块。

注意,`conftest.py`文件的命名是固定的,不能更改为其他名称。这是pytest框架的约定。

在`conftest.py`文件中,你可以定义各种pytest的配置选项,如自定义的fixture函数、钩子函数、插件配置等。这些配置和函数将在整个测试过程中生效,并可以被所有测试模块共享和使用。

总结起来,`conftest.py`文件是一个用于存放pytest配置和共享fixture函数的特殊文件,应该放置在包含测试用例的目录中,以便pytest能够自动加载并应用其中的配置和函数。

这是 conftest.py文件的代码。

conftest.py

from datetime import datetime
from py.xml import html
import pytest
import inspect
from py.xml import html
import pytest
from datetime import datetime
from conf.red_conf import Project_url # 这是我自己引入的模块,可以注释
import io
from PIL import ImageGrab


def _capture_screenshot():
    # 截取当前屏幕的截图
    img = ImageGrab.grab()
    # 将截图转换为二进制数据
    buffer = io.BytesIO()
    img.save(buffer, format='PNG')
    img_str = buffer.getvalue()
    return img_str


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    # report.nodeid = report.nodeid.encode("utf-8")    # 设置编码中文


"""
Environment部分
"""
def pytest_configure(config):
    config._metadata['测试地址'] = Project_url[:len(Project_url)-4]


"""
Summary部分
"""
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    #Get configure content.
    prefix.extend([html.p("测试人: 测试人员")])

"""
Results部分-头
"""
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th("Description"))
    cells.insert(3, html.th("Time", class_="sortable time", col="time"))
    cells.pop()

"""
Results部分-行
"""
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))
    cells.insert(3, html.td(datetime.now(), class_="col-time"))#当前时间
    cells.pop()

更改描述部分

pytest-html默认获取的是测试方法的__doc__属性,也就是,测试函数下的注释   如下的"""  """中的内容.

下面是一个示例测试用例代码:

test_005.py

# conding:utf-8

import pytest

'''
从结果可以看出用例 1 失败了,用例 2 呾 3 没执行,直接标记为
xfail 了
'''

canshu = [{'user': 'admin', 'psw': ''}]

@pytest.fixture(scope='module')
def login(request):
    user = request.param['user']
    psw = request.param['psw']
    print('正在操作登录,账号:%s, 密码:%s' % (user, psw))
    if psw:
        return True
    else:
        return False

class Test_xx():

    def test_01(self, login):
        '''用例1登录'''
        resulT= login
        print('用例1:%s' % resulT)
        assert resulT == True

    def test_02(self, login):
        resulT = login
        print('用例2, 登录结果:%s' % resulT)
        if not resulT:
            pytest.xfail('登录不成功, 标记为xfail')
        assert 1 == 1

    @pytest.mark.flaky(reruns=1, reruns_delay=2)  # 使用装饰器进行失败重运行 ,可在命令行 –reruns=1 reruns_delay=2 失败后重运行1次,延时2s
    def test_03(self, login):
        resulT = login
        print('用例3,登录结果:%s' % resulT)
        if not resulT:
            pytest.xfail('登录不成功, 标记xfail')
        assert 1 == 1

if __name__ == '__main__':
    pytest.main(['-s', '--lf',  'test_005.py'])  # 收集到第一次失败的用例,进行执行/则运行全部

希望对大家有帮助!不清楚及时问作者,注意测试报告中文的乱码。

最后:下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值