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

fixture(scope="function", params=None, autouse=False, ids=None, name=None):
"""使用装饰器标记fixture的功能
** 作者:上海-悠悠 QQ交流群:**
可以使用此装饰器(带或不带参数)来定义fixture功能。 fixture功能的名称可以在以后使用
引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。
测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。 :arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session". :arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它 :arg autouse: 如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture :arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成 :arg name: fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
“fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')“”。
  • function 每一个函数或方法都会调用
  • class 每一个类调用一次,一个类可以有多个方法
  • module,每一个.py文件调用一次,该文件内又有多个function和class
  • session 是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

scope="function"

@pytest.fixture()如果不写参数,默认就是scope="function",它的作用范围是每个测试用例来之前运行一次,销毁代码在测试用例运行之后运行。

import pytest

@pytest.fixture()
def first():
print("\n获取用户名")
a = "yoyo"
return a @pytest.fixture(scope="function")
def sencond():
print("\n获取密码")
b = ""
return b def test_1(first):
'''用例传fixture'''
print("测试账号:%s" %first)
assert first == "yoyo" def test_2(sencond):
'''用例传fixture'''
print("测试密码:%s" %sencond)
assert sencond == "" if __name__ == "__main__":
pytest.main(["-s", "test_fixture7.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture7.py
获取用户名
测试账号:yoyo
.
获取密码
测试密码:
. ========================== passed in 0.01 seconds ===========================

用例放到类里面也一样

import pytest

@pytest.fixture()
def first():
print("\n获取用户名")
a = "yoyo"
return a @pytest.fixture(scope="function")
def sencond():
print("\n获取密码")
b = ""
return b class TestCase():
def test_1(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" def test_2(self, sencond):
'''用例传fixture'''
print("测试密码:%s" % sencond)
assert sencond == "" if __name__ == "__main__":
pytest.main(["-s", "test_fixture7.py"])

scope="class"

fixture为class级别的时候,如果一个class里面有多个用例,都调用了此fixture,那么此fixture只在该class里所有用例开始前执行一次

import pytest

@pytest.fixture(scope="class")
def first():
print("\n获取用户名,scope为class级别只运行一次")
a = "yoyo"
return a class TestCase():
def test_1(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" def test_2(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_fixture9.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture9.py
获取用户名,scope为class级别只运行一次
测试账号:yoyo
.测试账号:yoyo
. ========================== passed in 0.13 seconds ===========================

scope="module"

fixture为module级别时,在当前.py脚本里面所有用例开始前只执行一次

import pytest

@pytest.fixture(scope="module")
def first():
print("\n获取用户名,scope为module级别当前.py模块只运行一次")
a = "yoyo"
return a def test_1(first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" class TestCase():
def test_2(self, first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_fixture10.py"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture10.py
获取用户名,scope为module级别当前.py模块只运行一次
测试账号:yoyo
.测试账号:yoyo
. ========================== passed in 0.14 seconds ===========================

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 first():
print("\n获取用户名,scope为session级别多个.py模块只运行一次")
a = "yoyo"
return a

test_fixture11.py和test_fixture12.py用例脚本

# test_fixture11.py

import pytest
def test_1(first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_fixture11.py"]) # test_fixture12.py
import pytest def test_2(first):
'''用例传fixture'''
print("测试账号:%s" % first)
assert first == "yoyo" if __name__ == "__main__":
pytest.main(["-s", "test_fixture12.py"])

如果想同时运行test_fixture11.py和test_fixture12.py,在cmd执行

pytest -s test_fixture11.py test_fixture12.py
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture11.py . [ %]
test_fixture12.py . [%] ========================== passed in 0.03 seconds =========================== D:\YOYO\fixt>pytest -s test_fixture11.py test_fixture12.py
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\YOYO\fixt, inifile:
plugins: rerunfailures-4.1, metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items test_fixture11.py
获取用户名,scope为session级别多个.py模块只运行一次
测试账号:yoyo
.
test_fixture12.py 测试账号:yoyo
. ========================== passed in 0.03 seconds ===========================

fixture作用范围的更多相关文章

  1. pytest进阶之fixture

    前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...

  2. pytest文档24-fixture的作用范围(scope)

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

  3. Pytest【定制fixture】

    在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集.配置测试前系统的初始化状态.为批量测 ...

  4. pytest框架: fixture之conftest.py

    原文地址:https://blog.csdn.net/BearStarX/article/details/101000516 一.fixture优势1.fixture相对于setup和teardown ...

  5. 【PYTEST】第四章Fixture

    知识点: 利用fixture共享数据 conftest.py共享fixture 使用多个fixture fixture作用范围 usefixture 重命名 1. 利用fixture共享数据 test ...

  6. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  7. 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)

    目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...

  8. pytest--fixture

    前戏 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...

  9. pytest学习纪要123-针对经常用到的内容详实记录

    pytest123 本文主要参考:https://www.cnblogs.com/yoyoketang/tag/pytest 如有侵权,请站内联系我 目录 pytest123 1.setup和tear ...

随机推荐

  1. CentOS 安装 oralce Java的图形出错: libXtst.so.6: cannot open shared object file: No such file or directory

    问题类似: shared object file: No such file or directory occurred..java.lang.UnsatisfiedLinkError: /tmp/O ...

  2. 次小生成树(LCA倍增)

    算法: 求出MST之后枚举每条在MST之外的边 连上之后会出现环 找到环中除加上的边之外权值最大的边 删除该边之后得到一颗新树 做法: 利用LCA倍增地维护最小生成树上两点之间的最大边权 每次枚举在M ...

  3. Django学习系列9:接着修改首页

    现在的功能测试还是失败的,继续修改代码,让其通过.因为HTML现在保存在模板中,可以尽情修改,无需编写额外的单元测试.我们需要一个<h1>元素 修改:lists/templates/hom ...

  4. WinMain lpCmdLine

    int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine, int){ //命令行参数 TCHAR pCommandLine[2 ...

  5. ffmpeg函数04__v_register_output_format()

    注册复用器,编码器等的函数av_register_all() 注册编解码器avcodec_register_all() 注册复用器的函数是av_register_output_format(). 注册 ...

  6. Spring整合MongoDB(转)

    1.认识Spring Data MongoDB 之前还的确不知道Spring连集成Nosql的东西都实现了,还以为自己又要手动封装一个操作MongoDB的API呢,结果就发现了Spring Data ...

  7. js 循环post

    var url_s=["h/a","h/b","h/c"]; function post_test(url,callback) { //请求 ...

  8. TTTTTTTTTTT POJ 2749 修牛棚 2-Sat + 路径限制 变形

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7019   Accepted: 2387 De ...

  9. 捕获有问题的SQL

  10. 「SCOI2015」小凸玩矩阵

    题目链接 问题分析 题目给了充足的暗示,我们只需要二分答案然后跑匈牙利即可.要相信匈牙利的速度 参考程序 #include <bits/stdc++.h> using namespace ...