原文:https://www.cnblogs.com/peiminer/p/9376352.html
之前我寫的unittest的setup和teardown,還有setupClass和teardownClass(需要配合@classmethod裝飾器一起使用),接下來就介紹pytest的類似於這類的固件。
(1.setup_function、teardown_function 2.setup_class、teardown_class 3.setup_method、teardown_method 4.setup_module、teardown_module)
setup/teardown和unittest里面的setup/teardown是一樣的功能,這里setup_method和teardown_method的功能和setup/teardown功能是一樣的,優先級是先執行setup_method,在執行setup。一般二者用其中一個即可,就不詳細介紹了。setup_class和teardown_class等價於unittest里面的setupClass和teardownClass
一、函數級的(setup_function、teardown_function)只對函數用例生效,而且不在類中使用
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest """ 只對函數用例生效,不在類中 setup_function teardown_function """ def setup_function(): print "setup_function():每個方法之前執行" def teardown_function(): print "teardown_function():每個方法之后執行" def test_01(): print "正在執行test1" x = "this" assert 'h' in x def test_02(): print "正在執行test2" x = "hello" assert hasattr(x,"hello") def add(a,b): return a+b def test_add(): print "正在執行test_add()" assert add(3,4) == 7 if __name__=="__main__": pytest.main(["-s","test_function.py"])
運行結果為:(-s為了顯示用例的打印信息 -q只顯示結果不顯示過程)
可以看出執行的結果是:
setup_function--》 test_01 --》teardown_function
setup_function--》 test_02 --》teardown_function
setup_function--》 test_add --》teardown_function
二、類級的(setup_class、teardown_class)在類中使用,類執行之前運行一次,類執行之后運行一次
#!/usr/bin/env/python # -*-coding:utf-8-*- """ 在類之前和之后執行一次 setup_class teardown_class """ import pytest class TestClass(object): def setup_class(self): print "setup_class(self):每個類之前執行一次" def teardown_class(self): print "teardown_class(self):每個類之后執行一次" def add(self,a,b): print "這是加法運算" return a+b def test_01(self): print "正在執行test1" x = "this" assert 'h' in x def test_add(self): print "正在執行test_add()" assert self.add(3, 4) == 7
執行結果:
可以看出執行的順序是 setup_class --》 test1 --》test_add()--》teardown_class
三、類中方法級的(setup_method、teardown_method)在每一個方法之前執行一次,在每一個方法之后執行一次
#!/usr/bin/env/python # -*-coding:utf-8-*- """ 開始於方法始末(在類中) setup_method teardown_method """ import pytest class TestMethod(object): def setup_class(self): print "setup_class(self):每個類之前執行一次\n" def teardown_class(self): print "teardown_class(self):每個類之后執行一次" def setup_method(self): print "setup_method(self):在每個方法之前執行" def teardown_method(self): print "teardown_method(self):在每個方法之后執行\n" def add(self,a,b): print "這是加法運算" return a+b def test_01(self): print "正在執行test1" x = "this" assert 'h' in x def test_add(self): print "正在執行test_add()" assert self.add(3, 4) == 7
執行結果: setup_class --》 setup_method -->test1 -->teardown_method --》setup_method --> test_add()--》teardown_method --> teardown_class
四、模塊級的(setup_module、teardown_module)全局的,在模塊執行前運行一遍,在模塊執行后運行一遍
#!/usr/bin/env/python # -*-coding:utf-8-*- import pytest """ 開始於模塊始末,全局的 setup_module teardown_module """ def setup_module(): print "setup_module():在模塊最之前執行\n" def teardown_module(): print "teardown_module:在模塊之后執行" def setup_function(): print "setup_function():每個方法之前執行" def teardown_function(): print "teardown_function():每個方法之后執行\n" def test_01(): print "正在執行test1" x = "this" assert 'h' in x def add(a,b): return a+b def test_add(): print "正在執行test_add()" assert add(3,4) == 7
運行結果:setup_module --> setup_function --> test_01--> teardown_function --> setup_function --> test_add()--> teardown_function --> teardown_module
五、當類和函數都有的時候
#!/usr/bin/env/python # -*-coding:utf-8-*- """ 在類之前和之后執行一次 setup_class teardown_class """ import pytest def setup_module(): print "setup_module():在模塊最之前執行\n" def teardown_module(): print "teardown_module:在模塊之后執行" def setup_function(): print "setup_function():每個方法之前執行" def teardown_function(): print "teardown_function():每個方法之后執行\n" def test_10(): print "正在執行test1" x = "this" assert 'h' in x def add0(a,b): return a+b def test_add(): print "正在執行test_add()" assert add0(3,4) == 7 class TestClass(object): def setup_class(self): print "setup_class(self):每個類之前執行一次" def teardown_class(self): print "teardown_class(self):每個類之后執行一次" def add(self,a,b): print "這是加法運算" return a+b def test_01(self): print "正在執行test1" x = "this" assert 'h' in x def test_add(self): print "正在執行test_add()" assert self.add(3, 4) == 7 if __name__=="__main__": pytest.main(["-s","test_class0.py"])
運行結果:可以看出來,都互不影響,setup_module還是在最之前執行,所有之后執行。
setup_modele --> setup_function -->test1 -->teardown_function --> setuo_function -->test_add -->teardown_function -->setup_class -->teardown_class-->taerdown_module