前言
今天我們再說一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同學應該知道我們在初始化環境和銷毀工作時,unittest使用的是setUp,tearDown方法,那么在pytest框架中同樣存在類似的方法,今天我們就來具體說明。
先附上官方文檔的一段說明
1.每個級別的setup/teardown都可以多次復用
2.如果相應的初始化函數執行失敗或者被跳過則不會執行teardown方法
3.在pytest4.2之前,xunit fixture 不遵循fixture的作用規則的,因此可以在一個session級別且參數auto=True的fixture前執行setup_method方法
但是到目前為止,所有的xunit fixture已經遵循了fixture執行的規則
function級別
實例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
def setup_function(function): print ( '\n--------------------' ) print ( '函數執行前所做的操作' ) print ( '\n--------------------' ) def teardown_function(function): print ( '\n--------------------' ) print ( '函數執行后所做的操作' ) print ( '\n--------------------' ) def test_function_1(): print ( '\n測試函數1' ) def test_function_2(): print ( '\n測試函數2' ) if __name__ = = '__main__' : import pytest pytest.main([ '-sq' , 'functionLevel.py' ]) |
輸出結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
functionLevel.py - - - - - - - - - - - - - - - - - - - - 函數執行前所做的操作 - - - - - - - - - - - - - - - - - - - - 測試函數 1 - - - - - - - - - - - - - - - - - - - - 函數執行后所做的操作 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 函數執行前所做的操作 - - - - - - - - - - - - - - - - - - - - 測試函數 2 - - - - - - - - - - - - - - - - - - - - 函數執行后所做的操作 - - - - - - - - - - - - - - - - - - - - [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = |
說明
通過輸出結果我們可以總結:setup_function會在每一個測試函數前執行初始化操作;teardown_function會在每一個測試函數執行后執行銷毀工作
method級別
實例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class TestMethod( object ): def setup_method( self , method): print ( '\n--------------------' ) print ( '方法執行前所做的操作' ) print ( '\n--------------------' ) def teardown_method( self , method): print ( '\n--------------------' ) print ( '方法執行后所做的操作' ) print ( '\n--------------------' ) def test_method_1( self ): print ( '\n測試方法1' ) def test_method_2( self ): print ( '\n測試方法2' ) if __name__ = = '__main__' : import pytest pytest.main([ '-sq' , 'methodLevel.py' ]) |
輸出結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
methodLevel.py - - - - - - - - - - - - - - - - - - - - 方法執行前所做的操作 - - - - - - - - - - - - - - - - - - - - 測試方法 1 - - - - - - - - - - - - - - - - - - - - 方法執行后所做的操作 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 方法執行前所做的操作 - - - - - - - - - - - - - - - - - - - - 測試方法 2 - - - - - - - - - - - - - - - - - - - - 方法執行后所做的操作 - - - - - - - - - - - - - - - - - - - - [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.03 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = |
說明
通過輸出結果我們可以總結:setup_method會在每一個測試方法前執行初始化操作;teardown_method會在每一個測試方法執行后執行銷毀工作,且方法級別的fixture是作用在測試類中的方法上的
class級別
實例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class TestClass( object ): @classmethod def setup_class( cls ): print ( '\nsetup_class() for {}' . format ( cls .__name__)) @classmethod def teardown_class( cls ): print ( '\nteardown_class() for {}' . format ( cls .__name__)) def test_1( self ): print ( 'self.test_1()' ) def test_2( self ): print ( 'self.test_2()' ) if __name__ = = '__main__' : import pytest pytest.main([ '-sq' , 'classLevel.py' ]) |
輸出結果
1
2
3
4
5
6
7
|
classLevel.py setup_class() for TestClass . self .test_1() . self .test_2() teardown_class() for TestClass [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.06 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = |
說明
通過輸出結果我們可以總結:setup_class會在測試類執行前執行一次初始化操作;teardown_class會在測試類執行后執行一次銷毀工作,且class級別的fixture需要使用@classmethod裝飾
module級別
實例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def setup_module(module): print ( '\nsetup_module() for {}' . format (module.__name__)) def teardown_module(module): print ( '\nteardown_module() for {}' . format (module.__name__)) def test_1(): print ( 'test_1()' ) def test_2(): print ( 'test_2()' ) class TestClass( object ): def test_3( self ): print ( 'self.test_3()' ) def test_4( self ): print ( 'self.test_4()' ) if __name__ = = '__main__' : import pytest pytest.main([ '-sq' , 'moduleLevel.py' ]) |
輸出結果
1
2
3
4
5
6
7
8
9
|
moduleLevel.py setup_module() for moduleLevel .test_1() .test_2() . self .test_3() . self .test_4() teardown_module() for moduleLevel [ 100 % ] = = = = = = = = = = = = = = = = = = = = = = = = = = 4 passed in 0.04 seconds = = = = = = = = = = = = = = = = = = = = = = = = = = = |
說明
通過輸出結果我們可以總結:setup_module會在整個測試文件也就是模塊中的測試類或者測試函數,測試方法執行前執行一次初始化操作;teardown_module會在整個測試文件也就是模塊中的測試類或者測試函數,方法執行后執行一次銷毀工作
以上就是xunit fixture的4個級別,實際工作中該如何使用還需多練習,深入理解才能得心應手!
附上官方文檔做參考雖是英文但是很詳細
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-xunit-fixture.html