Python自动化测试pytest中fixtureAPI简单说明
什么是fixture
根据pytest官方文档的说明,fixture可以简单的归纳为具有以下功能的函数:
- 配置测试前系统的初始状态;
- 定义传入测试中的数据集;
- 为批量测试提供数据源等
- 调用灵活。可以在测试函数、模块、类或整个项目中声明fixture的名称来进行调用;
- 使用灵活。fixture即适用于简单的单元测试又适用于复杂的功能测试。根据测试的需求可对fixture进行参数化使用,并且可对fixture进行重复使用。
- fixture是以模块化方式实现的,因此允许fixture调用其他fixture函数;
- teardown的实现逻辑更加清晰明了,并且方便进行管理。
通过pytest官方文档的说明,我们可以知道:
- pytest以线性的方式顺序执行测试用例所调用的fixture函数;
- 当顺序较前的fixture函数执行报错后,pytest会停止执行该测试所调用的其他fixture,并且将测试标记出错;
- 测试标记错误,并不意味着测试未通过,只能说明测试无法尝试执行下去,因此我们需要尽可能的去为测试函数减少必要的依赖关系。
1.在以下demo代码中,
order()
返回类型存在问题,正确的应该返回一个list,我们给其返回一个None:import pytest@pytest.fixturedef order():return None#正确应该返回[],我们给返回一个None@pytest.fixturedef append_first(order):order.append(1)@pytest.fixturedef append_second(order, append_first):order.extend([2])@pytest.fixture(autouse=True)def append_third(order, append_second):order += [3]def test_order(order):assert order == [1, 2,3]
运行后结果如下:
test_order
被标记Error,并且信息提示:test setup failed
,说明是调用的fixture函数存在问题,且说明了错误原因。文章图片
2.如果是
test_order
运行未通,运行信息会怎么样提醒呢?我们按照以下demo修改测试代码,修改test_order
的断言语句:import pytest@pytest.fixturedef order():return []#返回一个list@pytest.fixturedef append_first(order):order.append(1)@pytest.fixturedef append_second(order, append_first):order.extend([2])@pytest.fixture(autouse=True)def append_third(order, append_second):order += [3]def test_order(order):assert order == [1, 2]#断言失败,正确应该是 order==[1,2,3]
运行结果如下:
test_order
被标记failed,且提醒是AssertionError
,断言出错。这说明是test_order
本身运行未通过。文章图片
2.fixture API @pytest.fixture()说明 pytest使用
@pytest.fixture()
来声明fixture方法。具体如何使用,我会在文章后面进行详细说明。在此,主要来简单说明一下fixture()
。def fixture(fixture_function: Optional[_FixtureFunction] = None,*,scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",params: Optional[Iterable[object]] = None,autouse: bool = False,ids: Optional[Union[Iterable[Union[None, str, float, int, bool]],Callable[[Any], Optional[object]],]] = None,name: Optional[str] = None,) -> Union[FixtureFunctionMarker, _FixtureFunction]:
参数说明:
2.1 scope
fixture函数的作用域。作用域从小到大依次为:
function(默认)
、class
、module
、package
、session
。还可传入一个可调用对象,以实现动态修改fixture的作用域。
后面会单独写一篇文章,为大家详细介绍fixture的scope。
2.2 params
传入测试数据集,动态生成测试用例,每一条数据都单独生成一条测试用例。通过
request.param
,可以获取传入的这些数据。后面会单独写一篇文章,为大家详细介绍fixture的参数化。
2.3 autouse
fixture自动应用标识。
如果是True,则在同作用域下的测试函数,会自动调用该fixture;如果是False,则测试函数需要主动去调用该fixture。
后面会在介绍fixture调用方法的文章给大家详细说明。
2.4 ids
测试用例ID标识,与
parmas
传入的参数一一对应。当未定义时,会自动生成id。示例2:
1.传入ids参数,运行以下demo:
import pytest@pytest.fixture(params=[1,2,3],ids=['A','B','C'])def ids(request):data=https://www.it610.com/article/request.paramprint(f'获取测试数据{data}')return datadef test_ids(ids):print(ids)
运行结果:
在执行信息中,我们可以发现
ids
的三个参数和params
的三个参数一一对应显示,并且ids
的参数作为测试用例id的一部分呈现出来。文章图片
2. 修改上面demo中的代码,不传入ids参数,运行一下:
import pytest@pytest.fixture(params=[1,2,3]) #未传入idsdef ids(request):data=https://www.it610.com/article/request.paramprint(f'获取测试数据{data}')return datadef test_ids(ids):print(ids)
运行结果:
查看运行结果我们可以发现,虽然没有传入
ids
,但是却自动生成了ids
。文章图片
测试结束后,我们常常以测试报告的形式来汇报测试结果,如结合allure呈现测试结果。通过
ids
传入的参数可以对测试用例进行说明,这样更方便我们查看测试结果。2.5 name
fixture的别名。fixture的name默认是
@pytest.fixture
所装饰的函数的函数名。使用fixture的别名可以提高代码的阅读性。示例3:
以下面的demo为例:
import pytest@pytest.fixture()def login():print('login')class SubClass:def sub_login(self):print('subcalss_login')class TestCase:def test_case1(self,login):#调用fixture——loginlogin=SubClass()#定义一个login并实例化SubClasslogin.sub_login() #调用SubClass中的sub_login()print('这是testcase1')
我们定义了一个fixture函数——
login()
,同时在test_case1中实例化了一个Subclass
类,并起名为login
,然后调用了SubClass类中的sub_login()
。如果代码复杂的情况,很容易将fixture函数的login与SubClass实例的login弄混淆,增加代码的阅读的复杂度。当我们使用fixture别名的话,在阅读代码的时候就很容易进行区分。
@pytest.fixture(name='module_login') def login():print('login')
class TestCase:def test_case1(self,module_login):#使用fixture别名:module_loginlogin=SubClass()#定义一个login并实例化SubClasslogin.sub_login() #调用SubClass中的sub_login()print('这是testcase1')
注意:
当使用name参数后,则无法再通过@pytest.fixture所装饰的函数的函数名来进行调用,必须使用name所指定fixture别名来调用。
2.6 fixture_function
目前pytest官方文档未给出具体说明。
文末说明:
以上内容是我在阅读pytest官方文档后,依照个人理解进行整理。内容可能会有理解错误之处,欢迎大家留言指正。谢谢!
【Python自动化测试pytest中fixtureAPI简单说明】 更多关于自动化测试pytest中fixtureAPI的资料请关注脚本之家其它相关文章!
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- 人脸识别|【人脸识别系列】| 实现自动化妆
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)