前言
今天我们再说一下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