『德不孤』Pytest框架 — 2、Pytest的基本使用
- 2022 年 1 月 20 日
- 筆記
- 測試基礎技能 - Pytest測試框架
1、Pytest安裝
CMD命令窗口執行Pytest測試框架安裝命令:pip install -U pytest
(意思是如果沒有安裝,就進行安裝,如果已安裝就升級到最新版)
在CMD中檢查Pytest測試框架是否安裝成功:pytest –-version
C:\Users\L>pytest --version
pytest 6.1.2
2、Pytest常用插件
Pytest有很多非常強大的第三方插件,並且這些插件能夠實現很多實用的操作,並且還可以自定義Pytest插件。
比較好用的例如:
pytest-selenium
:集成Selenium
。pytest-html
:生成html
格式的自動化測試報告。pytest-rerunfailures
:失敗case
重複執行。pytest-xdist
:測試用例分散式執行,也可以說是多CPU分發。pytest-ordering
:用於改變測試用例的執行順序。allure-pytest
:用於生成美觀的測試報告。
Pytest插件列表網址: //plugincompat.herokuapp.com,裡面包含很多插件包,大家可依據工作的需求選擇使用。
說明:我常用安裝Python工具包的方式。
一個Python項目中可能會安裝很多安裝包,再次創建虛擬環境(新項目)是需要重新安裝的,這很麻煩也費時間,或者項目部署的時候避免重裝。
可以將現有項目的所有安裝包記錄在requirements.txt
文件,再另一個環境下一鍵安裝所有安裝包。
requirements.txt
文件,用於記錄所有依賴包及其精確的版本號,以便在新環境中進行部署操作。
- 使用以下命令將當前虛擬環境中的依賴包以版本號生成至文件中:
pip freeze > requirements.txt
- 當需要創建這個虛擬環境的完全副本,可以創建一個新的虛擬環境,並在其上運行以下命令:
pip install -r requirements.txt
如上所說:
我們可以把所有插件的模組名都寫入一個.txt
文件夾中。
pytest-html
pytest-rerunfailures
pytest-xdist
pytest-ordering
allure-pytest
然後在CMD命令行中執行pip install -r requirements.txt
命令即可。(全局安裝)
也可以在PyCharm中的命令行執行,但只針對於該項目。
提示:可以安裝指定版本的模組
通過使用
==
、>=
、<=
、> <
來指定版本,不寫則安裝最新版。例如:
pip install pytest-xdist==2.2.0
將安裝
pytest-xdist
模組2.2.0版本。
3、Pytest運行的第一個例子
我們直接在Pycharm例編寫程式碼就可以。
"""
1.學習目標
掌握pytest編寫測試用例的基本方法
2.操作步驟
2.1 導入pytest
2.2 直接編寫測試用例
默認必須以test開頭,你也可以到Pytest中修改測試例文件開頭。
2.3 執行用例
pytest.main("-s 文件名")
3.python assert斷言
assert 條件,"異常資訊"
3.需求
"""
# 1.導入pytest
import pytest
# 2.編寫所需的測試用例
# 此類方法表示一個函數
def test_login():
print("登錄步驟")
# pytest所用的斷言是Python自身斷言assert關鍵字
assert "abcd" in "abcdefg"
def test_register():
print("註冊步驟")
assert False
# 3.setup和teardown
# def setup_function():
# print("打開瀏覽器/打開APP")
#
# def teardown_function():
# print("關閉瀏覽器/關閉APP")
# 3.執行測試用例
if __name__ == '__main__':
# 注意過格式,main參數是一個列表
pytest.main(["-s", "test_pytest_01.py"])
"""
執行結果:
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: J:\PyCharmWorkSpace\Pytest_d\pytest_demo
collected 2 items
test_pytest_01.py
登錄步驟.
註冊步驟F
(. 表示成功 ,F 表示失敗)
================================== FAILURES ===================================
________________________________ test_register ________________________________
(下面是失敗的具體說明)
def test_register():
print("註冊步驟")
> assert False
E assert False
test_pytest_01.py:28: AssertionError
=========================== short test summary info ===========================
FAILED test_pytest_01.py::test_register - assert False
========================= 1 failed, 1 passed in 0.09s =========================
Process finished with exit code 0
"""
4、Pytest框架的運行方式
(1)Pytest主函數模式
- 運行所有測試用例:
pytest.main()
也可以加上參數:pytest.main(['-vs'])
提示:注意是所用測試用例,包括不同文件上的測試用例,都會執行。
- 執行指定文件的測試用例:
pytest.main(['-s','-v','test_a.py'])
也可以pytest.main(['-vs','test_a.py'])
- 執行指定包下的所有測試用例:
pytest.main(['-vs','./interface_testcase'])
提示:main函數的參數是一個列表數據類型。
(2)命令行模式
- 運行所有測試用例:
pytest
- 執行指定文件的測試用例:
pytest 文件路徑/文件名
例如:pytest -vs ./test_a.py
- 執行指定包下的所有測試用例:
pytest 包路徑/包名
例如:pytest -vs ./interface_testcase
(3)通過node id
執行指定測試用例
nodeid
由包名+模組文件名、分隔符、類名、方法名、函數名、參數構成,之間使用::
進行分割,舉例如下:
- 運行模組中的指定用例類
pytest -vs ./interface_testcase/test_interface.py::test_01_func
或者
pytest.main(['-vs' ./interface_testcase/test_interface.py::test_01_func)
- 運行模組中的指定用例方法
pytest -vs test_interface.py::test_01_func::test_method
或者
pytest.main(['-vs' test_interface.py::test_01_func::test_method)
5、在PyCharm中以Pytest的方式運行測試用例
步驟1:
點擊File —> Settings —> Tools —> Python Integrated Tools —> Testing
將default test runner
由unittests
變為pytest
,apply
應用一下。
步驟2:
在PyCharm的Edit configurations...
中配置以pytest
方式運行測試用例。
點擊PyCharm右上角的Edit configurations...
,
在Edit configurations...
中點擊左上角的+
號標誌,添加Python tests —> pytest
步驟3:
然後選擇target
運行的測試文件,可以選擇module
(文件名),比如test_01.py
,也可以選擇文件路徑scripts path
。
設置完成後點擊apply
應用。
最後:
我們在執行測試用例後,查看結果面板如下:
會發現比之前查看測試結果好用、美觀多了。