当前位置 博文首页 > python单元测试之pytest的使用

    python单元测试之pytest的使用

    作者:yun678 时间:2021-08-10 18:35

    目录
    • 一、前提准备
    • 二、pytest生成自带的html测试报告
    • 三、pytest运行方式
    • 四、allure  

    一、前提准备

    1、前提:需要安装pytest和pytest-html(生成html测试报告)

    pip install pytest 和 pip install pytest-html 

    安装插件:pip install 插件名

    2、命名规范

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

    Pytest: setup, setup_class 和 teardown, teardown_class 函数 ( 和 unittest 执行效果一样 ) 运行于测试方法的始末,即 : 运行一次测试函数会运行一次 setup 和 teardown 运行于测试方法的始末 , 但是不管有多少测试函数都只执行一次 setup_class 和 teardown_class

    二、pytest生成自带的html测试报告

    前提条件:需要下载pytest-html模块(python自带的生成测试报告模块)

    pip install pytest-html

    如果不安装pytest-html会报:

    案例: 1)

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

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

    import pytest
    class Test():
        def test1(self):
            print("这是测试1")
        def test1(self):
            print("这是测试2")
    if __name__ == '__main__':
        pytest.main(["--html=./report.html", "test_004.py"])
    

    结果:

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

    pytest.main([‘--html=./report.html',‘模块.py::类::test_a_001'])

    import pytest
    class Test():
        def test1(self):
            print("这是测试1")
        def test2(self):
            print("这是测试2")
    if __name__ == '__main__':
        pytest.main(["--html=./report.html", "test_004.py::Test::test1"])
    

    结果:

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

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

    语句: pytst.main(['-x','--html=./report.html','t12est000.py'])

    -x出现一条测试用例失败就退出测试
    -s:显示print内容

    三、pytest运行方式

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

    四、allure  

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

    1、Allure常用的几个特性

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

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

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

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

    案例1:关于pytest与Allure生成html测试用例 rr.csv

    2,3,5
    5,6,11

    readCsv

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

    开发代码:

    class Cale():
        def jia(self,a,b):
            c=a+b
            return c
        def jian(self,a,b):
            c=a-b
            return c
        def cheng(self,a,b):
            c=a*b
            return c
        def chu(self,a,b):
            c=a/b
            return c
    

    生成html代码:

    import pytest
    from pytest01.readDemo.readCsv import ReadCsv
    from pytest01.demo.cale import Cale
    import os
    import allure
    r=ReadCsv()
    cc=r.read_csv()
    d=Cale()
    class Test():
        @allure.story("加法函数测试正确")
        def test001(self):
            for i in cc:
                dd=d.jia(int(i[0]),int(i[1]))
                assert dd==int(i[2])
    if __name__ == '__main__':
        pytest.main(['--alluredir', 'report/result', 'test_02.py'])
        split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
        os.system(split)
    

    jsjbwy
    下一篇:没有了