在Debian上进行Python自动化测试,可按以下步骤操作:
更新系统并安装基础工具
sudo apt update && sudo apt install python3 python3-pip
创建虚拟环境(推荐)
python3 -m venv venv
source venv/bin/activate
安装测试框架和库
pip install pytest
requests
:HTTP接口测试selenium
:Web UI自动化faker
:生成测试数据编写测试用例
test_example.py
):def test_addition():
assert 1 + 1 == 2
test_example.py
):import unittest
class TestExample(unittest.TestCase):
def test_addition(self):
self.assertEqual(1 + 1, 2)
if __name__ == '__main__':
unittest.main()
运行测试
pytest
python -m unittest test_example.py
高级配置(可选)
pytest-xdist
,使用-n
参数指定进程数。pytest --html=report.html
生成HTML报告。说明:优先使用虚拟环境隔离依赖,根据项目需求选择框架(pytest更灵活,unittest为内置标准库),通过命令行直接运行测试即可。