【软件测试学习笔记】pytest-allure生成测试报告

pytest-allure生成测试报告

安装模块:pip install allure-pytest

# 第一步:生成xml数据
pytest --alluredir=./report/xml testcase.py
# 第二步:生成html文件
allure generate --clean ./report/xml -o ./result/html

将截图加入到报告里

allure.attach(f, ‘图片名’, allure.attachment_type.JPG)

# -*- coding: utf-8 -*-
​
from selenium import webdriver
import allure
​
browser=webdriver.Chrome()
browser.get("https://www.baidu.com")
try:
    browser.find_element_by_id("zhiyi").send_keys('test123456')  # 输入密码,
except Exception as e:
    file_name = './test.jpg'
    browser.save_screenshot(file_name)  # 截图函数
    '''allure添加截图附件'''
    with open(file_name, mode='rb') as file:
        # 读取文件,将读取的结果作为参数传给allure
        f = file.read()  
    # 把图片添加到allure操作步骤里
    allure.attach(f, 'login', allure.attachment_type.JPG)  
    raise e

pytest中yield和return的区别和相同点

共同点
return和yield都可以返回值
区别
yield返回值后,后面的代码还会继续运行
return返回值后,后面的代码不会继续运行

# -*- coding: utf-8 -*-
​
import pytest
​
@pytest.fixture()
def openbrower():
    print("打开浏览器")
    yield "返回浏览器"
    print("关闭浏览器")
​
def test01(openbrower):
    print(openbrower)

运行结果

证明yield后面的代码仍执行了

testcase.py::test01 打开浏览器
# 返回值
返回浏览器
PASSED关闭浏览器

usefixtures与传fixture区别

fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。
当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以
@pytest.mark.usefixtures(“装饰器名”)

Pytest常用的插件

pytest-selenium   集成 selenium
pip install allure-pytest   生成漂亮的allure测试报告
pip install pytest-sugar   优化运行效果
pip install pytest-rerunfailures   执行用例失败后重新运行
pip install pytest-xdist   多线程并行与分布式执行
pip install pytest-assume   多条断言前面报错后面依然执行
pip install pytest-cover   测试覆盖率

一键安装多个模块

创建requirement.txt文件
selenium==3.0
requests
pip install -r requirement.txt

文章首发于微信公众号:程序员一凡

加油吧,测试人!路就在脚下,成功就在明天!

未来的你肯定会感谢现在拼命的自己!

给大家推荐一个软件测试技术交流群:1079636098 群友福利免费领取

愿你我相遇,皆有所获! 欢迎关注微信公众号:程序员一凡

1.免费领取一份216页软件测试工程师面试宝典文档资料。

2.软件测试学习路线以及相对应的视频学习教程免费分享!

猜你喜欢

转载自blog.csdn.net/qq_42434318/article/details/112847446
今日推荐