pytest框架(四)

测试用例setup和teardown

 

 

代码示例一

 1 # coding=utf-8
 2 import pytest
 3 
 4 
 5 def setup_module():
 6     print("setup_module:整个.py模块只执行一次")
 7     print("比如:所有用例开始前只打开一次浏览器")
 8 
 9 
10 def teardown_module():
11     print("teardown_module:整个.py模块只执行一次")
12     print("比如:所有用例结束只最后关闭浏览器")
13 
14 
15 def setup_function():
16     print("setup_function:每个用例开始前都会执行")
17 
18 
19 def teardown_function():
20     print("teardown_function:每个用例结束前都会执行")
21 
22 
23 def test_one():
24     print("正在执行----test_one")
25     x = "this"
26     assert 'h' in x
27 
28 
29 def test_two():
30     print("正在执行----test_two")
31     x = "hello"
32     assert hasattr(x, 'check')
33 
34 
35 def test_three():
36     print("正在执行----test_three")
37     a = "hello"
38     b = "hello world"
39     assert a in b
40 
41 
42 if __name__ == "__main__":
43     pytest.main(["-s", "test_fixt.py"])

代码示例二

 1 # coding=utf-8
 2 
 3 import pytest
 4 
 5 
 6 class TestClass:
 7     def setup(self):
 8         print("setup: 每个用例开始前执行")
 9 
10     def teardown(self):
11         print("teardown: 每个用例结束后执行")
12 
13     def setup_class(self):
14         print("setup_class:所有用例执行之前")
15 
16     def teardown_class(self):
17         print("teardown_class:所有用例执行之前")
18 
19     def setup_method(self):
20         print("setup_method:  每个用例开始前执行")
21 
22     def teardown_method(self):
23         print("teardown_method:  每个用例结束后执行")
24 
25     def test_one(self):
26         x = "this"
27         assert 'h' in x
28 
29     def test_two(self):
30         x = "hello"
31         assert hasattr(x, 'check')
32 
33     def test_three(self):
34         a = "hello"
35         b = "hello world"
36         assert a in b
37 
38 
39 if __name__ == "__main__":
40     pytest.main(['-q', 'test_class.py'])

 

posted @ 2018-08-22 19:22  小白2510  阅读(308)  评论(0编辑  收藏  举报