1. 程式人生 > >手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告-02(非常詳細,非常實用)

手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告-02(非常詳細,非常實用)

簡介

俗話說“人靠衣服馬靠鞍”一個專案做的在好,沒有一分的漂亮的測試報告有時候也是很難在客戶那邊驗收的,今天巨集哥就帶你們解決這一難題。

前邊一篇文章是分享如何搭建pytest+Allure的環境,從而生成一份精美的、讓人耳目一新的測試報告,但是有的小夥伴或者童鞋們可能會問,我能不能按照自己的想法為我的專案測試結果量身打造一份屬於我自己的測試報告了,當然可以了。

Allure常用註解

Allure提供了以下常用註解(未列出部分請訪問官網瞭解),具體用法如下。

Feature: 標註主要功能模組
Story: 標註Features功能模組下的分支功能
Severity: 標註測試用例的重要級別

Step: 標註測試用例的重要步驟
Issue和TestCase: 標註Issue、Case,可加入URL

1、Features定製詳解

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-30
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest


@allure.feature('test_module_01')
def test_case_01():
    """
    用例描述:Test case 01
    """
    assert 0


@allure.feature('test_module_02')
def test_case_02():
    """
    用例描述:Test case 02
    """
    assert 0 == 0


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report'])

(3)新增feature,Report展示見下圖。

2、Story定製詳解

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-29
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest


@allure.feature('test_module_01')
@allure.story('test_story_01')
def test_case_01():
    """
    用例描述:Test case 01
    """
    assert 0


@allure.feature('test_module_02')
@allure.story('test_story_02')
def test_case_02():
    """
    用例描述:Test case 02
    """
    assert 0 == 0


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report'])

(3)新增story,Report展示見下圖。

3、用例標題和用例描述定製詳解

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-29
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest

@allure.feature('test_module_01')
@allure.story('test_story_01')
#test_case_01為用例title
def test_case_01():
    """
    用例描述:這是用例描述,Test case 01,描述本人
    """
    #註釋為用例描述
    assert 0



if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report'])

(3)新增用例標題和用例描述,Report展示見下圖。

4 、Severity定製詳解

Allure中對嚴重級別的定義:
1、 Blocker級別:中斷缺陷(客戶端程式無響應,無法執行下一步操作)
2、 Critical級別:臨界缺陷( 功能點缺失)
3、 Normal級別:普通缺陷(數值計算錯誤)
4、 Minor級別:次要缺陷(介面錯誤與UI需求不符)
5、 Trivial級別:輕微缺陷(必輸項無提示,或者提示不規範)

 

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-29
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest


@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('blocker')
def test_case_01():
    """
    用例描述:Test case 01
    """
    assert 0

@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('critical')
def test_case_02():
    """
    用例描述:Test case 02
    """
    assert 0 == 0

@allure.feature('test_module_01')
@allure.story('test_story_02')
@allure.severity('normal')
def test_case_03():
    """
    用例描述:Test case 03
    """
    assert 0

@allure.feature('test_module_01')
@allure.story('test_story_02')
@allure.severity('minor')
def test_case_04():
    """
    用例描述:Test case 04
    """
    assert 0 == 0


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report'])

(3)新增Severity,Report展示見下圖。

5、Step定製詳解

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-29
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest

@allure.step("字串相加:{0},{1}")     
# 測試步驟,可通過format機制自動獲取函式引數
def str_add(str1, str2):
    if not isinstance(str1, str):
        return "%s is not a string" % str1
    if not isinstance(str2, str):
        return "%s is not a string" % str2
    return str1 + str2

@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('blocker')
def test_case():
    str1 = 'hello'
    str2 = '巨集哥'
    assert str_add(str1, str2) == 'hello巨集哥'


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report'])

(3)新增Step,Report展示見下圖。 

6、Issue和TestCase定製詳解

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-29
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest


@allure.step("字串相加:{0},{1}")     # 測試步驟,可通過format機制自動獲取函式引數
def str_add(str1, str2):
    print('hello')
    if not isinstance(str1, str):
        return "%s is not a string" % str1
    if not isinstance(str2, str):
        return "%s is not a string" % str2
    return str1 + str2

@allure.feature('test_module_01')
@allure.story('test_story_01')
@allure.severity('blocker')
@allure.issue("http://www.baidu.com")
@allure.testcase("http://www.testlink.com")
def test_case():
    str1 = 'hello'
    str2 = '巨集哥'
    assert str_add(str1, str2) == 'hello巨集哥'


if __name__ == '__main__':
    pytest.main(['-s', '-q', '--alluredir', './report'])

(3)新增Issue和TestCase,Report展示見下圖。

7、attach定製詳解

(1)程式碼實現

(2)參考程式碼

# coding=utf-8
# 1.先設定編碼,utf-8可支援中英文,如上,一般放在第一行

# 2.註釋:包括記錄建立時間,建立人,專案名稱。
'''
Created on 2019-9-29
@author: 北京-巨集哥   QQ交流群:707699217
Project:手把手教你Pytest+Allure2.X定製報告詳細教程,給自己的專案量身打造一套測試報告
'''
# 3.匯入模組
import allure
import pytest

# file = open('C:\\Users\\DELL\\Desktop\\duhong\\test.png', 'rb').read()
# allure.attach('test_img', file, allure.attachment_type.PNG)PNG
@pytest.fixture
def attach_file_in_module_scope_fixture_with_finalizer(request):
    allure.attach('A text attacment in module scope fixture', 'blah blah blah', allure.attachment_type.TEXT)
    def finalizer_module_scope_fixture():
        allure.attach('A text attacment in module scope finalizer', 'blah blah blah blah',
                      allure.attachment_type.TEXT)
    request.addfinalizer(finalizer_module_scope_fixture)


def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_finalizer):
    pass


def test_multiple_attachments():
    allure.attach.file('C:\\Users\\DELL\\Desktop\\duhong\\test.png', attachment_type=allure.attachment_type.PNG)
    allure.attach('<head></head><body> a page </body>', 'Attach with HTML type', allure.attachment_type.HTML)

(3)新增attach引數,Report展示見下圖。

 

 

 

 

小結

此外,Allure還支援Jenkins Plugin,後面我會專門寫一篇博文給小夥伴們繼續介紹和分享,感興趣的話請關注我的部落格。

其實上面說的大家都可以到官網來學習allure的用法和使用。

好了,明天就是國慶節了,巨集哥這裡提前祝大家國慶節快樂,吃好玩好喝好。

您的肯定就是我進步的動力。如果你感覺還不錯,就請鼓勵一下吧!記得點波 推薦 哦!!!(點選右邊的小球即可!(^__^) 嘻嘻……)