Selenium3+Pytest+Allure落地Python Web自動化測試
Selenium3+Pytest+Allure落地Python Web自動化測試
## Download: https://xmq1024.com/5334.html
# Selenium3+Pytest+Allure落地Python Web自動化測試
## 一、背景
Web自動化測試已經(jīng)成為了現(xiàn)代軟件開發(fā)中必不可少的一環(huán),而Selenium是一款廣泛應用于Web自動化測試的工具,其能夠模擬人類對Web頁面的交互操作,從而實現(xiàn)Web頁面自動化測試。
Selenium的Python版本Selenium3,相對于Selenium2.x來說,增加了很多新的特性,比如對Firefox瀏覽器的支持,同時還優(yōu)化了Chrome瀏覽器的支持。而另外一方面,Pytest是一個功能強大的Python測試框架,它支持多種類型的測試、斷言、fixture、參數(shù)化等。
當這兩個工具結合使用時,就可以快速、高效地實現(xiàn)Web自動化測試,同時Allure作為一款優(yōu)秀的測試報告框架,可以為我們提供美觀、易讀的測試報告。
## 二、準備工作
在進行Web自動化測試之前,需要安裝以下工具:
- Python3
- Pytest
- Selenium3
- WebDriver(Chrome、Firefox等瀏覽器對應的驅動程序)
- Allure
## 三、使用Selenium3+Pytest+Allure進行Web自動化測試
### 1. 編寫測試用例
首先需要編寫測試用例,以百度搜索為例:
```python
import pytest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class TestBaiduSearch():
def setup_class(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(5)
def teardown_class(self):
self.driver.quit()
def test_baidu_search(self):
self.driver.get("https://www.baidu.com")
self.driver.find_element_by_id("kw").send_keys("pytest")
self.driver.find_element_by_id("su").click()
assert "pytest" in self.driver.title
```
這里使用了Pytest框架,通過`setup_class`方法初始化瀏覽器驅動,`teardown_class`方法關閉瀏覽器驅動,`test_baidu_search`方法實現(xiàn)搜索操作并判斷結果是否正確。
### 2. 執(zhí)行測試用例
執(zhí)行測試用例時需要在命令行中執(zhí)行以下命令:
```bash
pytest --alluredir=./result
```
其中,`--alluredir`參數(shù)表示測試結果輸出路徑,這里將測試結果輸出到`./result`目錄下。
### 3. 生成測試報告
執(zhí)行測試完成后,需要使用Allure生成測試報告,執(zhí)行以下命令:
```bash
allure generate ./result -o ./report --clean
```
其中,`./result`表示測試結果目錄,`./report`表示測試報告輸出目錄。
### 4. 查看測試報告
測試報告生成完成后,可以在瀏覽器中查看測試報告,執(zhí)行以下命令:
```bash
allure open ./report
```
## 四、總結
本文介紹了如何使用Selenium3+Pytest+Allure進行Web自動化測試,通過結合使用這三款工具,可以快速、高效地實現(xiàn)Web自動化測試,并且生成美觀、易讀的測試報告。