python單元測試框架pytest——fixture函數(類似unitest的setup和teardown)


pytest的setup和teardown函數(曾被一家雲計算面試官問到過)。

pytest提供了fixture函數用以在測試執行前和執行后進行必要的准備和清理工作。與python自帶的unitest測試框架中的setup、teardown類似,但是fixture函數對setup和teardown進行了很大的改進。

  • fixture函數可以使用在測試函數中,測試類中,測試文件中以及整個測試工程中。
  • fixture支持模塊化,fixture可以相互嵌套
  • fixture支持參數化
  • fixture支持unittest類型的setup和teardown

1)模塊級(setup_module/teardown_module)開始於模塊始末:在所有測試用例開始執行setup_module和所有用例執行結束后執行teardown_module

2)類級(setup_class/teardown_class)開始於類的始末:在當前測試類的開始與結束執行

3)類里面的(setup/teardown)(運行在調用函數的前后)

4)功能級(setup_function/teardown_function)開始於功能函數始末(不在類中):用於每個測試用例開始執行時執行setup_function和在每個用例執行結束后執行teardown_function

5)方法級(setup_method/teardown_method)開始於方法始末(在類中):在每個測試方法開始與結束執行

 1 def setup_module(module):  2 print("setup_module module:%s" % module.__name__)  3  4  5 def teardown_module(module):  6 print("teardown_module module:%s" % module.__name__)  7  8  9 def setup_function(function): 10 print("setup_function function:%s" % function.__name__) 11 12 13 def teardown_function(function): 14 print("teardown_function function:%s" % function.__name__) 15 16 17 def test_numbers_3_4(): 18 print('test_numbers_3_4 <============================ actual test code') 19 assert 3 * 4 == 12 20 21 22 def test_strings_a_3(): 23 print('test_strings_a_3 <============================ actual test code') 24 assert 'a' * 3 == 'aaa' 25 26 27 class TestUM: 28 def setup(self): 29 print("setup class:TestStuff") 30 31 def teardown(self): 32 print("teardown class:TestStuff") 33 34 def setup_class(cls): 35 print("setup_class class:%s" % cls.__name__) 36 37 def teardown_class(cls): 38 print("teardown_class class:%s" % cls.__name__) 39 40 def setup_method(self, method): 41 print("setup_method method:%s" % method.__name__) 42 43 def teardown_method(self, method): 44 print("teardown_method method:%s" % method.__name__) 45 46 def test_numbers_5_6(self): 47 print('test_numbers_5_6 <============================ actual test code') 48 assert 5 * 6 == 30 49 50 def test_strings_b_2(self): 51 print('test_strings_b_2 <============================ actual test code') 52 assert 'b' * 2 == 'bb'

好好觀察一下運行結果:

setup_module      module:test_pytest_setup_teardown
setup_function    function:test_numbers_3_4
test_numbers_3_4  <============================ actual test code
.teardown_function function:test_numbers_3_4
setup_function    function:test_strings_a_3
test_strings_a_3  <============================ actual test code
.teardown_function function:test_strings_a_3
setup_class       class:TestUM
setup_method      method:test_numbers_5_6
setup             class:TestStuff
test_numbers_5_6  <============================ actual test code
.teardown          class:TestStuff
teardown_method   method:test_numbers_5_6
setup_method      method:test_strings_b_2
setup             class:TestStuff
test_strings_b_2  <============================ actual test code
.teardown          class:TestStuff
teardown_method   method:test_strings_b_2
teardown_class    class:TestUM
teardown_module   module:test_pytest_setup_teardown

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM