平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦。
fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了

调用fixture三种方法

  • 1.函数或类里面方法直接传fixture的函数参数名称
  • 2.使用装饰器@pytest.mark.usefixtures()修饰
  • 3.autouse=True自动使用

用例传fixture参数

方法一:先定义start功能,用例全部传start参数,调用该功能

# content of test_06.py
import time
import pytest @pytest.fixture(scope="function")
def start(request):
print('\n-----开始执行function----') def test_a(start):
print("-------用例a执行-------") class Test_aaa(): def test_01(self, start):
print('-----------用例01--------------') def test_02(self, start):
print('-----------用例02------------') if __name__ == "__main__":
pytest.main(["-s", "test_06.py"])

装饰器usefixtures

方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

# content of test_07.py
import time
import pytest @pytest.fixture(scope="function")
def start(request):
print('\n-----开始执行function----') @pytest.mark.usefixtures("start")
def test_a():
print("-------用例a执行-------") @pytest.mark.usefixtures("start")
class Test_aaa(): def test_01(self):
print('-----------用例01--------------') def test_02(self):
print('-----------用例02------------') if __name__ == "__main__":
pytest.main(["-s", "test_07.py"])

设置autouse=True

方法三、autouse设置为True,自动调用fixture功能

  • start设置scope为module级别,在当前.py用例模块只执行一次,autouse=True自动使用
  • open_home设置scope为function级别,每个用例前都调用一次,自动使用
# content of test_08.py
import time
import pytest # ** 作者:上海-悠悠 QQ交流群:** @pytest.fixture(scope="module", autouse=True)
def start(request):
print('\n-----开始执行moule----')
print('module : %s' % request.module.__name__)
print('----------启动浏览器---------')
yield
print("------------结束测试 end!-----------") @pytest.fixture(scope="function", autouse=True)
def open_home(request):
print("function:%s \n--------回到首页--------" % request.function.__name__) def test_01():
print('-----------用例01--------------') def test_02():
print('-----------用例02------------') if __name__ == "__main__":
pytest.main(["-s", "test_08.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: D:\, inifile:
plugins: metadata-1.7., html-1.19., allure-adaptor-1.7.
collected items ..\..\..\..\..\..\YOYO\peizhi\test_08.py
-----开始执行moule----
module : YOYO.peizhi.test_08
----------启动浏览器---------
function:test_01
--------回到首页--------
-----------用例01--------------
.function:test_02
--------回到首页--------
-----------用例02------------
.------------结束测试----------- ========================== passed in 0.01 seconds ===========================

上面是函数去实现用例,写的class里也是一样可以的

# content of test_09.py
import time
import pytest # ** 作者:上海-悠悠 QQ交流群:** @pytest.fixture(scope="module", autouse=True)
def start(request):
print('\n-----开始执行moule----')
print('module : %s' % request.module.__name__)
print('----------启动浏览器---------')
yield
print("------------结束测试 end!-----------") class Test_aaa():
@pytest.fixture(scope="function", autouse=True)
def open_home(self, request):
print("function:%s \n--------回到首页--------" % request.function.__name__) def test_01(self):
print('-----------用例01--------------') def test_02(self):
print('-----------用例02------------') if __name__ == "__main__":
pytest.main(["-s", "test_09.py"])

fixture之autouse=True的更多相关文章

  1. pytest 15 fixture之autouse=True

    前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦.fixture里面有个参数autouse,默认是Fa ...

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

    conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("&qu ...

  3. pytest文档17-fixture之autouse=True

    前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦. fixture里面有个参数autouse,默认是F ...

  4. Pytest系列(4) - fixture的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面一篇讲了setup.te ...

  5. Pytest学习(四) - fixture的使用

    前言 写这篇文章,整体还是比较坎坷的,我发现有知识断层,理解再整理写出来,还真的有些难. 作为java党硬磕Python,虽然对我而言是常事了(因为我比较爱折腾,哈哈),但这并不能影响我的热情. 执念 ...

  6. Pytest(3)fixture的使用

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

  7. 【pytest系列】- fixture测试夹具详解

    如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html fixture的优势 ​ pyt ...

  8. pytest-conftest.py作用范围

    1.conftest.py解释 conftest.py是pytest框架里面一个很重要的东西,它可以在这个文件里面编写fixture,而这个fixture的作用就相当于我们unittest框架里面的s ...

  9. pytest进阶之fixture

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

随机推荐

  1. thinkphp 静态缓存设置

    'HTML_CACHE_RULES'=> array('ActionName' => array('静态规则', '静态缓存有效期', '附加规则'),'ModuleName(小写)' = ...

  2. 查看 MySQL 数据库的编译参数

    grep CONFIGURE_LINE /app/mysql/bin/mysqlbug 提示:还发现很多人先 cat,在 grep,很不专业,应杜绝. 范例 3: [root@VM-001~]# gr ...

  3. c字符串函数

    1.  bcmp(3) 类ma似于strncmp(3) 但是比较结果不一定是两个字符的ascii码之差. 返回值:相等0,不相等非零(不一定是-1) 2.bcopy(3)类ma似于strncpy(3) ...

  4. __name__='main' 这句代码是什么意思?《读书笔记》

    当我们阅读 别人的python代码都会有 if name == "main"这么一行语句,但却不知道为什么要写这个?有什么用 想知道这段代码什么意思让我们来根据例子来测试一下 我们 ...

  5. JAVA泛型里面各值代表的意义

    Java泛型中的标记符含义:  E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number ...

  6. 阅读之SQL优化

    一.性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的SQL语句, ...

  7. 系统命令模块subprocess

    系统命令 可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 commands.* --废弃,3.x中被移除 ...

  8. SpringBoot + kaptcha 生成、校对 验证码

    1.引入 kaptcha 的 Maven 依赖 <dependency> <groupId>com.github.penggle</groupId> <art ...

  9. vue-cli3.0的配置

    转自 https://www.cnblogs.com/sangzs/p/9543242.html module.exports = { // 基本路径 baseUrl: '/', // 输出文件目录 ...

  10. Cassandra 集群配置

    集群机制 一致性哈希(数据一致性) Token Range Partitoner Gossip协议(流言,无中心获取领导者) 用于在环内节点之间传播状态信息 周期运行,每次在环中随机挑选一个对象节点, ...