前言
pytest作為更高級的測試框架垫释,在底層上兼容了unittest的前置后置邏輯瞳腌,以便測試人員兼容開發(fā)
前置分為兩個部分去講谴忧,一種是純方法層次上的前后置,一種是類內(nèi)部的用例的前后置
前后置類型
- 模塊級(setup_module/teardown_module)開始于模塊始末努咐,全局的
- 函數(shù)級(setup_function/teardown_function)只對函數(shù)用例生效
- 類級(setup_class/teardown_class)只在類前后運行一次(在類中生效苦蒿,類外不生效)
- 方法級(setup_method/teardown_method)開始于方法始末(在類中生效,類外不生效)
- setup/teardown 運行在調(diào)用方法的前后渗稍,在類中有測試方法和類外有測試函數(shù)時生效佩迟,是最接近用例的前后置
方法用例前后置
依次執(zhí)行setup_module >[ setup_function > setup > testcase > teardown > teardown_method ]> teardown_module
,其中[]
在有多個用例時循環(huán)執(zhí)行
- 看下示例代碼
# content of test_setup_teardown.py
import pytest
def setup_module():
print('\nsetup_module:模塊級別前置团滥,每個模塊執(zhí)行1次')
def teardown_module():
print('setup_module:模塊級別后置,每個模塊執(zhí)行1次')
def setup_function():
print("setup_function:每個用例開始前都會執(zhí)行")
def teardown_function():
print("teardown_function:每個用例結(jié)束后都會執(zhí)行")
def setup():
print('setup:方法調(diào)用前报强,每個測試函數(shù)調(diào)用前都會執(zhí)行')
def teardown():
print('\nteardown:方法調(diào)用后灸姊,每個測試函數(shù)結(jié)束后都會執(zhí)行')
def test_one():
print("正在執(zhí)行---test_one")
def test_two():
print("正在執(zhí)行---test_two")
- 看下執(zhí)行結(jié)果是否和預(yù)設(shè)一致
(venv) C:\測試文件夾\project\python\pytest_demo\fixture> pytest -sv test_setup_teardown.py
======================================================= test session starts ========================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\program files (x86)\python36-32\python.exe
cachedir: .pytest_cache
rootdir: C:\測試文件夾\project\python\pytest_demo, configfile: pytest.ini
collected 2 items
test_setup_teardown.py::test_one
setup_module:模塊級別前置,每個模塊執(zhí)行1次
setup_function:每個用例開始前都會執(zhí)行
setup:方法調(diào)用前秉溉,每個測試函數(shù)調(diào)用前都會執(zhí)行
正在執(zhí)行---test_one
PASSED
teardown:方法調(diào)用后力惯,每個測試函數(shù)結(jié)束后都會執(zhí)行
teardown_function:每個用例結(jié)束后都會執(zhí)行
test_setup_teardown.py::test_two setup_function:每個用例開始前都會執(zhí)行
setup:方法調(diào)用前,每個測試函數(shù)調(diào)用前都會執(zhí)行
正在執(zhí)行---test_two
PASSED
teardown:方法調(diào)用后召嘶,每個測試函數(shù)結(jié)束后都會執(zhí)行
teardown_function:每個用例結(jié)束后都會執(zhí)行
setup_module:模塊級別后置父晶,每個模塊執(zhí)行1次
======================================================== 2 passed in 0.01s =========================================================
類用例前后置
依次執(zhí)行setup_module > setup_class > [ setup_function > setup > testcase > teardown > teardown_method ]> teardown_class > teardown_module
,其中[]
在有多個用例時循環(huán)執(zhí)行
- 看下示例代碼
# content of test_setup_teardown.py
import pytest
def setup_module():
print('\nsetup_module:模塊級別前置,每個模塊執(zhí)行1次')
def teardown_module():
print('setup_module:模塊級別后置弄跌,每個模塊執(zhí)行1次')
class Test:
@classmethod
def setup_class(cls):
print("setup_class:所有用例執(zhí)行之前(類級)")
@classmethod
def teardown_class(cls):
print("teardown_class:所有用例執(zhí)行之后(類級)")
def setup(self):
print("setup:每個用例開始前執(zhí)行(調(diào)用方法前)")
def teardown(self):
print("teardown:每個用例結(jié)束后執(zhí)行(調(diào)用方法后)")
def setup_method(self):
print("setup_method:每個用例開始前執(zhí)行(方法級)")
def teardown_method(self):
print("teardown_method:每個用例結(jié)束后執(zhí)行(方法級)")
def test_one(self):
print("正在執(zhí)行---test_one")
def test_two(self):
print("正在執(zhí)行---test_two")
- 看下執(zhí)行結(jié)果是否和預(yù)設(shè)一致
(venv) C:\測試文件夾\project\python\pytest_demo\fixture>pytest -sv test_setup_teardown.py::Test
======================================================= test session starts ========================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\program files (x86)\python36-32\python.exe
cachedir: .pytest_cache
rootdir: C:\測試文件夾\project\python\pytest_demo, configfile: pytest.ini
collected 2 items
test_setup_teardown.py::Test::test_one
setup_module:模塊級別前置甲喝,每個模塊執(zhí)行1次
setup_class:所有用例執(zhí)行之前(類級)
setup_method:每個用例開始前執(zhí)行(方法級)
setup:每個用例開始前執(zhí)行(調(diào)用方法前)
正在執(zhí)行---test_one
PASSEDteardown:每個用例結(jié)束后執(zhí)行(調(diào)用方法后)
teardown_method:每個用例結(jié)束后執(zhí)行(方法級)
test_setup_teardown.py::Test::test_two setup_method:每個用例開始前執(zhí)行(方法級)
setup:每個用例開始前執(zhí)行(調(diào)用方法前)
正在執(zhí)行---test_two
PASSEDteardown:每個用例結(jié)束后執(zhí)行(調(diào)用方法后)
teardown_method:每個用例結(jié)束后執(zhí)行(方法級)
teardown_class:所有用例執(zhí)行之后(類級)
setup_module:模塊級別后置,每個模塊執(zhí)行1次
======================================================== 2 passed in 0.02s =========================================================