Selenium3+Pytest+Allure落地Python Web自动化测试
Selenium3+Pytest+Allure落地Python Web自动化测试
## Download: https://xmq1024.com/5334.html
# Selenium3+Pytest+Allure落地Python Web自动化测试
## 一、背景
Web自动化测试已经成为了现代软件开发中必不可少的一环,而Selenium是一款广泛应用于Web自动化测试的工具,其能够模拟人类对Web页面的交互操作,从而实现Web页面自动化测试。
Selenium的Python版本Selenium3,相对于Selenium2.x来说,增加了很多新的特性,比如对Firefox浏览器的支持,同时还优化了Chrome浏览器的支持。而另外一方面,Pytest是一个功能强大的Python测试框架,它支持多种类型的测试、断言、fixture、参数化等。
当这两个工具结合使用时,就可以快速、高效地实现Web自动化测试,同时Allure作为一款优秀的测试报告框架,可以为我们提供美观、易读的测试报告。
## 二、准备工作
在进行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`方法实现搜索操作并判断结果是否正确。
### 2. 执行测试用例
执行测试用例时需要在命令行中执行以下命令:
```bash
pytest --alluredir=./result
```
其中,`--alluredir`参数表示测试结果输出路径,这里将测试结果输出到`./result`目录下。
### 3. 生成测试报告
执行测试完成后,需要使用Allure生成测试报告,执行以下命令:
```bash
allure generate ./result -o ./report --clean
```
其中,`./result`表示测试结果目录,`./report`表示测试报告输出目录。
### 4. 查看测试报告
测试报告生成完成后,可以在浏览器中查看测试报告,执行以下命令:
```bash
allure open ./report
```
## 四、总结
本文介绍了如何使用Selenium3+Pytest+Allure进行Web自动化测试,通过结合使用这三款工具,可以快速、高效地实现Web自动化测试,并且生成美观、易读的测试报告。