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 @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的更多相关文章
- pytest 15 fixture之autouse=True
前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦.fixture里面有个参数autouse,默认是Fa ...
- 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 ...
- pytest文档17-fixture之autouse=True
前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦. fixture里面有个参数autouse,默认是F ...
- Pytest系列(4) - fixture的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 前面一篇讲了setup.te ...
- Pytest学习(四) - fixture的使用
前言 写这篇文章,整体还是比较坎坷的,我发现有知识断层,理解再整理写出来,还真的有些难. 作为java党硬磕Python,虽然对我而言是常事了(因为我比较爱折腾,哈哈),但这并不能影响我的热情. 执念 ...
- Pytest(3)fixture的使用
fixture的优势 Pytest的fixture相对于传统的xUnit的setup/teardown函数做了显著的改进: 命名方式灵活,不局限于 setup 和teardown 这几个命名 conf ...
- 【pytest系列】- fixture测试夹具详解
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html fixture的优势 pyt ...
- pytest-conftest.py作用范围
1.conftest.py解释 conftest.py是pytest框架里面一个很重要的东西,它可以在这个文件里面编写fixture,而这个fixture的作用就相当于我们unittest框架里面的s ...
- pytest进阶之fixture
前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...
随机推荐
- 学习-Pytest(三)setup/teardown
1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...
- GetShortPathName函数
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathName" (ByVal ...
- (转) httpclient对cookie的处理
session的保持是通过cookie来维持的,所以如果用户有勾选X天内免登录,这个session 就X天内一直有效,就是通过这个cookie来维护.如果没选X天内免登录,基本上就本次才能保持sess ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(2)|VSCODE配置]
我们今天来配置下vscode+rust. vscode开发rust很方便.但配置有点坑,我们都认为vscode很简单,很完善. 但这里很多同学也出现不少问题. 我们在这里简单记录下win7下配置的过程 ...
- MySQL优化建议与使用规范
适用场景:并发量大.数据量大的互联网业务;可以先阅读必须掌握的MySQL优化指南 一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存页优化使得资源 ...
- Connection refused 排查过程
Connection refused 排查过程 connection refused 排查 起因 今天在连接 rabbitmq 时,报 Connection refused (如下图),借此机会记 ...
- ESP8266-模拟输出(PWM)
PWM(Pulse Width Modulation,脉宽调制),是在保持波的频率不变的同时改变脉宽的技术 首先,我们来理解一下占空比.一个脉冲周期由一个ON周期(VCCC)和一个OFF周期(GND) ...
- Docker(四):Docker常用命令
除过以上我们使用的Docker命令外,Docker还有一些其它常用的命令 拉取docker镜像 docker pull image_name 查看宿主机上的镜像,Docker镜像保存在/var/lib ...
- linux服务器外网内网(双网络)搭建
一共有2台服务器,分别用a,b表示.a双网卡,即有外网也有内网.b只有内网环境.a,b的内网是通过交换机组建.至于外网怎么搭建我就不说了.关键说一说内网是怎么组建的. 如果你对linux不熟悉,对网卡 ...
- 阿里云移动研发平台 EMAS 助力银行业打造测试中台,提升发版效能
随着移动互联网的发展,手机银行凭借低成本.操作简单.不受时间空间约束等优势,正逐步替代传统的网银交易方式.越来越多的银行开始了“业务移动化”转型之路,“手机APP”已经成为企业价值传递和关系维护的关键 ...