pytest---allure中增加用例详情

前言

  前面介绍了如何生成allure的报告,看着allure的页面非常好看,但是感觉少了一些内容,allure还可以增加一些用例详情内容,这样让我们的报告看着更加绚丽。

allure增加用例详情

我们可以在报告测试套件中增加用例详情内容。

用例标题

1、需要导入allure模块

2、在每条用例函数前需要加上 @allure.title('标题内容') 

3、正常执行生成allure报告。

import allure

class TestCase:

    @allure.title('用例1的名称')
    def test_01(self):
        print('---用例01---')
        assert 1

    @allure.title('用例2的名称')
    def test_02(self):
        print('---用例02---')
        assert 1

    @allure.title('用例3的名称')
    def test_03(self):
        print('---用例03---')
        assert 2

通过在cmd中输入 pytest --alluredir ./report/result 执行测试用例,在执行 allure serve report/result 打开allure报告。这样就能在报告中看出生成了三条用例,并将对应的用例名称显示出来了。

用例描述

用例除了用例标题显示出用例内容外,我们也可以通过用例描述更加详细的在allure中展示出来

这里和unittest的时候显示标题一样,直接通过python的语法在用例中增加注释

import allure

class TestCase:

    @allure.title('用例1的名称')
    def test_01(self):
        '''用例_01的描述内容'''
        print('---用例01---')
        assert 1

    @allure.title('用例2的名称')
    def test_02(self):
        '''用例_02的描述内容'''
        print('---用例02---')
        assert 1

    @allure.title('用例3的名称')
    def test_03(self):
        '''用例_03的描述内容'''
        print('---用例03---')
        assert 2

同样通过cmd命令行中输入对应的打开allure的报告内容。可以进入到用例详情页面中查看到,描述已经成功添加了。

 用例操作步骤

 allure中也可以添加将用例的操作步骤进行添加进去,这里通过 allure.step() 的方法来实现添加操作步骤

import allure

class TestCase:

    @allure.title('登录用户')
    def test_01(self):
        '''登录用户'''
        print('---用例01---')
        with allure.step('输入登录用户名'):
            print('输入用户名')
        with allure.step('输入登录的密码'):
            print('输入密码')
        with allure.step('点击登录'):
            print('点击登录!')
        assert 1

    @allure.title('进入测试页面')
    def test_02(self):
        '''进入测试页面'''
        print('---用例02---')
        with allure.step('进入测试页面'):
            print('进入测试页面')
        with allure.step('点击测试内容'):
            print('点击测试内容')
        assert 1

和上面的操作一样,打开cmd进行生成allure命令。通过在allure中进行查看报告内容。可以看到已经在测试步骤中添加上了。

定义测试用例相关链接

自动化测试用例都是通过功能用例转换过来的,我们也可以通过allure将我们的测试用例相关的链接到我们的自动化测试用例中,并通过allure展示出来,这里可以通过 @allure.issue() 进行添加bug缺陷内容, @allure.testcase() 添加测试用例链接

import allure

class TestCase:

    @allure.issue('https://home.cnblogs.com/u/qican/')
    @allure.testcase('https://www.baidu.com/')
    @allure.title('登录用户')
    def test_01(self):
        '''登录用户'''
        print('---用例01---')
        with allure.step('输入登录用户名'):
            print('输入用户名')
        with allure.step('输入登录的密码'):
            print('输入密码')
        with allure.step('点击登录'):
            print('点击登录!')
        assert 1

    @allure.issue('https://home.cnblogs.com/u/qican/')
    @allure.testcase('https://www.baidu.com/')
    @allure.title('进入测试页面')
    def test_02(self):
        '''进入测试页面'''
        print('---用例02---')
        with allure.step('进入测试页面'):
            print('进入测试页面')
        with allure.step('点击测试内容'):
            print('点击测试内容')
        assert 1

继续通过allure的报告执行方式,生成allure报告和打开allure报告,就可以看到我们的测试用例相关链接已经添加好了。

用例标签模块

功能测试中可以对测试用例根据不同的模块进行划分,自动化中也可以对用例进行不同模块的划分,然后通过allure的形式进行展示出来,这里我们可以通过 @allure.feature() 对其用例进行增加不同模块。也可以通过 @allure.epic 设置用例整体标签以及模块内容

import allure

@allure.epic("属于登录标签")
@allure.feature('登录模块')
class TestCase:
    @allure.title('登录用户')
    def test_01(self):
        '''登录用户'''
        print('---用例01---')
        with allure.step('输入登录用户名'):
            print('输入用户名')
        with allure.step('输入登录的密码'):
            print('输入密码')
        with allure.step('点击登录'):
            print('点击登录!')
        assert 1

    @allure.title('进入测试页面')
    def test_02(self):
        '''进入测试页面'''
        print('---用例02---')
        with allure.step('进入测试页面'):
            print('进入测试页面')
        with allure.step('点击测试内容'):
            print('点击测试内容')
        assert 1

@allure.epic("属于退出登录标签")
@allure.feature('退出登录模块')
class Test01:

    def test_01(self):
        print('---用例03---')

    def test_02(self):
        print('---用例04---')

同样通过cmd进行生成allure报告,然后通过查看allure报告内容,通过下图已经可以很清楚的看出来在增加了用例标签和用例模块

 

总结

通过上面安静简单的总结,allure还是很强大的,可以将我们的报告设计的更加好看,对应测试用例模块的划分也很好的展示出来,最最最主要的是领导能看懂了。

 

posted @ 2021-07-12 13:38  测试-安静  阅读(464)  评论(0编辑  收藏  举报