一、fixture里面有个参数scope,通过scope可以控制fixture的作用范围,根据作用范围大小划分:session>module>class>function,具体作用范围如下:

1.function 函数或者方法级别都会被调用

2.class 类级别调用一次

3.module 模块级别调用一次

4.session 是个多文件调用一次(可以跨 .py 文件调用,每个.py文件就是 module)

例如整个模块有多条测试用例,需要在全部用例执行之前打开浏览器,全部执行完之后去关闭浏览器,打开和关闭操作只执行一次,如果每次都要重新执行打开操作,会非常占用系统资源,这种场景除了 setup——module,teardown_module 可以实现,还可以通过设置模块级别的 fixture装饰器【@pytst.fixture(scope="module")】来实现

scope='module'

fixture 参数 scope=‘module’ ,module作用是整个模块都会生效

#!/usr/bin/env python
# _*_coding: utf-8 _*_
import pytest @pytest.fixture(scope='module')
def open():
print("打开浏览器")
yield print("执行teardown !")
print("最后关闭浏览器") @pytest.mark.usefixtures("open")
def test_search1():
print("test_search1")
raise NameError
pass def test_search2():
print("test_search2")
pass def test_search3():
print("test_search3")
pass

代码解析:@pytest.fixture()如果不写参数,参数默认scope=‘function’。当scope='module'时,在当前 .py脚本里面所有的用例开始前只执行一次。scope巧妙与yield组合使用,相当于setup和teardown方法。还可以使用@pytest.mark.usefixtures装饰器,传入前置函数名作为参数

运行结果如下:

Testing started at 12:03 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture_scope.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture_scope.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items test_fixture_scope.py 打开浏览器
Ftest_search1 test_fixture_scope.py:14 (test_search1)
@pytest.mark.usefixtures("open")
def test_search1():
print("test_search1")
> raise NameError
E NameError test_fixture_scope.py:18: NameError
.test_search2
.test_search3
执行teardown !
最后关闭浏览器
[100%] ================================== FAILURES ===================================
________________________________ test_search1 _________________________________ @pytest.mark.usefixtures("open")
def test_search1():
print("test_search1")
> raise NameError
E NameError test_fixture_scope.py:18: NameError
---------------------------- Captured stdout setup ----------------------------
打开浏览器
---------------------------- Captured stdout call -----------------------------
test_search1
=========================== short test summary info ===========================
FAILED test_fixture_scope.py::test_search1 - NameError
========================= 1 failed, 2 passed in 0.19s ========================= Process finished with exit code 0 Assertion failed Assertion failed

从上面运行结果可以看出scope=‘module’ 和yield结合,相当于setup_module 和teardown_module方法,整个模块运行之前调用open()方法中的yield前面打印输出 的打开浏览器 ,整个运行之后调用了yield后面的打印语句执行teardown与关闭浏览器,yield来唤醒teardown的执行,如果用例出现异常,不影响yield后面teardown执行

可以使用 @pytest.mark.usefixtures装饰器来进行方法的传入

pytest:通过scope控制fixture的作用范围的更多相关文章

  1. Pytest单元测试框架之FixTure基本使用

    前言: 在单元测试框架中,主要分为:测试固件,测试用例,测试套件,测试执行及测试报告: 测试固件不难理解,也就是我们在执行测试用例前需要做的动作和测试执行后的需要做的事情: 比如在UI自动化测试中,我 ...

  2. Pytest(3)fixture的使用

    fixture的优势 Pytest的fixture相对于传统的xUnit的setup/teardown函数做了显著的改进: 命名方式灵活,不局限于 setup 和teardown 这几个命名 conf ...

  3. pytest 4.scope="module"介绍

    前言: 上一篇讲到fixture通过scope参数控制setup级别,不填的时候默认 scope="function",那么接下来就会讲scope="module&quo ...

  4. pytest自动化4:fixture之yield实现teardown

    出处:https://www.cnblogs.com/yoyoketang/p/9401554.html 前言: 上一篇介绍了fixture通过scope参数控制setup级别,我们一起来温故下fix ...

  5. pytest自动化3:fixture之conftest.py实现setup

    出处:https://www.cnblogs.com/yoyoketang/p/9390073.html 前言: 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作 ...

  6. Pytest高级进阶之Fixture

    From: https://www.cnblogs.com/feiyi211/p/6626314.html 一. fixture介绍 fixture是pytest的一个闪光点,pytest要精通怎么能 ...

  7. pytest.5.参数化的Fixture

    From: http://www.testclass.net/pytest/parametrize_fixture/ 背景 继续上一节的测试需求,在上一节里,任何1条测试数据导致断言不通过后测试用例就 ...

  8. pytest初始化与清除fixture(二)

    @pytest.fixture用法 1.导入pytest模块:import pytest 2.调用装饰器函数:@pytest.fixture(callable_or_scope=None,*args, ...

  9. pytest进阶使用【fixture(一)fixture与setup/teardown区别】

    fixture翻译为装置. 我觉得名字是很贴合功能的,可以自由给函数装置上自己想要的功能. 当在说pytest比unitest灵活时,fixture肯定是其中的一个理由. 测试数据的准备和执行以后的数 ...

随机推荐

  1. Hive中的UDF详解

    hive作为一个sql查询引擎,自带了一些基本的函数,比如count(计数),sum(求和),有时候这些基本函数满足不了我们的需求,这时候就要写hive hdf(user defined funati ...

  2. 让你轻松掌握 Python 中的 Hook 钩子函数

    1. 什么是Hook 经常会听到钩子函数(hook function)这个概念,最近在看目标检测开源框架mmdetection,里面也出现大量Hook的编程方式,那到底什么是hook?hook的作用是 ...

  3. python字符串、列表通过值找索引/键

    python透过"值"找字符串和列表中的索引和键. 1 #!usr/bin/env python3 2 #-*- coding=utf-8 -*- 3 4 ''' 5 python ...

  4. 一步步教你:如何用Qemu来模拟ARM系统

    这是道哥的第011篇原创 目录 前言 为什么需要ARM模拟系统 应用程序的开发 系统开发(BSP) Qemu是什么? Qemu的两种模式 Qemu 能做什么?或者说适合做什么? 在 Ubuntu16. ...

  5. sql语句用法大全

    https://www.w3school.com.cn/sql/sql_in.asp .substr函数格式   (俗称:字符截取函数) 格式1: substr(string string, int ...

  6. 为什么Java中lambda表达式不能改变外部变量的值,也不能定义自己的同名的本地变量呢?

    作者:blindpirate链接:https://www.zhihu.com/question/361639494/answer/948286842来源:知乎著作权归作者所有.商业转载请联系作者获得授 ...

  7. [Skill]加速npm与yarn还原

    npm源 使用cnpm alias cnpm="npm --registry=https://registry.npm.taobao.org //或者 npm install -g cnpm ...

  8. 【分享】wdcp服务器管理系统常用维护工具

    wdcp (WDlinux Control Panel) 是一套用PHP开发的Linux服务器管理系统,类似国外流行的cpanel,旨在易于使用和管理Linux服务器,可以在线通过网页管理服务器和虚拟 ...

  9. dhcp分发地址以及静态路由设置

    路由器R1配置: system-view [Huawei]sysname R1 [R1]user-interface console 0 [R1-ui-console0]idle-timeout 0 ...

  10. 深入理解MySQL索引(上)

    简单来说,索引的出现就是为了提高数据查询的效率,就像字典的目录一样.如果你想快速找一个不认识的字,在不借助目录的情况下,那我估计你的找好长时间.索引其实就相当于目录. 几种常见的索引模型 索引的出现是 ...