个人网站logo需要备案吗,网站运营优化培训,wordpress移动端导航栏,安徽省建设工程信用信息网文章目录 1.setup和teardown简介2.模块级别的 setup 和 teardown3.函数级别的 setup 和 teardown4.方法级别的 setup 和 teardown5.类级别的 setup 和 teardown 1.setup和teardown简介
在 pytest 中#xff0c;setup 和 teardown 用于在测试用例执行前后执行一些准备和清理操… 文章目录 1.setup和teardown简介2.模块级别的 setup 和 teardown3.函数级别的 setup 和 teardown4.方法级别的 setup 和 teardown5.类级别的 setup 和 teardown 1.setup和teardown简介
在 pytest 中setup 和 teardown 用于在测试用例执行前后执行一些准备和清理操作。
setup 和 teardown共有四个级别
等级说明范围函数级别setup_function 和 teardown_function针对每个测试函数执行前和执行后进行操作。类级别setup_class 和 teardown_class针对每个测试类执行前和执行后进行操作它们必须是类的静态方法。方法级别setup_method 和 teardown_method针对类中的每个测试方法执行前和执行后进行操作。模块级别setup_module 和 teardown_modulesetup_module 和 teardown_module针对整个测试模块执行前和执行后进行操作。
2.模块级别的 setup 和 teardown
针对整个测试模块执行前和执行后进行操作。 示例
def setup_module(module):print(f开始执行测试模块: {module.__name__})def teardown_module(module):print(f测试模块 {module.__name__} 执行完毕)def test_example1():print(执行测试用例1)assert 11def test_example2():print(执行测试用例2)assert 22运行结果
3.函数级别的 setup 和 teardown
针对每个测试函数执行前和执行后进行操作。 示例
def setup_function(function):print(f开始执行测试函数: {function.__name__})def teardown_function(function):print(f测试函数 {function.__name__} 执行完毕)def test_example1():print(执行测试用例1)assert 11def test_example2():print(执行测试用例2)assert 22运行结果
4.方法级别的 setup 和 teardown
针对类中的每个测试方法执行前和执行后进行操作。 示例
class TestClass:def setup_method(self,method):print(f开始执行测试方法: {method.__name__})def teardown_method(self, method):print(f测试方法 {method.__name__} 执行完毕)def test_example1(self):print(执行测试用例1)assert 1 1def test_example2(self):print(执行测试用例2)assert 2 2运行结果
5.类级别的 setup 和 teardown
针对每个测试类执行前和执行后进行操作它们必须是类的静态方法。 示例
class TestClass:def setup_class(cls):print(f开始执行测试类: {cls.__name__})def teardown_class(cls):print(f测试类 {cls.__name__} 执行完毕)def test_example1(self):print(执行测试用例1)assert 1 1def test_example2(self):print(执行测试用例2)assert 2 2运行结果