1. 程式人生 > >Allure+pytest安裝和使用

Allure+pytest安裝和使用

windows下安裝 Allure工具
環境
1、安裝JDK1.8+
2、安裝Allure
下載Allure的zip安裝包,點選此處
解壓到allure-commandline目錄
進入bin目錄,執行allure.bat
新增allure到環境變數PATH(\安裝路徑\allure-commandline\bin)
新增環境變數的方法:
計算機–屬性–高階系統設定–環境變數–系統變數–path–編輯

在這裡插入圖片描述
然後新開cmd
在這裡插入圖片描述

安裝Allure Pytest Adaptor
Allure Pytest Adaptor是Pytest的一個外掛,通過它我們可以生成Allure所需要的用於生成測試報告的資料。安裝pytest-allure-adaptor外掛方法:

$ pip install pytest-allure-adaptor

test_allure.py

# coding:utf-8

import pytest
import allure


# 測試函式
@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.severity("critical")               # 優先順序,包含blocker, critical, normal, minor, trivial 幾個不同的等級
@allure.feature("測試模組_demo1")           # 功能塊,feature功能分塊時比story大,即同時存在feature和story時,feature為父節點
@allure.story("測試模組_demo2")             # 功能塊,具有相同feature或story的用例將規整到相同模組下,執行時可用於篩選
@allure.issue("BUG號:123")                 # 問題表識,關聯標識已有的問題,可為一個url連結地址
@allure.testcase("用例名:測試字串相等")      # 用例標識,關聯標識用例,可為一個url連結地址
@pytest.mark.parametrize("para_one, para_two",              # 用例引數
                         [("hello world", "hello world"),   # 用例引數的引數化資料
                          (4, 4),
                          ("中文", "中文")],
                         ids=["test ASCII string",          # 對應用例引數化資料的用例名
                              "test digital string",
                              "test unicode string"])
def test_case_example(para_one, para_two):
    """用例描述:測試字串相等
    :param para_one: 引數1
    :param para_two: 引數2
    """

    # 獲取引數
    paras = vars()
    # 報告中的環境引數,可用於必要環境引數的說明,相同的引數以後者為準
    allure.environment(host="172.6.12.27", test_vars=paras)
    # 關聯的資料資訊, 可在報告中記錄儲存必要的相關資訊
    allure.attach("用例引數", "{0}".format(paras))
    # 呼叫測試函式
    res = str_add(para_one, para_two)
    # 對必要的測試中間結果資料做備份
    allure.attach("str_add返回結果", "{0}".format(res))
    # 測試步驟,對必要的測試過程加以說明
    with pytest.allure.step("測試步驟2,結果校驗 {0} == {1}".format(res, para_one+para_two)):
        assert res == para_one+para_two, res


if __name__ == '__main__':
    # 執行,指定執行測試模組_demo1, 測試模組_demo2兩個模組,同時指定執行的用例優先順序為critical,blocker
    pytest.main(['--allure_stories=測試模組_demo1, 測試模組_demo2', '--allure_severities=critical, blocker'])

執行case
在這裡插入圖片描述
執行完成後會在指定目錄下生產xml報告
在這裡插入圖片描述

使用allure生產html視覺化報告

在這裡插入圖片描述