前言

平常写自动化用例会写一些前置的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 # ** 作者:上海-悠悠 QQ交流群:588402570** @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 # ** 作者:上海-悠悠 QQ交流群:588402570** @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交流群:588402570** @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.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\, inifile:
plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 2 items ..\..\..\..\..\..\YOYO\peizhi\test_08.py
-----开始执行moule----
module : YOYO.peizhi.test_08
----------启动浏览器---------
function:test_01
--------回到首页--------
-----------用例01--------------
.function:test_02
--------回到首页--------
-----------用例02------------
.------------结束测试----------- ========================== 2 passed in 0.01 seconds ===========================

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

# content of test_09.py
import time
import pytest # ** 作者:上海-悠悠 QQ交流群:588402570** @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"])

---------------------------------pytest结合selenium自动化完整版-------------------------

全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

作者:上海-悠悠 QQ交流群:874033608

也可以关注下我的个人公众号:yoyoketang

pytest文档17-fixture之autouse=True的更多相关文章

  1. fixture之autouse=True

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

  2. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  3. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  4. pytest文档51-内置fixture之cache使用

    前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...

  5. pytest 15 fixture之autouse=True

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

  6. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  7. pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...

  8. pytest文档43-元数据使用(pytest-metadata)

    前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...

  9. pytest文档42-fixture参数化params

    前言 参数化是自动化测试里面必须掌握的一个知识点,用过 unittest 框架的小伙伴都知道使用 ddt 来实现测试用例的参数化. pytest 测试用例里面对应的参数可以用 parametrize ...

随机推荐

  1. 微信小程序地图模块

    微信小程序的地图模块官方提供的API比较少,详情请见   官方文档 以下为一个示例                               <!--pages/location/locati ...

  2. 一步一步学习IdentityServer3 (5)

    这篇文章介绍下数据持久化问题,官方例子可能都是缓存数据 Client  User Scope 下面介绍下怎么使用数据库持久化 这里需要导入nuget包 :IdentityServer3.EntityF ...

  3. jquery选择里存在特殊字符,需要加双转义字符

    //元素为:<input type="checkbox" value="abc/index" /> //处理选择器转义问题 //去除值 $val = ...

  4. USACO 6.4 Wisconsin Squares

    Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling past ...

  5. EcOS安装

    从ubuntu 拷贝到 centos cd /media ls cd ./sf_EcOS 这个目录就是共享目录,名字可能不一样 cp -r studio.zip /home/ 1. 查看版本 cent ...

  6. MapReduce与批处理------《Designing Data-Intensive Applications》读书笔记14

    之前的文章大量的内容在和大家探讨分布式存储,接下来的章节进入了分布式计算领域.坦白说,个人之前专业的重心侧重于存储,对许多计算的内容理解可能不是和确切,如果文章中的理解有所不妥,愿虚心赐教.本篇将和大 ...

  7. Python并发编程系列之多进程(multiprocessing)

    1 引言 本篇博文主要对Python中并发编程中的多进程相关内容展开详细介绍,Python进程主要在multiprocessing模块中,本博文以multiprocessing种Process类为中心 ...

  8. POJ 3254 & POJ 1185(状压DP入门)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16773   Accepted: 8860 Desc ...

  9. Highmaps网页图表教程之数据标签与标签文本

    Highmaps网页图表教程之数据标签与标签文本 Highmaps数据标签 数据标签用于在地图图表上展现节点对应的数据.数据标签展现数据是静态的,只要节点一加载,数据标签就会出现在节点附近.在High ...

  10. Jquery的方法(一)

    一.文档操作1.内部插入:append(),appendTo(),prepend():2.外部插入:after(),before():3.删除操作:remove(),empty():4.克隆操作:cl ...