Python是當(dāng)前最流行的編程語(yǔ)言之一。它為Web后端,數(shù)據(jù)科學(xué)筆記本,sysadmin腳本等提供支持。它的語(yǔ)法簡(jiǎn)潔,易讀且優(yōu)雅–非常適合初學(xué)者和專(zhuān)家。您可以想象的一切都只是一個(gè)導(dǎo)入。自然地,Python還是測(cè)試自動(dòng)化的最好的語(yǔ)言。它的簡(jiǎn)潔性使測(cè)試人員可以將更多的精力放在測(cè)試上,而不必在代碼上。未完成大量編程工作的測(cè)試人員往往比其他語(yǔ)言(如Java或C#)學(xué)習(xí)Python的速度更快。Python非常適合啟動(dòng)測(cè)試!
任何功能測(cè)試自動(dòng)化項(xiàng)目的核心都是“核心”測(cè)試框架。該框架處理測(cè)試用例結(jié)構(gòu),測(cè)試執(zhí)行以及通過(guò)/失敗結(jié)果報(bào)告。這是可以添加額外的程序包和代碼(例如Selenium WebDriver)的基礎(chǔ)。
pytest是Python最好的測(cè)試框架之一。它簡(jiǎn)單,可擴(kuò)展且具有Python風(fēng)格。測(cè)試用例是作為函數(shù)而不是類(lèi)編寫(xiě)的。測(cè)試斷言失敗將與實(shí)際值一起報(bào)告。插件可以添加代碼覆蓋率,漂亮的報(bào)告和并行執(zhí)行。pytest也可以與Django和Flask等其他框架集成。根據(jù)2018年P(guān)ython開(kāi)發(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ì)為其依賴(lài)項(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è)新文件: Pipfile
和 。 Pipfile指定了項(xiàng)目的要求,而 Pipfile.lock “鎖定”了項(xiàng)目將使用的顯式版本。該命令中的“ –dev”選項(xiàng)表示pytest軟件包將僅用于開(kāi)發(fā),而不用于部署。Pipfile.lock
按照慣例,大多數(shù)項(xiàng)目會(huì)將所有測(cè)試放在一個(gè) 目錄下。讓我們遵循以下約定:tests/
mkdir tests
cd tests
創(chuàng)建一個(gè) 為我們的第一個(gè)測(cè)試命名的Python模塊,并添加以下代碼:test_math.py
def test_addition():
assert 1 + 1 == 2
使用pytest編寫(xiě)的測(cè)試通常不需要太多代碼。這兩行是功能齊全的測(cè)試用例!測(cè)試用例是作為函數(shù)而不是類(lèi)編寫(xiě)的。像這樣的基本測(cè)試不需要導(dǎo)入。使用Python的本機(jī) assert
語(yǔ)句代替自定義斷言調(diào)用。
讓我們運(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è)試通過(guò)了!
pytest是如何發(fā)現(xiàn)我們的測(cè)試的?按名稱(chēng):pytest將搜索名為 的模塊中命名的 測(cè)試函數(shù) 。有趣的是,pytest不需要任何測(cè)試目錄中的文件。test_*
test_*.py
__init__.py
如果測(cè)試失敗,會(huì)發(fā)生什么?讓我們添加另一個(gè)帶有錯(cuò)誤的測(cè)試來(lái)找出:
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 ===========================
我們回到了正軌。
如果我們要使用多個(gè)輸入組合來(lái)運(yùn)行相同的測(cè)試過(guò)程,該怎么辦?pytest有一個(gè)裝飾器!讓我們編寫(xiě)一個(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
這次,pytest
必須導(dǎo)入模塊。在 裝飾將取代的輸入元組的測(cè)試功能參數(shù),運(yùn)行每一次輸入元組中的測(cè)試功能。再次運(yùn)行測(cè)試將顯示更多的通過(guò)點(diǎn):@pytest.mark.parametrize
$ 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è)試的好方法。
pytest將未處理的異常視為測(cè)試失敗。實(shí)際上,該 assert
語(yǔ)句僅引發(fā)異常以注冊(cè)失敗。如果我們要驗(yàn)證是否正確引發(fā)了異常該怎么辦?使用 與期望的異常類(lèi)型,如下所示:pytest.raises
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 ===========================
聯(lián)系客服