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