自动化测试框架Pytest No.2
Pytest的setup、teardown函数
setup和teardown函数的用例运行级别
-
模块级(setup_module/teardown_module)开始于模块始末,全局的
-
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
-
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
-
方法级(setup_method/teardown_method)开始于方法始末(在类中)
-
类里面的(setup/teardown)运行在调用方法的前后
setup、teardown函数存在于测试类内部
- 模块级别
import pytest
def setup_module():
print("------>setup_module")
def teardown_module():
print("----->teardown_module")
class Test_2:
def test_a(self):
print("----->test_a")
assert 1
def test_b(self):
print("----->test_b")
if __name__=='__main__':
pytest.main(["-s","test_2.py"])
- 函数级别
import pytest
# 模块级开始
def setup_module():
print("------>setup_module")
# 模块级结束
def teardown_module():
print("----->teardown_module")
# 函数级开始
def setup_function():
print("----->setup_function")
# 函数级结束
def teardown_function():
print("----->teardown_function")
def test_a():
print("----->test_a")
assert 1
def test_b():
print("----->test_b")
assert 1
if __name__=='__main__':
pytest.main(["-s","test_2.py"])
- 类级别
运行于测试类的始末:在一个测试内只运行一次setup_class和teardown_class,不关心测试类内有多少个测试函数。
import pytest
class Test_2:
#类级别开始
def setup_class(self):
print("----->setup_class")
#类级别结束
def teardown_class(self):
print("----->tesrdown_class")
def test_a(self):
print("----->test_a")
assert 1
def test_b(self):
print("----->test_b")
if __name__=='__main__':
pytest.main(["-s","test_2.py"])
- 类里面的
运行于测试方法的始末:运行一次测试函数会运行一次setup和teardown
import pytest
class Test_2:
# 函数级开始
def setup(self):
print("----->setup_method")
# 函数级结束
def teardown(self):
print("----->teardown_mrthod")
def test_a(self):
print("----->test_a")
assert 1
def test_b(self):
print("----->test_b")
if __name__=='__main__':
pytest.main(["-s","test_2.py"])
- 模块、类、方法、函数
import pytest
# 模块级别开始
def setup_module():
print("------>setup_module")
# 模块级别结束
def teardown_module():
print("----->teardown_module")
class Test_2:
#类级别开始
def setup_class(self):
print("----->setup_class")
#类级别结束
def teardown_class(self):
print("----->tesrdown_class")
# 方法级开始
def setup_method(self):
print("----->setup_method")
# 方法级结束
def teardown_method(self):
print("----->teardown_method")
# 函数级开始
def setup(self):
print("----->setup")
# 函数级结束
def teardown(self):
print("----->teardown")
def test_a(self):
print("----->test_a")
assert 1
def test_b(self):
print("----->test_b")
if __name__=='__main__':
pytest.main(["-s","test_2.py"])
import pytest
# 模块级开始
def setup_module():
print("------>setup_module")
# 模块级结束
def teardown_module():
print("----->teardown_module")
# 函数级开始
def setup_function():
print("----->setup_function")
# 函数级结束
def teardown_function():
print("----->teardown_function")
def test_a():
print("----->test_a")
assert 1
def test_b():
print("----->test_b")
assert 1
class Test_2:
#类级别开始
def setup_class(self):
print("----->setup_class")
#类级别结束
def teardown_class(self):
print("----->tesrdown_class")
# 方法级开始
def setup_method(self):
print("----->setup_method")
# 方法级结束
def teardown_method(self):
print("----->teardown_method")
# 类里面开始
def setup(self):
print("----->setup")
# 类里面结束
def teardown(self):
print("----->teardown")
def test_c(self):
print("----->test_c")
assert 1
def test_d(self):
print("----->test_d")
assert 1
if __name__=='__main__':
pytest.main(["-s","test_2.py"])