前言

官方文档关于fixture功能的解释如下:

The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions:

  • fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects.
  • fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures.
  • fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across function, class, module or whole test session scopes.

fixtrue特点

  1. 和setup和teardown一样的功能,但相对更灵活一些
  2. 通过conftest.py文件可以实现数据共享,方便调用

使用fixtrue

通过fixtrue源码,可以看到fixtrue的几个参数作用如下:

def fixture(callable_or_scope=None, *args, scope="function", params=None, autouse=False, ids=None, name=None)
:arg scope: the scope for which this fixture is shared, one of
``"function"`` (default), ``"class"``, ``"module"``,
``"package"`` or ``"session"`` (``"package"`` is considered **experimental**
at this time). This parameter may also be a callable which receives ``(fixture_name, config)``
as parameters, and must return a ``str`` with one of the values mentioned above. See :ref:`dynamic scope` in the docs for more information.
scope: scope 有四个级别参数 "function", "class", "module" or "session". 默认情况下为function
:arg params: an optional list of parameters which will cause multiple
invocations of the fixture function and all of the tests
using it.
The current parameter is available in ``request.param``.
一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它 :arg autouse: if True, the fixture func is activated for all tests that
can see it. If False (the default) then an explicit
reference is needed to activate the fixture.
如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture :arg ids: list of string ids each corresponding to the params
so that they are part of the test id. If no ids are provided
they will be generated automatically from the params. 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
:arg name: the name of the fixture. This defaults to the name of the
decorated function. If a fixture is used in the same module in
which it is defined, the function name of the fixture will be
shadowed by the function arg that requests the fixture; one way
to resolve this is to name the decorated function
``fixture_<fixturename>`` and then use
``@pytest.fixture(name='<fixturename>')``.
fixture的名称。 默认装饰函数的名称。 如果fixture在定义它的同一模块中使用,
功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
"""

  


 

实际使用

在举例过程中可以使用 pytest --setup-show 文件名.py 执行测试用例,可以查看用例的执行细节

第一个例子:scope为默认值

import pytest

@pytest.fixture(scope="class")
def login():
print("登录") def test_1(login):
print("登录后1") def test_2(login):
print("登录后2") test_fix2.py
SETUP C login
test_fix2.py::test_1 (fixtures used: login).
TEARDOWN C login
SETUP C login
test_fix2.py::test_2 (fixtures used: login).
TEARDOWN C login

  从执行结果看到,当fixtrue参数为空,默认为function时,两条测试用例各执行了一次login函数

  

第二个例子:scope=class

@pytest.fixture(scope="class")
def login():
print("登录") class TestCase():
def test_1(self, login):
print("登录后1") def test_2(self, login):
print("登录后2") 执行结果:
test_fix2.py 
SETUP C login
test_fix2.py::TestCase::test_1 (fixtures used: login).
test_fix2.py::TestCase::test_2 (fixtures used: login).
TEARDOWN C login

 从执行结果可以看到,在两条测试用例login在该类中只执行了一次 

第三个例子:scope="module"

import pytest


@pytest.fixture(scope="module")
def login():
print("登录") def test_1(login):
print("登录后1") def test_2(login):
print("登录后2") class TestCase():
def test_case1(self, login):
print("类方法,登录1") def test_case2(self, login):
print("类方法,登录2") 执行结果:

test_fix2.py
SETUP M login
test_fix2.py::test_1 (fixtures used: login).
test_fix2.py::test_2 (fixtures used: login).
test_fix2.py::TestCase::test_case1 (fixtures used: login).
test_fix2.py::TestCase::test_case2 (fixtures used: login).
TEARDOWN M login

第四个例子:scope="session",跨文件使用

文件一

import pytest

def test_1(login):
print("登录后1") def test_2(login):
print("登录后2") class TestCase():
def test_case1(self, login):
print("类方法,登录1") def test_case2(self, login):
print("类方法,登录2") 文件二 import pytest def test_1(login):
print('第二个文件登录后的第一个用例')
conftest.py 文件
import pytest
@pytest.fixture(scope="session")
def login():
print("登录")

执行结果:

test_fix2.py
SETUP S login
test_fix2.py::test_1 (fixtures used: login).
test_fix2.py::test_2 (fixtures used: login).
test_fix2.py::TestCase::test_case1 (fixtures used: login).
test_fix2.py::TestCase::test_case2 (fixtures used: login).
test_fix1.py
test_fix1.py::test_1 (fixtures used: login).
TEARDOWN S login

  由上面的执行结果可知,当scope="session"的时候,多个测试文件,多个测试用例,添加fixtrue装饰器后都是执行一次

小结

由以上四个简单的例子,可以看到pytest具有的强大的fixtrue功能,可以使用例设计更加灵活,测试数据、测试依赖等配置更加方便,以后在书写测试脚本的时候,可以根据不同请款合理使用fixtrue功能

pytest学习笔记二 fixtrue的更多相关文章

  1. pytest 学习笔记二:兼容unittest、执行方式、生成报告

    1.官方文档上说pytest兼容unittest时,不支持setUpModule 和 tearDownModule,但实际验证是可以的. 验证的场景是py文件中,只有一个测试类, 经验证有多个测试类, ...

  2. [转载]pytest学习笔记

    pytest学习笔记(三)   接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, ...

  3. WPF的Binding学习笔记(二)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...

  4. AJax 学习笔记二(onreadystatechange的作用)

    AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...

  5. [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计

    源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...

  6. JMX学习笔记(二)-Notification

    Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...

  7. java之jvm学习笔记二(类装载器的体系结构)

    java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...

  8. Java IO学习笔记二

    Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...

  9. 《SQL必知必会》学习笔记二)

    <SQL必知必会>学习笔记(二) 咱们接着上一篇的内容继续.这一篇主要回顾子查询,联合查询,复制表这三类内容. 上一部分基本上都是简单的Select查询,即从单个数据库表中检索数据的单条语 ...

随机推荐

  1. 【linux】linux磁盘空间 目录查看清理 和 文件查看清理

    =========================大目录排查============================= 一.首先查看磁盘挂载,磁盘空间使用情况 1.进入根路径 cd / 2.查看磁盘挂 ...

  2. 深入V8引擎-初始化之InitPlatform

    上一篇其实想讲初始化的第二步,但是内容比较无聊,所以换了一个话题,谈了谈v8的命名空间和宏,稍微轻松一下. 在这里还是接着说说初始化过程,毕竟写博客的初衷是对自己努力的记录,不是为了吸粉,这篇没图,对 ...

  3. thinkphp3.2 无法加载模块

    当使用thinkphp3.2时候 出现一个无法加载模块的错误的时候 不要慌张,只需要在根目录下的 index.php  加入一句话就可 define('BIND_MODULE','Home'); // ...

  4. 关于CSS特性和选择器

    1层叠  样式冲突的遵循就近原则, 2继承  子代会继承父代的某些属性,例如字体大小,字体颜色 3优先级 !important 权重最高 通配符 * 和 继承的属性 权重为 0 0 0 0 行内样式权 ...

  5. combox绑定数据

    HSMobile_Function.HSMobile_ProjectIDSelect(ProjectID, out dt_Machine);//取出表数据            comboBox_Ma ...

  6. 实验吧简单的sql注入3

    今天早上起来发现有人评论说我没更新实验吧sql注入3,主要是因为前段时间都去做bugku去了 但是重做这道题发现以前的姿势不行了,exp()报错不再溢出,现在不能用这个姿势,所以这里重新整理了一遍思路 ...

  7. linux线程回收

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), v ...

  8. 在知识爆炸的年代如何学习,避免成为PPT架构师

    计算机的发展大体遵循摩尔定律,IT要学的东西越来越多,感觉无从下手 然后发现许多人,专门喜欢说这些名词概念装高大上,脱离一线开发,技术跟风盲目崇拜新的骚东西,比如docker,k8s,微服务,open ...

  9. MySQL数据库(三)--表相关操作(二)之约束条件、关联关系、复制表

    一.约束条件  1.何为约束 除了数据类型以外额外添加的约束 2.约束条件的作用 为了保证数据的合法性,完整性 3.主要的约束条件 NOT NULL # 标识该字段不能为空,默认NULL,可设置NOT ...

  10. 《linux就该这么学》课堂笔记13 网络会话、ssh、远程会话

    1.常见的网卡绑定驱动有三种模式—mode0.mode1和mode6 mode0(平衡负载模式):平时两块网卡均工作,且自动备援,但需要在与服务器本地网卡相连的交换机设备上进行端口聚合来支持绑定技术. ...