1. 程式人生 > >pytest框架(四)

pytest框架(四)

down mod test 正在執行 分享 asa Coding his elf

測試用例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框架(四)