Pytest框架集成Allure定製測試報告詳解(一)

Allure簡介
Allure是一款非常輕量級並且非常靈活的開源測試報告生成框架。 它支持絕大多數測試框架, 例如TestNG、Pytest、JUint等。它簡單易用,易於集成。下面就Pytest如何與Allure集成做詳細介紹。

Pytest框架集成Allure
Pytest是Python的單元測試框架,非常方便和易用。強烈推薦對於用Python進行測試工作的小夥伴使用這個測試框架,相比與Python自帶的UnitTest好用太多太多。今天我們主要是介紹如何將測試報告生成工具Allure集成到Pytest中。目前現在已經有allure2了,我們要使用的就是這個allure2

一、Features、Story定製詳解

@allure.feature # 用於定義被測試的功能,被測產品的需求點,模塊
@allure.story # 用於定義被測功能的用戶場景,即子功能點,用例


import pytest,os
import allure
class Test(object):
    @allure.feature('登錄功能')
    @allure.story('登錄成功')
    def test_login(self):
        assert 1 == 1

    def test2(self):
        assert 1==1
if __name__=="__main__":
    #生成測試報告json
    pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
    #將測試報告轉爲html格式
    split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
    os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
    os.system(split)
    print(split)


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
..
2 passed in 0.06s
Report successfully generated to .\report\html
allure generate ./report/result -o ./report/html --clean

Process finished with exit code 0

二、title用例標題和description用例描述定製詳解

@allure.title(用例的標題)

@allure.description(用例的描述)

或用例描述也可寫成這樣

"""
這裏是登錄成功測試用例
"""
import pytest,os
import allure
class Test(object):
    @allure.feature('登錄功能')
    @allure.story('登錄成功')
    @allure.title('用例的標題')#用例的標題
    @allure.severity('blocker')
    @allure.issue('https://www.baidu.com/')#添加權限對應鏈接
    @allure.testcase('https://www.baidu.com/')#添加用例對應鏈接
    def test_login(self):
        """
        這裏是登錄成功測試用例
        :return:
        """
        assert 1 == 1

    @allure.severity('critical')
    def test_01(self):
        assert 1==1

    @allure.severity('normal')
    def test_02(self):
        assert 1==1

    @allure.severity('minor')
    def test_03(self):
        assert 1==1

    @allure.severity('trivial')
    def test_04(self):
        assert 1==1

if __name__=="__main__":
    #生成測試報告json
    pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
    #將測試報告轉爲html格式
    split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
    os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
    os.system(split)
    print(split)


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
.....
5 passed in 0.09s
Report successfully generated to .\report\html
allure generate ./report/result -o ./report/html --clean

Process finished with exit code 0

三、Severity定製標記用例級別詳解

根據測試用例的重要性劃分測試用例等級,如果沒指定等級,默認爲normal級別

Allure中對嚴重級別的定義:
1、 Blocker級別:中斷缺陷(客戶端程序無響應,無法執行下一步操作)

@allure.severity('blocker')

2、 Critical級別:臨界缺陷( 功能點缺失)

@allure.severity('critical')

3、 Normal級別:普通缺陷(數值計算錯誤)

@allure.severity('normal')

4、 Minor級別:次要缺陷(界面錯誤與UI需求不符)

@allure.severity('minor')

5、 Trivial級別:輕微缺陷(必輸項無提示,或者提示不規範)

@allure.severity('trivial')

import pytest,os
import allure
class Test(object):
    @allure.feature('登錄功能')
    @allure.story('登錄成功')
    @allure.severity('blocker')
    def test_login(self):
        """
        這裏是登錄成功測試用例
        :return:
        """
        assert 1 == 1

    @allure.severity('critical')
    def test_01(self):
        assert 1==1

    @allure.severity('normal')
    def test_02(self):
        assert 1==1

    @allure.severity('minor')
    def test_03(self):
        assert 1==1

    @allure.severity('trivial')
    def test_04(self):
        assert 1==1

if __name__=="__main__":
    #生成測試報告json
    pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
    #將測試報告轉爲html格式
    split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
    os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
    os.system(split)
    print(split)

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
.....
5 passed in 0.10s
Report successfully generated to .\report\html
allure generate ./report/result -o ./report/html --clean

Process finished with exit code 0

四、Step和attach定製詳解

allure.step("調用登錄"): # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中

allure.attach('賬號', '18221124104') # attach可以打印一些附加信息

import pytest,os
import allure


@allure.feature('購物車功能')  # feature定義功能
class Test(object):
    @allure.story('加入購物車')  # story定義用戶場景
    def test_add_shopping_trolley(self):
        login('橙子', '登錄密碼')  # 調用“步驟函數”
        with allure.step("瀏覽商品"):  # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟2
            allure.attach('商品1', 'NIKE球鞋')  # attach可以打印一些附加信息
            allure.attach('商品2', '大衆速騰')
        with allure.step("點擊商品"):  # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟3
            pass
        with allure.step("校驗結果"):
            allure.attach('期望結果', '添加購物車成功')
            allure.attach('實際結果', '添加購物車失敗')
            assert 'success' == 'failed'

    @allure.story('修改購物車')
    def test_edit_shopping_trolley(self):
        pass

    @pytest.mark.skipif(reason='本次不執行')
    @allure.story('刪除購物車')
    def test_delete_shopping_trolley(self):
        pass


@allure.step('賬號登錄')  # 還可以將一個函數作爲一個步驟,調用此函數時,報告中輸出一個步驟,步驟名字通常是函數名,我把這樣的函數叫“步驟函數”
def login(user, pwd):
    print(user, pwd)
if __name__=="__main__":
    #生成測試報告json
    pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
    #將測試報告轉爲html格式
    split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
    os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
    os.system(split)
    print(split)

"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
橙子 登錄密碼
F.s
================================== FAILURES ===================================
_______________________ Test.test_add_shopping_trolley ________________________

self = <test.test01.Test object at 0x0000024F3425C898>

    @allure.story('加入購物車')  # story定義用戶場景
    def test_add_shopping_trolley(self):
        login('橙子', '登錄密碼')  # 調用“步驟函數”
        with allure.step("瀏覽商品"):  # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟2
            allure.attach('商品1', 'NIKE球鞋')  # attach可以打印一些附加信息
            allure.attach('商品2', '大衆速騰')
        with allure.step("點擊商品"):  # 將一個測試用例分成幾個步驟,將步驟打印到測試報告中,步驟3
            pass
        with allure.step("校驗結果"):
            allure.attach('期望結果', '添加購物車成功')
            allure.attach('實際結果', '添加購物車失敗')
>           assert 'success' == 'failed'
E           AssertionError

test01.py:51: AssertionError
1 failed, 1 passed, 1 skipped in 0.18s
Report successfully generated to .\report\html
allure generate ./report/result -o ./report/html --clean

Process finished with exit code 0

五、Issue缺陷鏈接和TestCase用例鏈接定製詳解

@allure.issue()   缺陷     對應缺陷管理系統裏面的鏈接,在測試報告中可以點擊跳轉的

@allure.testcase()  測試用例的鏈接地址    對應功能測試用例系統裏面的case鏈接,在測試報告中可以點擊跳轉的

import pytest,os
import allure
class Test(object):
    @allure.feature('登錄功能')
    @allure.story('登錄成功')
    @allure.severity('blocker')
    @allure.issue('https://www.baidu.com/')#添加缺陷對應鏈接
    @allure.testcase('https://www.baidu.com/')#添加用例對應鏈接
    def test_login(self):
        """
        這裏是登錄成功測試用例
        :return:
        """
        assert 1 == 1

    @allure.severity('critical')
    def test_01(self):
        assert 1==1

    @allure.severity('normal')
    def test_02(self):
        assert 1==1

    @allure.severity('minor')
    def test_03(self):
        assert 1==1

    @allure.severity('trivial')
    def test_04(self):
        assert 1==1

if __name__=="__main__":
    #生成測試報告json
    pytest.main(["-s", "-q", '--alluredir', 'report/result', 'test01.py'])
    #將測試報告轉爲html格式
    split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'
    os.system('cd C:/Users/wangli/PycharmProjects/Test/test/report')
    os.system(split)
    print(split)


"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
.....
5 passed in 0.05s
Report successfully generated to .\report\html
allure generate ./report/result -o ./report/html --clean

Process finished with exit code 0

五、link鏈接定製詳解

@allure.link(‘https://www.baidu.com/’)

六、attachment附件制定

@allure.attachment()

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章