平常写自动化用例会写一些前置的 fixture 操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传返个参数,会比较麻烦。
fixture 里面有个参数 autouse,默讣是 Fasle 没开启的,可以设置为True 开启自动使用 fixture 功能,返样用例就丌用每次都去传参了
调用 fixture 三种方法
  1.函数或类里面方法直接传 fixture 的函数参数名称
  2.使用装饰器@pytest.mark.usefixtures()修饰
  3.autouse=True 自动使用

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

import pytest

@pytest.fixture(scope='function')
def start(request):
print('\n=========开始执行function============') def test_a(start):
print('=======用例a==========') class TestCase(): def test_01(self, start):
print('=======用例01=========') def test_02(self, start):
print('=======用例02=========') if __name__=='__main__':
pytest.main() 方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

import pytest

@pytest.fixture(scope='function')
def start(request):
print('==========开始执行function===========') @pytest.mark.usefixtures('start')
def test_a():
print('==============执行用例a===============') @pytest.mark.usefixtures('start')
class TestCase(): def test_01(self):
print('============用例01============') def test_02(self):
print('============用例02============') if __name__=='__main__':
pytest.main()

方法三:autouse 设置为 True,自动调用 fixture 功能
1.start 设置 scope 为 module 级别,在当前.py 用例模块只执行一次,autouse=True 自动使用
2.open_home 设置 scope 为 function 级别,每个用例前都调用一次,自动使用

import time
import pytest @pytest.fixture(scope='module', autouse=True)
def start(request):
print('\n==========开始执行modlue=========')
print(f'module: {request.module.__name__}')
print('========启动浏览器========')
yield
print('===========结束测试===========') @pytest.fixture(scope='function', autouse=True)
def open_home(request):
print(f'function:{request.function.__name__}\n========回到首页======') def test_01():
print('==========用例01==========') def test_02():
print('==========用例02==========') # 第一种方法:类里面写
class TestCase():
@pytest.fixture(scope='function', autouse=True)
def open_home(self, request):
print(f'function:{request.function.__name__}\n========回到首页=======') def test_01(self):
print('==========类用例01============') def test_02(self):
print('==========类用例02============') # 第二种方法:也可以使用 @pytest.mark.usefixtures('open_home') 调用
@pytest.mark.usefixtures('open_home')
class TestCase1(): def test_01(self):
print('==========类用例01============') def test_02(self):
print('==========类用例02============') if __name__=='__main__':
pytest.main()

pytest五:fixture_autouse=True的更多相关文章

  1. 学习-Pytest(五)yield操作

    1.fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作 2.scope="module" 1.fixture参数scope=”modu ...

  2. Pytest系列(7) - skip、skipif跳过用例

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest.mark.sk ...

  3. pytest测试框架 -- assert断言和fixture固件

    一.断言 (1)使用assert语句进行断言 # test_run.py @pytest.mark.assert def test_assert(self): r = requests.get(&qu ...

  4. 2.pytest前后置(固件、夹具)处理

    一.setup/teardown/setup_calss/teardown_class 为什么需要这些功能? 比如:我们执行用例之前,需要做的哪些操作,我们用例执行之后,需要做哪些操作 # 在所有用例 ...

  5. flask-sqlalchemy、pytest 的单元测试和事务自动回滚

    flask-sqlalchemy.pytest 的单元测试和事务自动回滚 使用 flask-sqlalchemy 做数据库时,单元测试可以帮助发现一些可能意想不到的问题,像 delete-cascad ...

  6. pytest进阶之fixture

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

  7. 【Python】【自动化测试】【pytest】

    https://docs.pytest.org/en/latest/getting-started.html#create-your-first-test http://www.testclass.n ...

  8. pytest 学习笔记一:参数化与组织分层

    组织分层: 1.普通方式,和unittest分层类似: setup_module()  # 通常放在类外 setup_class(cls) setup(self) teardown(self) tea ...

  9. pytest的参数化测试

    感觉在单元测试当中可能有用, 但在django这种框架中,用途另一说. import pytest import tasks from tasks import Task def test_add_1 ...

随机推荐

  1. GTF文件

    一.GTF文件格式 Fields must be tab-separated. Also, all but the final field in each feature line must cont ...

  2. 4、JDBC-API

    访问数据库 /** * 在 java.sql 包中有 3 个接口分别定义了对数据库的调用的不同方式: * * Statement * * PrepatedStatement * * CallableS ...

  3. Codeforces 264 B. Good Sequences

    B. Good Sequences time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. 致敬Python 2.7! 致敬unicode函数!

    致敬Python 2.7! 致敬unicode函数! 终于下定决心放弃python 2.7, 拥抱Python 3.x的阵营了. 因为老是被中文编码虐待, 受够了. 同时也把机器里的widows XP ...

  5. C# 与 SQL Server 的数据类型对应关系

    (一)C#与SQL Server 2005(或以下版本): C# C#取值 SQL Server SQL Server取值 System.DateTime samlltime System.Objec ...

  6. Javaweb学习笔记——(四)——————JavaScript基础&DOM目录

    1.案例一:在末尾添加节点 第一个:获取到ul标签 第二部:创建li标签 document.createElement("标签名称")方法 第三步:创建文本 document.cr ...

  7. Winfrom多文档界面实现

    Winfrom多文档界面实现 闲来无事,研究了下比较常用的多文档界面风格,网上找了好多例子,最终实现.记录一下 废话不多说,先上个效果: 默认打开我的桌面,首页不允许关闭,xtraTabPage动态添 ...

  8. 理解django框架中的MTV与MVC模式

    1.Models:一个抽象层,用来构建和操作你的web应用中的数据,模型是你的数据的唯一的.权威的信息源.它包含你所储存数据的必要字段和行为.通常,每个模型对应数据库中唯一的一张表. from dja ...

  9. 修改weblogic的端口

    两种方法可以修改,第一种方法是后台管理界面修改,第二种是配置文件修改,下面分别介绍: 1.后台修改 (1)进入weblogic登陆界面:(默认端口是7001) (2)登陆之后点击服务器----然后管理 ...

  10. Dubbo本地存根

    在远程调用服务提供者的实现之前,如果需要做一些参数验证.缓存.判断.小功能等等,满足要求再调用服务提供者的远程服务,则我们可以通过编写一个本地存根来实现这种功能. (1).在公共项目中或服务消费者项目 ...