setup和teardown主要分為:
模塊級、類級
功能級、函數(shù)級
1、模塊級、類級 setup_class/teardown_class
運行于測試類的始末,即:在一個測試內只運行一次setup_class和teardown_class,不關心測試類內有多少個測試函數(shù)。
#enconding:utf-8
import pytest
class Test_Class:
? ? def setup_class(self):
? ? ? ? print('\nstart')
? ? def teardown_class(self):
? ? ? ? print('\nend')
? ? def test_1(self):
? ? ? ? print('111')
? ? ? ? assert 1
? ? def test_2(self):
? ? ? ? print('222')
? ? ? ? assert 1
if __name__=='__main__':
? ? pytest.main(['-s','-v','test_3.py'])
執(zhí)行結果:
test_3.py::Test_Class::test_1
start
111
PASSED
test_3.py::Test_Class::test_2 222
PASSED
end
2、功能級、函數(shù)級??setup()/teardown()
運行于測試方法的始末,即:運行一次測試函數(shù)會運行一次setup和teardown
import pytest
class Test_Class:
? ? def setup(self):
? ? ? ? print('\nstart')
? ? def teardown(self):
? ? ? ? print('\nend')
? ? def test_1(self):
? ? ? ? print('111')
? ? ? ? assert 1
? ? def test_2(self):
? ? ? ? print('222')
? ? ? ? assert 1
if __name__=='__main__':
? ? pytest.main(['-s','-v','test_2.py'])
執(zhí)行結果:
test_2.py::Test_Class::test_1
start
111
PASSED
end
test_2.py::Test_Class::test_2
start
222
PASSED
end