1. 程式人生 > 其它 >pytest---allure中增加用例詳情

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還是很強大的,可以將我們的報告設計的更加好看,對應測試用例模組的劃分也很好的展示出來,最最最主要的是領導能看懂了。