在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為內置標準庫),通過命令行直接運行測試即可。