前言:fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进:
1、有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活;
2、按模块化的方式实现,每个fixture都可以互相调用;
3、fixture的范围从简单的单元测试到复杂的功能测试,可以对fixture配置参数,或者跨函数function,类class,模块module或整个测试session范围。
环境:python3、sublime text3、pytest
用法:@pytest.fixture(),fixture命名不要以test开头,跟用例区分开;fixture是有返回值,没有返回值默认为None;用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。下面就是演示一下fixture的相关特性。
1、单个fixture的使用
import pytest
#可作为变量
@pytest.fixture()
def test_01():
m = 'hello pytest'
return m
def test_02(test_01):
assert test_01 == 'hello pytest'
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
2、单个fixture的使用
注:如果用例需要用到多个fixture的返回数据,fixture也可以返回一个元祖,list或字典,然后从里面取出对应数据
import pytest
#可作为变量
@pytest.fixture()
def test_01():
m = 'hello pytest'
n = 'learning'
print("传出参数m和n")
return (m,n)
def test_02(test_01):
x = test_01[0]
y = test_01[1]
assert x == 'hello pytest'
assert y == 'learning'
print('success')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
3、多个fixture相互调用
import pytest
#可作为变量
@pytest.fixture()
def test_01():
m = 'hello pytest'
print("传出参数m")
return m
@pytest.fixture()
def test_02():
n = 'learning'
print("传出参数n")
return n
def test_03(test_01,test_02):
x = test_01
y = test_02
assert x == 'hello pytest'
assert y == 'learning'
print('传入多个fixture参数success')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
4、上面是fixture的3中常用使用方式,下面介绍一下fixture的作用范围(scope)
1)fixture的作用范围
fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function
-function:每一个函数或方法都会调用
-class:每一个类调用一次,一个类中可以有多个方法
-module:每一个.py文件调用一次,该文件内又有多个function和class
-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module
2)fixture源码详解
fixture(scope='function',params=None,autouse=False,ids=None,name=None):
scope:有四个级别参数"function"(默认),"class","module","session"
params:一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它。
autouse:如果True,则为所有测试激活fixture func可以看到它。如果为False则显示需要参考来激活fixture
ids:每个字符串id的列表,每个字符串对应于params这样他们就是测试ID的一部分。如果没有提供ID它们将从params自动生成
name:fixture的名称。这默认为装饰函数的名称。如果fixture在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽,解决这个问题的一种方法时将装饰函数命令"fixture_<fixturename>"然后使用"@pytest.fixture(name='<fixturename>')"。
1)scope="function",作用于方法中
注:@pytest.fixture()如果不写参数,参数就是scope="function",它的作用范围是每个测试用例来之前运行一次,销毁代码在测试用例之后运行。
import pytest
@pytest.fixture()
def test_01():
m = 'hello pytest'
print("传出m")
return m
@pytest.fixture(scope='function')
def test_02():
n = 'learning'
print("\n传出n")
return n
def test_03(test_01):
x = 'hello pytest'
assert x == test_01
print('找到x')
def test_04(test_02):
y = 'learning'
assert y == test_02
print('找到y')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
2)scope="function",作用于类中的每个方法
import pytest
# 在类中调用
@pytest.fixture()
def test_01():
m = 'hello pytest'
print("传出m")
return m
@pytest.fixture(scope='function')
def test_02():
n = 'learning'
print("\n传出n")
return n
class Test_one():
def test_03(self,test_01):
x = 'hello pytest'
assert x == test_01
print('找到x')
def test_04(self,test_02):
y = 'learning'
assert y == test_02
print('找到y')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
3)scope="class"
注:fixture为class级别的时候,如果一个class里面有多个用例,都调用了次fixture,那么此fixture只在此class里所有用例开始前执行一次。例如:下面例子中,方法test_02被类调用了2次,但是只会在用例开始钱执行一次。
import pytest
@pytest.fixture()
def test_01():
m = 'hello pytest'
print("传出m")
return m
@pytest.fixture(scope='class')
def test_02():
name = '张三'
print("\n传出name")
return name
class Test_one():
def test_03(self,test_02):
x = 'hello pytest'
print('找不到x')
assert x == test_02
def test_04(self,test_02):
y = '张三'
assert y == test_02
print('\n找到y')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
4)scope="module"
注:fixture为module时,在当前.py脚本里面所有用例开始前只执行一次;例如在下面例子中test_02被test_03和类Test_one中的test_04调用了2次,但是只执行一次test_02。
import pytest
@pytest.fixture(scope='module')
def test_02():
name = '张三'
print("\n传出name")
return name
def test_03(test_02):
x = 'hello pytest'
print('找不到x')
assert x == test_02
class Test_one():
def test_04(self,test_02):
y = '张三'
assert y == test_02
print('\n找到y')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
5)scope="session"
fixture为session级别是可以跨.py模块调用的,当存在多个.py文件的用例时,如果多个用例只需调用一次fixture,那就可以设置为scope="session",并且写到conftest.py文件里。
conftest.py文件名称是固定的,pytest会自动识别该文件;放到项目的根目录下就可以全局调用了,如果放到某个package下,那就在该package内有效。
文件目录为:
#conftest.py下的代码
import pytest
@pytest.fixture(scope='session')
def test_02():
name = '张三'
print("\n传出name")
return name
#调用文件下的代码
import pytest
def test_03(test_02):
x = 'hello pytest'
print('找不到x')
assert x == test_02
class Test_one():
def test_04(self,test_02):
y = '张三'
assert y == test_02
print('\n找到y')
if __name__ == '__main__':
pytest.main(["-s",'-q','test_seven.py'])
参考:pytest框架之fixture详细使用