• 单元测试pytest


    一、安装

    可以在终端安装

    安装pytest:

     pip install pytest

    安装pytest-html(生成html测试报告):

    pip install pytest-html 

    二、命名规则

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

      unittest:

    Setup>>  setupclass , teardown >> teardownclass

    pytest:

     setup, setup_class和teardown, teardown_class函数(和unittest执行效果一样)

    三、Pytest生成自带的html测试报告

    3.1 下载pytest-html模块

    pip install pytest-html

    案例一:

    pytest.main("模块.py") 运行指定模块下,运行所有test开头的类和测试用例

     pytest.main(["--html=./report.html","模块.py"])

    案例二:

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

    pytest.main([‘--html=./report.html’]) 

    3.2 pytest调用语句

    1. pytst.main(['-x','--html=./report.html','test001.py'])
    2. -x出现一条测试用例失败就退出测试
    3. -s:显示print内容

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

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

    四、Pytest的运行方式

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

    五、文件读取

    5.1 读取csv文件

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

    5.2 读取xml文件

    1. from xml.dom import minidom
    2. class Readxml():
    3. def read_xml(self,filename,onename,twoname):
    4. root =minidom.parse(filename)
    5. firstnode =root.getElementsByTagName(onename)[0]
    6. secondnode=firstnode.getElementsByTagName(twoname)[0].firstChild.data
    7. return secondnode

    六、Allure的特性

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

    6.1 allure.feature

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

    6.2 allure.story

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

    代码案例:

    1. import pytest,allure,os
    2. class TestClass005():
    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_06.py']) #生成json类型的测试报告
    13. split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' #将测试报告转为html格式
    14. os.system(split) # system函数可以将字符串转化成命令在服务器上运行

    界面展示:

    6.3 with allure.step()

    用于描述测试步骤,将会输出到报告中

    6.4 allure.attach

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

    代码案例:

    1. import pytest,os,allure
    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', 'test_07.py'])
    12. split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    13. os.system(split)

  • 相关阅读:
    vue面试题
    Maven 打包方式探究
    c++ 学习 之 运算符重载
    【网页制作课作业】用HTML+CSS制作一个简单的学校网页(9页)
    MQ和分布式事务
    WebGL层次模型——单节点模型
    OpenCV Hough变换-直线检测
    图纸管理制度《四》
    应用层总结(未完待续 )
    【LeetCode】6. N 字形变换
  • 原文地址:https://blog.csdn.net/weixin_65219914/article/details/126531673