平常写自动化用例会写一些前置的 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. RHCE就该这么搞01

    RHCE就该这么搞01 学习之初:快速了解Linux Boot From Hard DiskInstallationUpgradeMore---------------Boot OpsionsStar ...

  2. grafana-zabbix部署和使用

    grafana-zabbix安装 官网安装介绍地址:https://grafana.com/plugins/alexanderzobnin-zabbix-app/installation 下载地址:h ...

  3. a超链接之返回顶部的两种实现方法

    1.通过css实现: 为页面顶部如body或者自己设置的盒子等加上唯一id属性 <body id="id"> .... <a href="#id&quo ...

  4. HttpTool.java 【暂保留】

    备注 在 java tool util 工具类 中已存在 HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; ...

  5. #线程池pool 【参考】

    参考链接 http://www.open-open.com/lib/view/open1415453575730.html 参考配置 http://www.cnblogs.com/linjiqin/a ...

  6. SQL——Hibernate SQL增删改查

    1.查询list数据 实例:user login public String userLogin(){ Session session = HibernateSessionFactory.getSes ...

  7. Linux之常识小结[版本]

    1 linux内核版本 major.minor.patchlevel 主版本号.次版本号.修补次数 次版本号的意义:偶数稳定版,奇数不稳定版 注意:查看系统版本信息 方法1.uname -a 方法2. ...

  8. 【通信】Jave代码中生成url http请求

    /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1& ...

  9. B. Array

    题目链接:http://codeforces.com/contest/224/problem/B 具体大意: 输入n,m. 给你一个区间,让你找某一段区间中包含m个不同的数,并且这段区间中的某一个小区 ...

  10. Android:(本地、可通信的、前台、远程)Service使用全面介绍

    2.具体使用解析 2.1 本地Service 这是最普通.最常用的后台服务Service. 2.1.1 使用步骤 步骤1:新建子类继承Service类 需重写父类的onCreate().onStart ...