pytest中的stup和teardown
setup和teardown解释:
- 在执行之前执行setup中的代码
- 在执行之后执行teardown中的代码
stup和teardown的具体实现方式:
- 模块级别的setup和teardown,在py文件执行之前执行和之后执行
- setup_module
- teardown_module
案例:
import pytest
def setup_module():
print("模块执行之前")
def teardown_module():
print("模块执行之后")
class TestAddFun(object):
def test_test01(self):
print("自动化第一个用例")
def test_test02(self):
print("自动化第二个用例")
if __name__ == '__main__':
pytest.main(["-s", "test_num.py"])
- 函数级别的setup和teardown,在执行函数之前执行和之后执行
- setup_function
- teardown_function
案例:
import pytest
def setup_function():
print("函数执行之前")
def teardown_function():
print("函数执行之后")
def test_test01(): # 在执行test_test01函数之前会执行setup_function,之后会执行teardown_function
print("自动化第一个用例")
def test_test02(): # # 在执行test_test02函数之前会执行setup_function,之后会执行teardown_function
print("自动化第二个用例")
if __name__ == '__main__':
pytest.main(["-s", "test_num.py"])
- 类级别的setup和teardown,在类执行之前执行和之后执行
- setup_function
- teardown_function
案例:
import pytest
class Test_AddFun(object):
def setup_class(self):
print("类执行之前")
def teardown_class(self):
print("类执行之后")
def test_test01(self):
print("自动化第一个用例")
def test_test02(self):
print("自动化第二个用例")
if __name__ == '__main__':
pytest.main(["-s", "test_num.py"])
- 方法级别的setup和teardown,在类中的方法之前执行和之后执行
- setup_method
- teardown_method
案例:
import pytest
class Test_AddFun(object):
def setup_method(self):
print("方法执行之前")
def teardown_method(self):
print("方法执行之后")
def test_test01(self): # 在执行test_test01方法之前会执行setup_method,之后会执行teardown_method
print("自动化第一个用例")
def test_test02(self): # 在执行test_test01方法之前会执行setup_method,之后会执行teardown_method
print("自动化第二个用例")
if __name__ == '__main__':
pytest.main(["-s", "test_num.py"])
浙公网安备 33010602011771号