欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pytest+allure自动化测试and图表展示

程序员文章站 2024-03-21 23:36:34
...

一.安装依赖库:

1.pytest依赖库:

1.pip install pytest
2.pip install pytest-allure-adapto

2.allure依赖库:

1.pip install pytest-allure-adaptor
2.下载allure-commandline
链接: https://github.com/allure-framework/allure2/releases.
3.下载完把bin所在的文件夹路径加入环境变量

二.编写代码:

1.代码目录:

pytest+allure自动化测试and图表展示
其中test_005.py代码包含运行代码的主函数

2.运行代码

#test_005.py的代码
import pytest, os, allure, subprocess

class Test_Pytest():
    @allure.feature('test_module_01')# 用于定义被测试的功能,被测产品的需求点
    @allure.story('test_story_01')# 用于定义被测功能的用户场景,即子功能点
    def test_01(self):
        print("test_01方法执行")
        assert 1 == 1

    @allure.feature('test_module_02')
    @allure.story('test_story_02')
    def test_02(self):
        print("test_02方法执行")
        allure.attach('测试','测试01')
        assert "l" in "love"

    @allure.feature('test_module_03')
    @allure.story('test_story_03')
    def test_03(self):
        print("test_03方法执行")
        for i in range(5):
            assert i - 2 == -2

    @allure.feature('test_module_01')
    @allure.story('test_story_02')
    def test_04(self):
        print("test_04方法执行")

    @allure.feature('test_module_01')
    @allure.story('test_story_03')
    def test_05(self):
        print("test_05方法执行")
        assert 3+4 == 7

    @allure.feature('test_module_02')
    @allure.story('test_story_01')
    def test_06(self):
        print("test_06方法执行")
        assert 'i' in 'issue'

    @allure.feature('test_module_02')
    @allure.story('test_story_03')
    def test_07(self):
        print("test_07方法执行")
        raise IOError('asvcvv')
        assert 'a' in 'abc'

    @allure.feature('test_module_03')
    @allure.story('test_story_02')
    def test_08(self):
        print("test_08方法执行")
        assert 'i' in 'issue'

    @allure.feature('test_module_03')
    @allure.story('test_story_01')
    def test_09(self):
        print("test_09方法执行")
        assert 'i' in 'issue'


if __name__ == "__main__":
    try:
        pytest.main(['-s', '-q', '--alluredir', 'report'])#测试当前文件夹以test开头的py文件
    except:
        print('pytest运行失败')
    try:
        p = subprocess.Popen('allure serve report', stdout=subprocess.PIPE, shell=True)#调用cmd,以allure方式打开运行结果
        data = p.stdout.read()
    except:
        print('调用cmd运行allure失败')
    finally:
        p.kill()

三.运行效果:

1.显示结果页面:

pytest+allure自动化测试and图表展示

2.结果页面分析:

1.左下角可以切换显示语言,切换成中文方式;
2.左上角区域是所有测试用例运行结果,其中绿色代表‘通过测试’,红色代表‘测试失败’,黄色代表‘测试故障’(一般是该测试抛出错误,但是没被接收处理);
3.‘测试套’区域显示的是以被测py文件为单位的测试结果;
4.‘测试场景’区域显示的是以@allure.feature(‘test_module_01’)标记的测试用例为单位;
5.‘趋势’区域显示的是与以前的测试结果的对比,需要将以前运行的结果中的history放到report里面才会有趋势图(我这种运行方式不会产生history文件);
6.‘类别’区域显示的是测试用例运行异常的种类为单位的测试结果。