pytest框架_测试报告(allure-pytest)

先附上代码仓库:https://github.com/tengfei-jiao/gisui
(1)简介
allure:诱惑力
这翻译无敌了,超级美观的测试报告
Allure是一款非常轻量级并且非常灵活的开源测试报告生成框架, 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。

(3)电脑安装allure
pycharm安装的是allure-pytest
为啥要安装呢?因为python生成测试报告之后是一堆json数据,需要打开电脑命令行窗口,执行allure命令生成测试报告html格式。
如何安装?

https://blog.csdn.net/weixin_43523699/article/details/101780373?ops_request_misc=&request_id=&biz_id=102&utm_term=windows%E5%AE%89%E8%A3%85allure&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-5-101780373.first_rank_v2_pc_rank_v29

(2)allure的常用方法
@allure.feature(‘这里是1级特性’)
@allure.story(‘这里是2级特性’)
@allure.title(‘这里是用例标题1’)
@allure.description(‘这里是用例描述1’)
@allure.severity(‘blocker’) 这里是用例级别,默认normal,分类:
@allure.issue(‘添加缺陷对应链接:https://www.baidu.com/’)
@allure.testcase(‘测试用例链接,比如:https://www.baidu.com/’)
具体内容参考代码中的test_case_1、2:

# coding=utf-8
import pytest
import allure
import os

@pytest.fixture()
def test_case_3():
    print('---3号用例完成---')

@pytest.fixture()
def test_case_4():
    print('---4号用例完成---')

@pytest.fixture()
def test_case_5():
    print('---5号用例完成---')

@pytest.fixture()
def test_case_6():
    print('---6号用例完成---')

@pytest.fixture()
def test_case_7():
    print('---7号用例完成---')

@pytest.fixture()
def test_case_8():
    print('---8号用例完成---')

# (1)这里按照【从下到上的顺序】,执行优先级是3、4、5
@pytest.mark.usefixtures('test_case_5')
@pytest.mark.usefixtures('test_case_4')
@pytest.mark.usefixtures('test_case_3')
class Testlogin001:

    @allure.feature('这里是1级特性')
    @allure.story('这里是2级特性')
    @allure.title('这里是用例标题1')
    @allure.description('这里是用例描述1')
    @allure.severity('blocker')
    @allure.issue('https://www.baidu.com/') #添加缺陷链接
    @allure.testcase('https://www.baidu.com/') #测试用例链接
    # 被pytest.fixture()装饰的函数,函数名可以作为变量传递给测试用例,最终在执行测试用例之前执行这个装饰过的函数
    def test_case_1(self, test_case_8):
        print('---1号用例完成---')
        with allure.step('测试步骤:'):
            allure.attach('测试步骤1')
            allure.attach('测试步骤2')


    @allure.feature('这里是1级特性')
    @allure.story('这里是2级特性')
    @allure.title('这里是用例标题2')
    @allure.description('这里是用例描述2')
    @allure.severity('critical')
    # (2)这里按照调用了前面的函数test_case_6,局部的调用,执行优先级是最高的。
    @pytest.mark.usefixtures('test_case_7')
    @pytest.mark.usefixtures('test_case_6')
    def test_case_2(self):
        print('---2号用例完成---')
        allure.attach('用例的一些test body信息')

    # 单参数单值
    @pytest.mark.parametrize('arg', [1])
    def test_case_9(self, arg):
        print("传入的值为:{}".format(arg))
        assert arg == 1

    # 单参数多值
    @pytest.mark.parametrize('arg',['abc',1,{
    
    'a':1,'b':3},(4,5)])
    def test_case_10(self, arg):
        print(f"传入的值为:{arg}")

    # 多参数多值
    @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("5-2", 3), ("5*2", 10)])
    def test_case_11(self, test_input, expected):
        print(f"原值:{test_input} 期望值{expected}")
        assert eval(test_input) == expected

    @pytest.mark.xfail(condition=1<3, reason='该功能尚未完善,还在调测中')
    def test_case_12(self):
        print('---12号用例完成---')

    @pytest.mark.skipif(reason='test_case_13用例还在调测中')
    def test_case_13(self):
        print('---13号用例完成---')


@pytest.mark.skipif(1>3, reason='Testlogin2模块还在调测中')
class Testlogin2:

    def test_case_14(self):
        print('---14号用例完成---')


if __name__ == "__main__":

    # pytest.main(['test_1.py'])

    # 生成测试报告---json格式
    pytest.main(['--alluredir', 'D:/se_frame/Reports/allure_data', 'test_1.py'])
    # allure转换成---html并打开测试报告
    os.system('cd D:/se_frame/Reports/allure_data')
    # 清理上次的报告并生成新的报告
    os.system('allure generate D:/se_frame/Reports/allure_data -o D:/se_frame/Reports/html --clean')
    os.system('allure serve D:/se_frame/Reports/allure_data')



# 此处省略以上用例的执行结果
test_1.py::Testlogin001::test_case_12 
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---12号用例完成---
XPASS (该功能尚未完善,还在调测中)
退出浏览器

test_1.py::Testlogin001::test_case_13 SKIPPED (test_case_13用例还在...)
test_1.py::Testlogin2::test_case_14 
启动浏览器
---进入要执行模块的的界面---
---14号用例完成---
PASSED
退出浏览器


--------- generated html file: file://D:\se_frame\Reports\report.html ---------
================== 11 passed, 1 skipped, 1 xpassed in 0.52s ===================
Report successfully generated to D:\se_frame\Reports\html
Generating report to temp directory...
Report successfully generated to C:\Users\ADMINI~1\AppData\Local\Temp\2657140710621107730\allure-report
Starting web server...
2021-02-21 17:47:37.966:INFO::main: Logging initialized @8977ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.1.6:62127/>. Press <Ctrl+C> to exit


生成的测试报告:
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45451320/article/details/113916870