2.pytest 命令引數

Maple發表於2020-12-24

失敗後停止

使用下面的引數可以讓測試在第1(N)次測試失敗後停止:

pytest x # 第一次測試失敗後停止測試 
pytest ‐‐maxfail=2 # 2次測試失敗後停止測試

修改檔案如下

# filename:test_02.py
import pytest

class TestDemo02:
def func(self, x):
return x + 1

# 修改成斷言失敗
def test_01(self):
assert self.func(3) == 14

# 修改成斷言失敗
def test_02(self):
assert self.func(3) == 5


if __name__ == '__main__':
pytest.main(['-s', '-x', 'test_02.py'])

執行後觀察結果

test_02.py F

================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________

...此處省略報錯具體原因

test_02.py:10: AssertionError
=========================== short test summary info ===========================
FAILED test_02.py::TestDemo02::test_01 - assert 4 == 14
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 1 failed in 0.09s ==============================

Process finished with exit code 0

可以看出,由於第一條報錯,2條用例只執行了第1條,使用‐‐maxfail=2可以失敗兩次後停止,大家自己嘗試下。

指定執行範圍

通過test_01.py檔案執行測試

pytest test_01.py

通過資料夾執行測試

pytest testcase

建立testcase目錄,然後把test_02.py移入目錄下,就像這樣

然後執行命令,可以發現只執行了test_02.py下的用例

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 3 items
testcase\test_02.py FF
================================== FAILURES ===================================
_____________________________ TestDemo02.test_01 ______________________________
...此處省略報錯具體原因
testcase\test_02.py:11: AssertionError
_____________________________ TestDemo02.test_02 ______________________________
...此處省略報錯具體原因
testcase\test_02.py:15: AssertionError
=========================== short test summary info ===========================
FAILED testcase/test_02.py::TestDemo02::test_01 - assert 4 == 14
FAILED testcase/test_02.py::TestDemo02::test_02 - assert 4 == 5
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
============================== 2 failed in 0.04s ==============================

通過關鍵字表示式來進行測試

這種方式會執行檔名,類名以及函式名與給定的字串表示式相匹配的測試用例。

pytest k "Demo02 and not 01"

上面的用例會執 行TestDemo02.test_02但是不會執行TestDemo02.test_01,就像這樣,另外3個沒執行

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 5 items / 3 deselected / 2 selected

testcase\test_02.py ..

======================= 2 passed, 3 deselected in 0.03s =======================

通過節點id來進行測試

引數化的類名、函式名和引數,用::分隔。

可以通過下面的方式執行模組中的指定的測試用例

pytest testcase/test_02.py::TestDemo02:test_01

可以從結果看出,執行了test_02.pyTestDemo02類下的test_01方法

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\auto-pytest
collected 1 item
testcase\test_02.py .
============================== 1 passed in 0.01s ==============================

通過標記來執行

pytest m tag01

這種方式會執行所有通過裝飾器 @pytest.mark.tag進行裝飾的測試用例,所以我們來修改test_02.py檔案

# filename:test_02.py
import pytest

class TestDemo02():
def func(self, x):
return x + 1

@pytest.mark.tag01
def test_01(self):
assert self.func(3) == 4

def test_02(self):
assert self.func(3) == 4

def test_03(self):
assert self.func(3) == 4

修改後,執行得到結果,可以看出只執行了打上標記的用例,篇幅問題,結果就不貼上來了

也可以通過pytest -m "tag01 or tag02"來執行標記為tag01和tag02的用例

相關文章