1 # coding:utf-8 2 3 import pytest 4 import allure 5 6 7 # 測試函數 8 @allure.step("字符串相加:{0},{1}") # 測試步驟,可通過format機制自動獲取函數參數 9 def str_add(str1, str2): 10 print "hello" 11 if not isinstance(str1, str): 12 return "%s is not a string" % str1 13 if not isinstance(str2, str): 14 return "%s is not a string" % str2 15 return str1 + str2 16 17 18 @allure.severity("critical") # 優先級,包含blocker, critical, normal, minor, trivial 幾個不同的等級 19 @allure.feature("測試模塊_demo1") # 功能塊,feature功能分塊時比story大,即同時存在feature和story時,feature為父節點 20 @allure.story("測試模塊_demo2") # 功能塊,具有相同feature或story的用例將規整到相同模塊下,執行時可用於篩選 21 @allure.issue("BUG號:123") # 問題表識,關聯標識已有的問題,可為一個url鏈接地址 22 @allure.testcase("用例名:測試字符串相等") # 用例標識,關聯標識用例,可為一個url鏈接地址 23 @pytest.mark.parametrize("para_one, para_two", # 用例參數 24 [("hello world", "hello world"), # 用例參數的參數化數據 25 (4, 4), 26 ("中文", "中文")], 27 ids=["test ASCII string", # 對應用例參數化數據的用例名 28 "test digital string", 29 "test unicode string"]) 30 def test_case_example(para_one, para_two): 31 """用例描述:測試字符串相等 32 :param para_one: 參數1 33 :param para_two: 參數2 34 """ 35 36 # 獲取參數 37 paras = vars() 38 # 報告中的環境參數,可用於必要環境參數的說明,相同的參數以后者為准 39 allure.environment(host="172.6.12.27", test_vars=paras) 40 # 關聯的資料信息, 可在報告中記錄保存必要的相關信息 41 allure.attach("用例參數", "{0}".format(paras)) 42 # 調用測試函數 43 res = str_add(para_one, para_two) 44 # 對必要的測試中間結果數據做備份 45 allure.attach("str_add返回結果", "{0}".format(res)) 46 # 測試步驟,對必要的測試過程加以說明 47 with pytest.allure.step("測試步驟2,結果校驗 {0} == {1}".format(res, para_one+para_two)): 48 assert res == para_one+para_two, res 49 50 51 if __name__ == '__main__': 52 # 執行,指定執行測試模塊_demo1, 測試模塊_demo2兩個模塊,同時指定執行的用例優先級為critical,blocker 53 pytest.main(['--allure_stories=測試模塊_demo1, 測試模塊_demo2', '--allure_severities=critical, blocker'])