Pytest高级进阶之Fixture

From: https://www.jianshu.com/p/54b0f4016300

一. fixture介绍

fixture是pytest的一个闪光点,pytest要精通怎么能不学习fixture呢?跟着我一起深入学习fixture吧。其实unittest和nose都支持fixture,但是pytest做得更炫。
fixture是pytest特有的功能,它用pytest.fixture标识,定义在函数前面。在你编写测试函数的时候,你可以将此函数名称做为传入参数,pytest将会以依赖注入方式,将该函数的返回值作为测试函数的传入参数。
fixture有明确的名字,在其他函数,模块,类或整个工程调用它时会被激活。
fixture是基于模块来执行的,每个fixture的名字就可以触发一个fixture的函数,它自身也可以调用其他的fixture。
我们可以把fixture看做是资源,在你的测试用例执行之前需要去配置这些资源,执行完后需要去释放资源。比如module类型的fixture,适合于那些许多测试用例都只需要执行一次的操作。
fixture还提供了参数化功能,根据配置和不同组件来选择不同的参数。
fixture主要的目的是为了提供一种可靠和可重复性的手段去运行那些最基本的测试内容。比如在测试网站的功能时,每个测试用例都要登录和退出,利用fixture就可以只做一次,否则每个测试用例都要做这两步也是冗余。

下面会反复提高Python的Module概念,Python中的一个Module对应的就是一个.py文件。其中定义的所有函数或者是变量都属于这个Module。这个Module 对于所有函数而言就相当于一个全局的命名空间,而每个函数又都有自己局部的命名空间。

二. Fixture基础实例入门

把一个函数定义为Fixture很简单,只能在函数声明之前加上“@pytest.fixture”。其他函数要来调用这个Fixture,只用把它当做一个输入的参数即可。
test_fixture_basic.py

import pytest@pytest.fixture()
def before(): print '\nbefore each test' def test_1(before): print 'test_1()' def test_2(before): print 'test_2()' assert 0 

下面是运行结果,test_1和test_2运行之前都调用了before,也就是before执行了两次。默认情况下,fixture是每个测试用例如果调用了该fixture就会执行一次的。

C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -s test_fixture_basic.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 2 items test_fixture_basic.py::test_1 before each test test_1() PASSED test_fixture_basic.py::test_2 before each test test_2() FAILED ================================== FAILURES =================================== ___________________________________ test_2 ____________________________________ before = None def test_2(before): print 'test_2()' > assert 0 E assert 0 test_fixture_basic.py:12: AssertionError ===================== 1 failed, 1 passed in 0.23 seconds ====================== 

如果你的程序出现了下面的错误,就是开始忘记添加‘import pytest',所以不要忘记罗。

=================================== ERRORS ====================================
_________________ ERROR collecting test_fixture_decorator.py __________________
test_fixture_decorator.py:2: in <module> @pytest.fixture() E NameError: name 'pytest' is not defined !!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!! =========================== 1 error in 0.36 seconds =========================== 

三. 调用fixture的三种方式

1. 在测试用例中直接调用它,例如第二部分的基础实例。

2. 用fixture decorator调用fixture

可以用以下三种不同的方式来写,我只变化了函数名字和类名字,内容没有变。第一种是每个函数前声明,第二种是封装在类里,类里的每个成员函数声明,第三种是封装在类里在前声明。在可以看到3中不同方式的运行结果都是一样。
test_fixture_decorator.py

import pytest@pytest.fixture()
def before(): print('\nbefore each test') @pytest.mark.usefixtures("before") def test_1(): print('test_1()') @pytest.mark.usefixtures("before") def test_2(): print('test_2()') class Test1: @pytest.mark.usefixtures("before") def test_3(self): print('test_1()') @pytest.mark.usefixtures("before") def test_4(self): print('test_2()') @pytest.mark.usefixtures("before") class Test2: def test_5(self): print('test_1()') def test_6(self): print('test_2()') 

运行结果如下,和上面的基础实例的运行效果一样。

C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -s test_fixture_decorator.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 6 items test_fixture_decorator.py::test_1 before each test test_1() PASSED test_fixture_decorator.py::test_2 before each test test_2() PASSED test_fixture_decorator.py::Test1::test_3 before each test test_1() PASSED test_fixture_decorator.py::Test1::test_4 before each test test_2() PASSED test_fixture_decorator.py::Test2::test_5 before each test test_1() PASSED test_fixture_decorator.py::Test2::test_6 before each test test_2() PASSED ========================== 6 passed in 0.10 seconds =========================== 

3. 用autos调用fixture

fixture decorator一个optional的参数是autouse, 默认设置为False。
当默认为False,就可以选择用上面两种方式来试用fixture。
当设置为True时,在一个session内的所有的test都会自动调用这个fixture。
权限大,责任也大,所以用该功能时也要谨慎小心。

import time
import pytest@pytest.fixture(scope="module", autouse=True)
def mod_header(request): print('\n-----------------') print('module : %s' % request.module.__name__) print('-----------------') @pytest.fixture(scope="function", autouse=True) def func_header(request): print('\n-----------------') print('function : %s' % request.function.__name__) print('time : %s' % time.asctime()) print('-----------------') def test_one(): print('in test_one()') def test_two(): print('in test_two()') 

从下面的运行结果,可以看到mod_header在该module内运行了一次,而func_header对于每个test都运行了一次,总共两次。该方式如果用得好,还是可以使代码更为简洁。
但是对于不熟悉自己组的测试框架的人来说,在pytest里面去新写测试用例,需要去了解是否已有一些fixture是module或者class级别的需要注意。

C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -s test_fixture_auto.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 2 items test_fixture_auto.py::test_one ----------------- module : test_fixture_auto ----------------- ----------------- function : test_one time : Sat Mar 18 06:56:54 2017 ----------------- in test_one() PASSED test_fixture_auto.py::test_two ----------------- function : test_two time : Sat Mar 18 06:56:54 2017 ----------------- in test_two() PASSED ========================== 2 passed in 0.03 seconds =========================== 

四. fixture scope

function:每个test都运行,默认是function的scope
class:每个class的所有test只运行一次
module:每个module的所有test只运行一次
session:每个session只运行一次

比如你的所有test都需要连接同一个数据库,那可以设置为module,只需要连接一次数据库,对于module内的所有test,这样可以极大的提高运行效率。

五. fixture 返回值

在上面的例子中,fixture返回值都是默认None,我们可以选择让fixture返回我们需要的东西。如果你的fixture需要配置一些数据,读个文件,或者连接一个数据库,那么你可以让fixture返回这些数据或资源。

如何带参数
fixture还可以带参数,可以把参数赋值给params,默认是None。对于param里面的每个值,fixture都会去调用执行一次,就像执行for循环一样把params里的值遍历一次。
test_fixture_param.py

import pytest@pytest.fixture(params=[1, 2, 3])
def test_data(request): return request.param def test_not_2(test_data): print('test_data: %s' % test_data) assert test_data != 2 

可以看到test_not_2里面把用test_data里面定义的3个参数运行里三次。

C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -s test_fixture_param.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe cachedir: .cache metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}} rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile: plugins: metadata-1.3.0, html-1.14.2 collected 3 items test_fixture_param.py::test_not_2[1] test_data: 1 PASSED test_fixture_param.py::test_not_2[2] test_data: 2 FAILED test_fixture_param.py::test_not_2[3] test_data: 3 PASSED ================================== FAILURES =================================== ________________________________ test_not_2[2] ________________________________ test_data = 2 def test_not_2(test_data): print('test_data: %s' % test_data) > assert test_data != 2 E assert 2 != 2 test_fixture_param.py:9: AssertionError ===================== 1 failed, 2 passed in 0.24 seconds ====================== 

转载于:https://www.cnblogs.com/Raul2018/p/10340452.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql 慢日志报警_一则MySQL慢日志监控误报的问题分析

之前因为各种原因&#xff0c;有些报警没有引起重视&#xff0c;最近放假马上排除了一些潜在的人为原因&#xff0c;发现数据库的慢日志报警有些奇怪&#xff0c;主要表现是慢日志报警不属实&#xff0c;收到报警的即时通信提醒后&#xff0c;隔一会去数据库里面去排查&#xf…

用css实现自定义虚线边框

开发产品功能的时候ui往往会给出虚线边框的效果图&#xff0c;于是乎&#xff0c;我们往往第一时间想到的是用css里的border&#xff0c;可是border里一般就提供两种效果&#xff0c;dashed或者dotted&#xff0c;ui这时就不满意了&#xff0c;说虚线太密了。废话不多说&#x…

无限复活服务器,绝地求生无限复活模式怎么玩 无限复活新手教程

相信不少的绝地求生玩家们最近都听说了其无限复活模式吧?因此肯定想要知道这种模式究竟该怎么玩&#xff0c;所以下面就来为各位带来此玩法的攻略相关&#xff0c;希望各位在看了如下的内容之后恩呢狗狗了解到新手教程攻略一览。“War”模式的设定以及玩法规则如下&#xff1a…

lua math.random()

math.random([n [,m]]) 用法&#xff1a;1.无参调用&#xff0c;产生[0, 1)之间的浮点随机数。 2.一个参数n&#xff0c;产生[1, n]之间的整数。 3.两个参数&#xff0c;产生[n, m]之间的整数。 math.randomseed(n) 用法&#xff1a;接收一个整数n作为随即序列的种子。 例&…

零基础学习ruby_学习Ruby:从零到英雄

零基础学习ruby“Ruby is simple in appearance, but is very complex inside, just like our human body.” — Matz, creator of the Ruby programming language“ Ruby的外观很简单&#xff0c;但是内部却非常复杂&#xff0c;就像我们的人体一样。” — Matz &#xff0c;R…

windows同时启动多个微信

1、创建mychat.bat文件(文件名任意)&#xff0c;输入以下代码&#xff0c;其中"C:\Program Files (x86)\Tencent\WeChat\"为微信的安装路径。以下示例为同时启动两个微信 start/d "C:\Program Files (x86)\Tencent\WeChat\" Wechat.exe start/d "C:\P…

mysql date time year_YEAR、DATE、TIME、DATETIME和TIMESTAMP详细介绍[MySQL数据类型]

为了方便在数据库中存储日期和时间&#xff0c;MySQL提供了表示日期和时间的数据类型&#xff0c;分别是YEAR、DATE、TIME、DATETIME和TIMESTAMP。下面列举了这些MSL中日期和时间数据类型所对应的字节数、取值范围、日期格式以及零值。从上图中可以看出&#xff0c;每种日期和时…

九度oj 题目1380:lucky number

题目描述&#xff1a;每个人有自己的lucky number&#xff0c;小A也一样。不过他的lucky number定义不一样。他认为一个序列中某些数出现的次数为n的话&#xff0c;都是他的lucky number。但是&#xff0c;现在这个序列很大&#xff0c;他无法快速找到所有lucky number。既然这…

安装Tengine

1.安装VMware2.安装CentOS6.53.配置网络a.修改 /etc/sysconfig/network-scripts/ifcfg-eth0配置文件,添加如下内容DEVICEeth0HWADDR00:0C:29:96:01:6BTYPEEthernetUUID41cbd943-024b-4341-ac7a-e4d2142b4938ONBOOTyesNM_CONTROLLEDyesBOOTPROTOnoneIPADDRxxx.xxx.x.xxx#例如:IP…

node seneca_使用Node.js和Seneca编写国际象棋微服务,第2部分

node seneca处理新需求而无需重构 (Handling new requirements without refactoring) Part 1 of this series talked about defining and calling microservices using Seneca. A handful of services were created to return all legal moves of a lone chess piece on a ches…

【OCR技术系列之八】端到端不定长文本识别CRNN代码实现

CRNN是OCR领域非常经典且被广泛使用的识别算法&#xff0c;其理论基础可以参考我上一篇文章&#xff0c;本文将着重讲解CRNN代码实现过程以及识别效果。 数据处理 利用图像处理技术我们手工大批量生成文字图像&#xff0c;一共360万张图像样本&#xff0c;效果如下&#xff1a;…

mysql 修改字段类型死锁_mysql数据库死锁的产生原因及解决办法

数据库和操作系统一样&#xff0c;是一个多用户使用的共享资源。当多个用户并发地存取数据 时&#xff0c;在数据库中就会产生多个事务同时存取同一数据的情况。若对并发操作不加控制就可能会读取和存储不正确的数据&#xff0c;破坏数据库的一致性。加锁是实现数据库并 发控制…

openwrt无盘服务器,搭建基于 OpenWrt/gPXE/iSCSI 的 Windows 无盘工作站

本文要介绍的是如何在 OpenWrt 平台上面搭建无盘工作站服务器以及 Windows 的 iSCSI 部署。当然&#xff0c;由于 OpenWrt 也可以算得上一种 Linux 发行版了&#xff0c;所以本文所介绍的一些方法&#xff0c;在其它 Linux 发行版上面仍有一定的参考价值。整个过程大概分为以下…

Ralink5350开发环境搭建

一、安装虚拟机(Oracle VM VirtualBox 或 VMware Workstation) 二、在虚拟机中安装linux操作系统&#xff08;当前使用的是Ubuntu1204桌面版&#xff09; 三、配置linux相关服务 安装、配置、启动ftp服务apt-get install vsftpd 改动 vsftpd 的配置文件 /etc/vsftpd.conf,将以…

figma下载_Figma重新构想的团队库

figma下载一个新的&#xff0c;功能更强大的界面&#xff0c;用于在整个组织中共享组件 (A new, more powerful interface for sharing Components across your organization) The Team Library in Figma is a set of shared Components across all files in a Team. Component…

boost python导出c++ map_使用Boost生成的Python模块:与C++签名不匹配

我正在使用名为Mitsuba的软件。它附带了一个用Boost包装的Python实现。 Python中的这一行&#xff1a;使用Boost生成的Python模块&#xff1a;与C签名不匹配scene SceneHandler.loadScene(fileResolver.resolve("model.xml"), paramMap)产生一个错误。根据文档&…

CSU-1982 小M的移动硬盘

CSU-1982 小M的移动硬盘 Description 最近小M买了一个移动硬盘来储存自己电脑里不常用的文件。但是他把这些文件一股脑丢进移动硬盘后&#xff0c;觉得这些文件似乎没有被很好地归类&#xff0c;这样以后找起来岂不是会非常麻烦&#xff1f; 小M最终决定要把这些文件好好归类&a…

杜比服务器系统安装教程,win10杜比音效如何安装?win10安装杜比音效的详细教程...

杜比音效想必大家都不陌生&#xff0c;听歌或者看电影开启杜比音效可以给人一种身临其境的感觉。不少朋友都升级了win10系统却不知道如何安装杜比音效&#xff1f;如何为自己的系统安装杜比音效呢&#xff1f;感兴趣的小伙伴请看下面的操作步骤。win10安装杜比音效的方法&#…

剑指Offer_52_正则表达式匹配

题目描述 请实现一个函数用来匹配包括.和的正则表达式。模式中的字符.表示任意一个字符&#xff0c;而表示它前面的字符可以出现任意次&#xff08;包含0次&#xff09;。 在本题中&#xff0c;匹配是指字符串的所有字符匹配整个模式。例如&#xff0c;字符串"aaa"与…

分布式系统开发注意点_分布式系统注意事项

分布式系统开发注意点by Shubheksha通过Shubheksha 分布式计算概述&#xff1a;分布式系统如何工作 (Distributed Computing in a nutshell: How distributed systems work) This post distills the material presented in the paper titled “A Note on Distributed Systems”…