pytest.fixture的初始化清除操作

需要导入模块pytest

使用装饰器:pytest.fixture(scope=‘function‘,autouse=False)

fixture()函数参数解释说明

  fixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function

  -function:每一个函数或方法都会调用

  -class:每一个类调用一次,一个类中可以有多个方法

  -module:每一个.py文件调用一次,该文件内又有多个function和class

  -session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

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.

    :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``.

    :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.

    :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.

    :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()函数参数说明

初始化清除操作:

  方式一:

  def simple_setup():

    清除操作

    yield

    清除操作or清除操作函数

  方式二:

    def simple_setup(request):

      清除操作

      request.addfinalizer(清除操作函数)

例子:

   参数中设置autouse=True ,则每个测试用例都执行初始化清除操作

@pytest.fixture(scope=‘function‘)
def simple_request():
    print(‘开始初始化‘)
    yield
    after_test()
def after_test():
    print(‘开始清除‘)
# autoust=False,添加初始化操作函数名作为参数,就会执行初始化操作,不加则不执行
def test_request(simple_request):
    print(‘测试用例1,开始执行测试‘)
    assert 1 == 1
def test_request2():
    print(‘测试用例2,开始执行测试‘)
    assert 1 == 1
@pytest.fixture(scope=‘function‘)
def simple_request(request):
    print(‘开始初始化‘)
    request.addfinalizer(after_test)

def after_test():
    print(‘开始清除‘)

# autoust=False,添加初始化操作函数名作为参数,就会执行初始化操作,不加则不执行
def test_request(simple_request):
    print(‘测试用例1,开始执行测试‘)
    assert 1 == 1

def test_request2():
    print(‘测试用例2,开始执行测试‘)
    assert 1 == 1

执行结果:

  

  

原文地址:https://www.cnblogs.com/aiyumo/p/12401938.html

时间: 2024-08-30 05:13:50

pytest.fixture的初始化清除操作的相关文章

数据结构开发(23):二叉树中结点的查找、插入、删除与清除操作

0.目录 1.二叉树中结点的查找操作 2.二叉树中结点的插入操作 3.二叉树中结点的删除操作 4.二叉树中结点的清除操作 5.小结 1.二叉树中结点的查找操作 查找的方式: 基于数据元素值的查找 BTreeNode<T>* find(const T& value) const 基于结点的查找 BTreeNode<T>* find(TreeNode<T>* node) const 树中数据元素和结点的查找: 基于数据元素值的查找: 定义功能:find(node,

pytest fixture中scope试验,包含function、module、class、session、package

上图是试验的目录结构 conftest.py:存放pytest fixture的文件 1 import uuid 2 import pytest 3 4 @pytest.fixture(scope="module") 5 def test_module(): 6 return "module"+str(uuid.uuid4()) 7 8 @pytest.fixture(scope="class") 9 def test_class(): 10 r

pytest 用 @pytest.mark.usefixtures(&quot;fixtureName&quot;)或@pytest.fixture(scope=&quot;function&quot;, autouse=True)装饰,实现类似setup和TearDown的功能

conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("") print("class-begin") yield print("class-end") test_autouse.py 1 import pytest 2 3 4 @pytest.mark.usefixtures("class_auto"

RF之条件判断、初始化清除-4

条件判断:        rf中用run keyword if 关键字做条件判断,以此来达到类似在python中if ...else...条件判断的功能. 注意:ELSE IF一定都是大写的,不然运行后会报错.        RF中解决太长的问题:可以用下一行 前面加三个省略号,在测试用例中,下一行的省略号前面必须留一个以上的空单元格. *** Test Cases *** 条件判断1 [Documentation]       run keyword if     需要注意的是:语法严格 in

线性表的顺序列表的定义、初始化等操作

#include <stdio.h>#include <stdlib.h>#define OK 1#define ERR 0#define MAXSIZE 100//定义顺序存储结构 typedef struct list{ int elem[MAXSIZE]; int last;}SeqList; //初始化线性表SeqList *InitList(){ SeqList *L; L = (SeqList *)malloc(sizeof(SeqList)); L->last

浅谈swift中的那些类,结构以及初始化的操作

首先呢,我们先声明一个类 class Parent { //声明一个属性 var p1: String = "abc" //声明一个方法 func m() { print("parent m") } //声明一个静态的方法 final func n(){ } } 然后我们new一个Parent类(注意了,在swift中是没有new的,如果想new 一个的话, 直接调用该类就可以了) var par = Parent() 调用parent的方法和属性 par.m()

线性表的单链表的定义、初始化等操作

#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERR 0 #define MAXSIZE 100 typedef int ElemType; //定义 typedef struct Node { ElemType data; struct Node *next; }Node,*LinkedList; //初始化 LinkedList LinkedListInit() { Node *L; L = (Node

svn初始化仓库操作

一.简介 SVN是Subversion的简称,是一个开放源代码的版本控制系统. 二.安装 1.官网下载,官网地址:https://tortoisesvn.net 2.安装检查环境 #查看svn版本信息 svn --version 三.初始化仓库 1.创建一个版本库, svnadmin create 版本库路径 例如我在D:\moy\moysvn创建一个仓库,则: svnadmin create D:\moy\moysvn 2.启动svn版本库, svnserve –d –r 版本库路径 以我为例

mysql 安装成功以及第一次安装成功初始化密码操作

一 把文件解压到一个目录下 这是解压后的目录 将my.ini文件考进去 双击打开my.ini 找到这两行更改成自己的解压路径保存 右键此电脑属性 找到高级系统设置配置环境变量 环境变量   新建 变量值是解压文件的路径 Path 单击path编辑 新建 之后 用管理员身份打开cmd进入文件路径 打开命令行窗口,在里面输入:mysqld --install 这个命令是安装服务, 执行完后, 提示英文的成功, 这时候你可以在你的 windows 服务中看到  MySQL 的服务,移除服务命令为:my