本文節選自霍格沃玆測試學院內部教材,進階學習,文末加群!
簡介
pytest 是一個成熟的全功能 Python 測試工具,可以幫助您編寫更好的程序。它與 Python 自帶的 Unittest 測試框架類似,但
pytest 使用起來更簡潔和高效,並且兼容 unittest 框架。pytest 有以下實用特性:
-
pytest 能夠支持簡單的單元測試和復雜的功能測試;
-
pytest 本身支持單元測試;
-
可以結合 Requests 實現接口測試;
-
結合 Selenium、Appium 實現自動化功能測試;
-
使用 pytest 結合 Allure 集成到 Jenkins 中可以實現持續集成。工作中一般會使用持續集成來完成代碼集成到主干分支之后的回歸測試,通過自動化測試的手段來實現產品的快速迭代,同時還能保證產品的高質量。
-
pytest 支持 315 種以上的插件;
參考網站:
安裝
pip install -U pytest
查看版本
pytest --version
### 在 Pycharm 中運行 pytest 用例
打開 Pycharm -> 設置 -> Tools -> Python Integrated Tools -> Testing: pytest
首先次設置成 pytest ,需要安裝 pytest,可以直接按照這個頁面的提示點擊“fix”,也可以在 Project interpreter 里面添加 pytest 依賴包。安裝完 pytest 之后,編寫的符合規則的測試用例都能被識別出來並且標出一個綠色的執行按鈕,點擊這個按鈕也能執行某個方法或者某個類。例如:
Pycharm 設置運行方式為 pytest 之后,用例左側會顯示綠色按鈕,可以直接點擊這個按鈕來執行這條用例。
用例的識別與運行
用例編寫規范:
-
測試文件以
test_
開頭(以_test
結尾也可以) -
測試類以
Test
開頭,並且不能帶有 init 方法 -
測試函數以
test_
開頭 -
斷言使用基本的
assert
即可
創建一個 python 文件,命名以 test_
開頭(或者以 _test
結尾),創建測試方法以 test_
開頭,測試類需要以 Test
開頭。創建文件名為 test_add.py
文件,代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def add(x, y):
return x + y
def test_add():
assert add(1, 10) == 11
assert add(1, 1) == 2
assert add(1, 99) == 100
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
運行 test_add.py
文件,在命令行進入到這個文件所在的路徑,可以直接使用 pytest 命令運行,pytest
會找當前目錄以及遞查找子目錄下所有的 test_*.py
或 *_test.py
的文件,把其當作測試文件。在這些文件里,pytest
會收集符合編寫規范的函數,類以及方法,當作測試用例並且執行,執行如下:
$ pytest
....
test_add.py ..F [100%]
....
self = <test_cases.test_add.TestClass object at 0x1091810d0>
def test_two(self):
x = "hello"
> assert hasattr(x, "check")
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_add.py:18: AssertionError
===================================================== 1 failed, 2 passed in 0.05s
...
結果分析 :執行結果中,F
代表用例未通過(斷言錯誤),.
用例通過。如果有報錯會有詳細的錯誤信息。pytest 也支持 Unittest
模式的用例定義。
控制用例的執行順序
pytest 加載所有的測試用例是亂序的,如果想指定用例的順序,可以使用 pytest-order
插件,指定用例的執行順序只需要在測試用例的方法前面加上裝飾器 @pytest.mark.run(order=[num])
設置order的對應的num值,它就可以按照 num 的大小順序來執行。
應用場景:有時運行測試用例需要指定它的順序,比如有些場景需要先運行完登錄,才能執行后續的流程比如購物流程,下單流程,這時就需要指定測試用例的順序。通過
pytest-ordering
這個插件可以完成用例順序的指定。
安裝
pip install pytest-ordering
案例
創建一個測試文件“test_order.py”,代碼如下:
import pytest
class TestPytest(object):
@pytest.mark.run(order=-1)
def test_two(self):
print("test_two,測試用例")
@pytest.mark.run(order=3)
def test_one(self):
print("test_one,測試用例")
@pytest.mark.run(order=1)
def test_three(self):
print("\ntest_three,測試用例")
執行結果,如下查看執行順序:
省略...
plugins: html-2.0.1, rerunfailures-8.0, xdist-1.31.0, \
ordering-0.6, forked-1.1.3, allure-pytest-2.8.11, metadata-1.8.0
collecting ... collected 3 items
test_order.py::TestPytest::test_three
test_order.py::TestPytest::test_one
test_order.py::TestPytest::test_two
省略...
從上面的執行結果可以看出,執行時以 order 的順序執行:order=1,order=3,order=-1。
以上,更多進階內容,
來霍格沃茲測試開發學社,學習更多軟件測試與測試開發的進階技術,知識點涵蓋web自動化測試 app自動化測試、接口自動化測試、測試框架、性能測試、安全測試、持續集成/持續交付/DevOps,測試左移、測試右移、精准測試、測試平台開發、測試管理等內容,課程技術涵蓋bash、pytest、junit、selenium、appium、postman、requests、httprunner、jmeter、jenkins、docker、k8s、elk、sonarqube、jacoco、jvm-sandbox等相關技術,全面提升測試開發工程師的技術實力
QQ交流群:484590337
公眾號 TestingStudio
點擊獲取更多信息