• 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"])

     

     案例二

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

    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()跳过该用例

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

    3.python的运行方式

    . 点好,表示用例通过

    F 表示失败 Failure

    E 表示用例中存在异常 Error

    4.文件读取

    4.1读取csv文件

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

    4.2读取xml文件

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

    5.allure

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

    首先配置allure的环境变量

     验证allure是否配置成功

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

    其次要安装allure
    pip install allure-pytest

    5.1:allure常用的几个特性

    @allure.feature  用于描述被测试产品需求

    @allure.story  用于描述feature的用户场景,即测试需求

    with allure.step():  用于描述测试步骤,将会输出到报告中

    allure.attach  用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图

    案例

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

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

    pytest和allure效果展示

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

     案例:

    1. import pytest,allure,os
    2. class TestShop():
    3. @allure.feature("购物车")
    4. @allure.story("产品展示")
    5. def testshow(self):
    6. with allure.step("查看吉利系列车信息"):
    7. allure.attach("博越","吉利")
    8. with allure.step("查看哈佛系列车信息"):
    9. allure.attach("H7","哈佛")
    10. if __name__ == '__main__':
    11. pytest.main(['--alluredir', 'report/result', 'testshow.py'])
    12. split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    13. os.system(split)
    Pytest allure 效果展示

     

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

  • 相关阅读:
    猿创征文|Redis的知识总结与项目应用
    深入理解操作系统之线程
    Open sesame! universal black box jailbreaking of large language models - 论文翻译
    面试题------>MySQL!!!
    设计模式学习笔记 - 设计原则 - 1.单一职责原则
    为什么要做数据可视化系统
    JAVA系列之类加载机制详解
    C 练习实例75-输入一个整数,并将其反转后输出
    y52.第三章 Kubernetes从入门到精通 -- k8s实战案例(二五)
    [笔记]Windows安全之《三》Shellcode
  • 原文地址:https://blog.csdn.net/qq_57411925/article/details/126527281