為什么選擇Python?Python是當前最流行的編程語言之一。它為Web后端,數(shù)據(jù)科學筆記本,sysadmin腳本等提供支持。它的語法簡潔,易讀且優(yōu)雅–非常適合初學者和專家。您可以想象的一切都只是一個導入。自然地,Python還是測試自動化的最好的語言。它的簡潔性使測試人員可以將更多的精力放在測試上,而不必在代碼上。未完成大量編程工作的測試人員往往比其他語言(如Java或C#)學習Python的速度更快。Python非常適合啟動測試! 什么是pytest?任何功能測試自動化項目的核心都是“核心”測試框架。該框架處理測試用例結(jié)構(gòu),測試執(zhí)行以及通過/失敗結(jié)果報告。這是可以添加額外的程序包和代碼(例如Selenium WebDriver)的基礎。 pytest是Python最好的測試框架之一。它簡單,可擴展且具有Python風格。測試用例是作為函數(shù)而不是類編寫的。測試斷言失敗將與實際值一起報告。插件可以添加代碼覆蓋率,漂亮的報告和并行執(zhí)行。pytest也可以與Django和Flask等其他框架集成。根據(jù)2018年P(guān)ython開發(fā)人員調(diào)查,它也是最受歡迎的Python測試框架。 入門讓我們創(chuàng)建我們的Python測試項目!如果您尚未這樣做,請下載并在您的計算機上安裝Python 3。然后,為項目創(chuàng)建一個新目錄: mkdir python-webui-testing cd python-webui-testing 每當我創(chuàng)建一個新的Python項目時,都會為其依賴項創(chuàng)建一個虛擬環(huán)境。這樣,同一臺計算機上的項目就不會有相互沖突的軟件包版本。我使用pipenv 是因為它簡化了工作流程。要全局安裝pipenv,請運行: pip install pipenv 然后,為新項目安裝pytest: $ pipenv install pytest --dev Pipenv將向您的項目添加兩個新文件: 第一次測試按照慣例,大多數(shù)項目會將所有測試放在一個 目錄下。讓我們遵循以下約定: mkdir tests cd tests 創(chuàng)建一個 為我們的第一個測試命名的Python模塊,并添加以下代碼: def test_addition(): assert 1 + 1 == 2 使用pytest編寫的測試通常不需要太多代碼。這兩行是功能齊全的測試用例!測試用例是作為函數(shù)而不是類編寫的。像這樣的基本測試不需要導入。使用Python的本機 運行測試讓我們運行我們的新測試。將目錄更改回項目根目錄,并調(diào)用pytest模塊: $ cd .. $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 1 item tests/test_math.py . [100%] =========================== 1 passed in 0.02 seconds =========================== 我們的第一個測試通過了! pytest是如何發(fā)現(xiàn)我們的測試的?按名稱:pytest將搜索名為 的模塊中命名的 測試函數(shù) 。有趣的是,pytest不需要任何測試目錄中的文件。 測試失敗如果測試失敗,會發(fā)生什么?讓我們添加另一個帶有錯誤的測試來找出: def test_subtraction(): diff = 1 - 1 assert diff == 0 讓我們重新運行這些測試: $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 2 items tests/test_math.py .. [100%] =========================== 2 passed in 0.02 seconds =========================== 我們回到了正軌。 參數(shù)化測試如果我們要使用多個輸入組合來運行相同的測試過程,該怎么辦?pytest有一個裝飾器!讓我們編寫一個新的參數(shù)化輸入乘法測試: import pytest
"a,b,expected", [(0, 5, 0), (1, 5, 5), (2, 5, 10), (-3, 5, -15), (-4, -5, 20)]) def test_multiplication(a, b, expected): assert a * b == expected 這次, $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 7 items tests/test_math.py ....... [100%] =========================== 7 passed in 0.03 seconds =========================== 參數(shù)是進行數(shù)據(jù)驅(qū)動測試的好方法。 驗證異常pytest將未處理的異常視為測試失敗。實際上,該 def test_divide_by_zero(): with pytest.raises(ZeroDivisionError): 1 / 0 重新運行測試以確保一切正常: $ pipenv run python -m pytest ============================= test session starts ============================== platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.12.0 rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing collected 8 items tests/test_math.py ........ [100%] =========================== 8 passed in 0.04 seconds =========================== ![]() ![]() |
|
來自: 軟件測試test > 《python自動化》