1. 程式人生 > 其它 >『德不孤』Pytest框架 — 2、Pytest的基本使用

『德不孤』Pytest框架 — 2、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外掛列表網址: https://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 runnerunittests變為pytestapply應用一下。

步驟2:

在PyCharm的Edit configurations...中配置以pytest方式執行測試用例。

點選PyCharm右上角的Edit configurations...

Edit configurations...中點選左上角的+號標誌,新增Python tests —> pytest

步驟3:

然後選擇target執行的測試檔案,可以選擇module(檔名),比如test_01.py,也可以選擇檔案路徑scripts path

設定完成後點選apply應用。

最後:

我們在執行測試用例後,檢視結果面板如下:

會發現比之前檢視測試結果好用、美觀多了。