在Ubuntu下进行Python自动化测试,可按以下步骤操作:
sudo apt update && sudo apt install python3 python3-pip。pip install pytest。python3 -m venv venvsource venv/bin/activate。unittest或pytest编写测试脚本,例如:# test_sample.py(pytest示例)
def test_addition():
assert 1 + 1 == 2
```。
requests库模拟HTTP请求,例如:import requests
def test_api():
response = requests.get("https://api.example.com")
assert response.status_code == 200
```。
selenium库操作浏览器,例如:from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
assert "Example" in driver.title
driver.quit()
```。
pytest test_sample.py(支持自动发现test_开头的文件/函数)。pytest-html生成HTML报告:pip install pytest-htmlpytest --html=report.html。关键工具推荐:
pytest + requests。pytest + selenium。参考资料:[1,2,3,4,5,6,7,8,9,10,11]