Python+Selenium 八:selenium+pytest+allure生成測試報告


 

一、目錄結構

·report 生成html報告的目錄

·result 執行allure生成的源文件

·screenshot 是截圖后保存的目錄

·data.yaml 存放的參數化的數據

·test_data.py 真正的執行程序

 

二、代碼示例

data.yaml

1 - allure
2 - pytest
3 - selenium

test_data.py

 1 import allure
 2 from selenium import webdriver
 3 import pytest
 4 import yaml
 5 import time
 6 
 7 
 8 @allure.testcase('http://www.github.com')
 9 @allure.feature('百度搜索')
10 @pytest.mark.parametrize('data', yaml.safe_load(open("data.yaml")))
11 def test_steps_demo(data):
12     with allure.step("打開百度首頁"):
13         driver = webdriver.Chrome()
14         driver.get("http://www.baidu.com")
15         driver.maximize_window()
16 
17     with allure.step(f"輸入關鍵詞:{data}"):
18         driver.find_element_by_id('kw').send_keys(data)
19         time.sleep(2)
20         driver.find_element_by_id('su').click()
21         time.sleep(2)
22 
23     with allure.step("保存圖片"):
24         driver.save_screenshot("./screenshot/"+data+".png")
25         allure.attach.file("./screenshot/"+data+".png")
26         attachment_type = allure.attachment_type.PNG
27 
28     with allure.step("關閉瀏覽器"):
29         driver.quit()
 

三、執行命令

1、命令:

1  pytest test_data.py -s -q --alluredir=./result/

執行后可在result目錄中生成原始的allure源文件

2、生成html報告:

1  allure generate ./result -o ./report --clean

執行后可在report中生成可視化的html報告

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM