08.25单元测试之pytest

前提:需要安装pytest和pytest-htm

pip install pytest 和pip install pytest-html

1.命名规则

pytest单元测试中的类名和方法名必须是以test开头的,执行中只能找到test开头的类方法,比unittest更加严谨

2.pytest生成自带的html测试报告

案例一

pytest.mian([“–html=./report.html”,”模块.py”])

08.25单元测试之pytest

案例二

运行指定模块指定类指定用例.冒号分割,并生成测试报告

pytest.main([“–html=./report.html”,”模块.py::类::test_a_001″])

案例三

直接执行pytest.main()自动查找当前目录下,以test开头的文件或者以test结尾的py文件

pytest.mian([“–html=./report.html”])

pytest调用语句

pytest . main ([ ‘-x’ , ‘–html=./report.html’ , ‘t12est000.py’ ])

-x 出现一条测试用例失败就退出测试
-v 丰富信息模式,输出更详细的用例执行信息
-s 显示print内容
-q 简化结果信息,不会显示每个用例的文件名

跳过

使用@pytest.mark.skip()跳过该用例

import pytest
@pytest.mark.skip()
def test001(self):
    assert 2==2

3.python的运行方式

. 点好,表示用例通过
F 表示失败 Failure
E 表示用例中存在异常 Error

4.文件读取

4.1读取csv文件

import csv          #导入csv模块
class ReadCsv():
    def read_csv(self):
        item=[]     #定义一个空列表
        file=csv.reader(open("../data/shujucsv.csv","r"))  #得到csv的文件对象
        for i in file:
            item.append(i)  #将获得的数据添加到列表中
        return item
r=ReadCsv()
print(r.read_csv())

4.2读取xml文件

from xml.dom import minidom
class ReadXml():
    def read_xml(self,first,second):
        file=minidom.parse("../data/shujuxml.xml")
        firstnode=file.getElementsByTagName(first)[0]
        secondnode=firstnode.getElementsByTagName(second)[0].firstChild.data
        return secondnode

5.allure

Allure 是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如 TestNG 、 Pytest、 JUint 等。它简单易用,易于集成。

首先配置allure的环境变量

08.25单元测试之pytest

验证allure是否配置成功

08.25单元测试之pytest

ps:若出现指令找不到则没有配置成功!!!!!!!!!!!!!!

其次要安装allure

pip install allure-pytest

5.1:allure常用的几个特性

@allure.feature 用于描述被测试产品需求
@allure.story 用于描述feature的用户场景,即测试需求
with allure.step(): 用于描述测试步骤,将会输出到报告中
allure.attach 用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图

案例

实现用户登录功能,场景为登录成功和登录失败

import pytest,allure,os
class TestClass():
    @allure.feature("登录功能")
    @allure.story("登陆成功")
    def test_success(self):
        assert 1==1
    @allure.feature("登录功能")
    @allure.story("登陆失败")
    def test_fail(self):
        assert 1==2
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'test_allure.py'])  # 生成json类型的测试报告
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'  # 将测试报告转为html格式
    os.system(split)  # system函数可以将字符串转化成命令在服务器上运行

pytest和allure效果展示

import pytest,allure,os
class TestShop():
    @allure.feature("购物车")
    @allure.story("产品展示")
    def testshow(self):
        with allure.step("查看吉利系列车信息"):
            allure.attach("博越","吉利")
        with allure.step("查看哈佛系列车信息"):
            allure.attach("H7","哈佛")
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'testshow.py'])
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)

案例:

import pytest,allure,os
class TestShop():
    @allure.feature("购物车")
    @allure.story("产品展示")
    def testshow(self):
        with allure.step("查看吉利系列车信息"):
            allure.attach("博越","吉利")
        with allure.step("查看哈佛系列车信息"):
            allure.attach("H7","哈佛")
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'testshow.py'])
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)

Pytest allure 效果展示

08.25单元测试之pytest

( ̄▽ ̄)~*—— ٩(๑❛ᴗ❛๑)۶谢谢阅读!!!!!!!!!!!!!

Original: https://blog.csdn.net/qq_57411925/article/details/126527281
Author: qq_57411925
Title: 08.25单元测试之pytest

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/774973/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球