码迷,mamicode.com
首页 > 其他好文 > 详细

pytest框架(四)

时间:2018-08-22 20:31:25      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:mod   stc   one   打开   代码   测试   coding   浏览器   def   

测试用例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])

 

pytest框架(四)

标签:mod   stc   one   打开   代码   测试   coding   浏览器   def   

原文地址:https://www.cnblogs.com/loveapple/p/9519859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!