测试用例setup和teardown
代码示例一
1 #coding=utf-8 2 importpytest 3 4 5 defsetup_module(): 6 print("setup_module:整个.py模块只执行一次") 7 print("比如:所有用例开始前只打开一次浏览器") 8 9 10 defteardown_module(): 11 print("teardown_module:整个.py模块只执行一次") 12 print("比如:所有用例结束只最后关闭浏览器") 13 14 15 defsetup_function(): 16 print("setup_function:每个用例开始前都会执行") 17 18 19 defteardown_function(): 20 print("teardown_function:每个用例结束前都会执行") 21 22 23 deftest_one(): 24 print("正在执行----test_one") 25 x = "this" 26 assert 'h' inx 27 28 29 deftest_two(): 30 print("正在执行----test_two") 31 x = "hello" 32 assert hasattr(x, 'check') 33 34 35 deftest_three(): 36 print("正在执行----test_three") 37 a = "hello" 38 b = "hello world" 39 assert a inb 40 41 42 if __name__ == "__main__": 43 pytest.main(["-s", "test_fixt.py"])
代码示例二
1 #coding=utf-8 2 3 importpytest 4 5 6 classTestClass: 7 defsetup(self): 8 print("setup: 每个用例开始前执行") 9 10 defteardown(self): 11 print("teardown: 每个用例结束后执行") 12 13 defsetup_class(self): 14 print("setup_class:所有用例执行之前") 15 16 defteardown_class(self): 17 print("teardown_class:所有用例执行之前") 18 19 defsetup_method(self): 20 print("setup_method: 每个用例开始前执行") 21 22 defteardown_method(self): 23 print("teardown_method: 每个用例结束后执行") 24 25 deftest_one(self): 26 x = "this" 27 assert 'h' inx 28 29 deftest_two(self): 30 x = "hello" 31 assert hasattr(x, 'check') 32 33 deftest_three(self): 34 a = "hello" 35 b = "hello world" 36 assert a inb 37 38 39 if __name__ == "__main__": 40 pytest.main(['-q', 'test_class.py'])