pytest文档17-fixture之autouse=True

前言

平常写自动化用例会写一些前置的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"])

** 作者:上海-悠悠 QQ交流群:588402570**

原文地址:https://www.cnblogs.com/yoyoketang/p/9530741.html

时间: 2024-08-30 16:53:53

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

Android官方入门文档[17]构建灵活的UI

Android官方入门文档[17]构建灵活的UI Building a Flexible UI构建灵活的UI This lesson teaches you to1.Add a Fragment to an Activity at Runtime2.Replace One Fragment with Another You should also read?Fragments?Supporting Tablets and Handsets这节课教你1.在运行时新增一个片段给一个活动2.用另一片段

pytest文档3-pycharm运行pytest

前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻烦,所以很有必要学习如何在pycharm里面运行pytest用例 pycharm运行三种方式 1.以xx.py脚本方式直接执行,当写的代码里面没用到unittest和pytest框架时,并且脚本名称不是以test_开头命名的,此时pycharm会以xx.py脚本方式运行 2.当脚本命名为test_x

pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)

前言 pytest提供的很多钩子(Hooks)方法方便我们对测试用例框架进行二次开发,可以根据自己的需求进行改造. 先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果. pytest_runtest_makereport 先看下相关的源码,在_pytest/runner.py下,可以导入之后,点进去查看 from _pytest import runner # 对应源码 def pytest_runtest_make

pytest文档1-环境准备与入门

pytest简介 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍,它具有如下特点: 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考 能够支持简单的单元测试和复杂的功能测试 支持参数化 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败 支持重复执行(rerun)失败的case 支持运行由nose, unittest编写的

pytest文档5-fixture之conftest.py

前言 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了.这就是本篇学习的目的,自定义测试用例的预置条件 fixture优势 1.firture相对于setup和teardown来说应该有以下几点优势 命名方式灵活,不局限于setup和teardown这几个命名 conftest.py 配置里可以实现数

pytest文档14-函数传参和firture传参数request

前言 为了提高代码的复用性,我们在写用例的时候,会用到函数,然后不同的用例去调用这个函数. 比如登录操作,大部分的用例都会先登录,那就需要把登录单独抽出来写个函数,其它用例全部的调用这个登陆函数就行. 但是登录的账号不能写死,有时候我想用账号1去登录,执行用例1,用账号2去登录执行用例2,所以需要对函数传参. 登录函数传参 把登录单独成立,写一个函数,传2个参数user和psw,写用例的时候调用登录函数,输入几组user,psw参数化登录用例 测试用例传参需要用装饰器@pytest.mark.p

pytest文档12-skip跳过用例

前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试. 常见示例是在非Windows平台上跳过仅限Windows的测试,或跳过测试依赖于当前不可用的外部资源(例如数据库). xfail意味着您希望测试由于某种原因而失败. 一个常见的例子是对功能的测试尚未实施,或尚未修复的错误. 当测试通过时尽管预计会失败(标有pytest.mark.xfail),它是一个xpas

pytest文档9-参数化parametrize

前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test_expectation.py # coding:utf-8 import pytest @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), (&

pytest文档13-allure2生成html报告(史上最详细)

前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1.python3.6 2.windows环境 3.pycharm 4.pytest-allure-adaptor 5.allure2.7.0 6.java1.8 ** 作者:上海-悠悠 QQ交流群:588402570** pytest-allure-adaptor下载 pip安装pytest-allu