pytest之fixture使用详解
简介:
fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进:
1.有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活。
2.按模块化的方式实现,每个fixture都可以互相调用。
3.fixture的范围从简单的单元测试到复杂的功能测试,可以对fixture配置参数,或者跨函数function,类class,模块module或整个测试session范围。
fixture可以当做参数传入
定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None。用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。
# 作者 :admin
import pytest
@pytest.fixture()
def test_01():
a = 5
return a def test_02(test_01):
assert test_01 == 5
print("断言成功") ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0
collected 1 item test_demo_pytest_fixture.py 断言成功
. ========================== 1 passed in 0.17 seconds ===========================
使用多个fixture
如果用例需要用到多个fixture的返回数据,fixture也可以返回一个元祖,list或字典,然后从里面取出对应数据。
# 作者 :admin
import pytest
@pytest.fixture()
def test_01():
a = 5
b = 6
return (a, b) def test_02(test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py .断言成功
[100%] ========================== 1 passed in 0.01 seconds ===========================
Process finished with exit code 0
fixture的作用范围(scope)
ixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function
-function:每一个函数或方法都会调用
-class:每一个类调用一次,一个类中可以有多个方法
-module:每一个.py文件调用一次,该文件内又有多个function和class
-session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module
代码示例:
# 作者 :admin
import pytest
@pytest.fixture(scope="class")
def test_01():
a = 5
b = 6
return (a, b) class TestNum:
def test_02(self,test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py .断言成功
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0
调用fixture的三种方法
1.函数或类里面方法直接传fixture的函数参数名称
注释:代码详见上图
2.使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例
# 作者 :admin
import pytest
@pytest.fixture(scope="class")
def test_01():
a = 5
b = 6
return (a, b) @pytest.mark.usefixtures("test_01")
class TestNum:
def test_02(self,test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py .断言成功
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0
3.叠加usefixtures
如果一个方法或者一个class用例想要同时调用多个fixture,可以使用@pytest.mark.usefixture()进行叠加。注意叠加顺序,先执行的放底层,后执行的放上层。
# 作者 :admin
import pytest
@pytest.fixture(scope="class")
def test_01():
a = 5
b = 6
return (a, b) @pytest.fixture(scope="class")
def test_02():
print("你是第二个执行") @pytest.mark.usefixtures("test_02")
@pytest.mark.usefixtures("test_01")
class TestNum:
def test_03(self,test_01):
a = test_01[0]
b = test_01[1]
assert a < b
print("断言成功") Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:\daima\Project_test\test_demo ============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
rootdir: E:\daima\Project_test\test_demo, inifile:
plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item test_demo_pytest_fixture.py 你是第二个执行
.断言成功
[100%] ========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0
pytest之fixture使用详解的更多相关文章
- 【pytest系列】- fixture测试夹具详解
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html fixture的优势 pyt ...
- Pytest fixture及conftest详解
前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...
- pytest框架fixture的使用
fixture可以当做参数传入 定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开.fixtu ...
- 【单元测试】NUint使用详解及Visual Studio配置
阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Visual Nunit 2010 NUnit Test ...
- Python安装、配置图文详解(转载)
Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...
- 【和我一起学python吧】Python安装、配置图文详解
Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...
- NUint使用详解及Visual Studio配置
NUint使用详解及Visual Studio配置 阅读目录 什么是单元测试? 为什么使用单元测试? NUint使用详解: 示例 属性 断言 简单测试 VS配置: External Tools Vis ...
- Python2.7字符编码详解
目录 Python2.7字符编码详解 声明 一. 字符编码基础 1.1 抽象字符清单(ACR) 1.2 已编码字符集(CCS) 1.3 字符编码格式(CEF) 1.3.1 ASCII(初创) 1.3. ...
- NUnit的Attribute使用详解
NUNIT使用详解(一) 2008/08/26 11:40 NUnit是一个单元测试框架,专门针对于.NET来写的,它是是xUnit的一员.NUnit完全由C#语言来编写,并且编写时充分利用了许多.N ...
随机推荐
- CentOS+Subversion 配置Linux 下 SVN服务器
1.安装#yum install subversion测试安装是否成功:#svnserve –version 回车显示版本说明安装成功 2.配置 ·建立版本库 #mkdir /opt/svnd ...
- Microsoft Dynamics CRM 2015 服务器系统的性能维护,追踪, 也可以用到任务管理器哟...
Microsoft Dynamics CRM 2015 的追踪是一个很有用的function,它能为我们的CRM调试,评估 提供有价值的信息:我们可以用window的性能监控工具来了解CRM的性能状态 ...
- MySQL 索引、视图
1.索引 什么是索引 一个索引是存储在表中的数据结构,索引在表的列名上创建.索引中包含了一个列的值,这些值保存在一个数据结构中 索引优缺点 索引大大提高了查询速度 会降低更新表的速度,如对表进行INS ...
- Ant 环境安装
1.下载安装 Ant,配置环境变量 进入 http://ant.apache.org/bindownload.cgi 下载 Ant 配置环境变量 新建 ANT_HOME 配置 Path 环境变量 配置 ...
- 【Java8新特性】一张图带你领略Java8有哪些新特性
写在前面 很多小伙伴留言说,冰河你能不能写一些关于Java8的文章呢,看书看不下去,看视频进度太慢.好吧,看到不少读者对Java8还是比较陌生的,那我就写一些关于Java8的文章吧,希望对大家有所帮助 ...
- mysql查询表内所有字段名和备注
select distinct column_name as 字段名,column_comment as 字段备注 from information_schema.columns where tabl ...
- vue2.0那些坑之使用elementUI后v-on:click事件不生效问题
最近在维护vue2.0的项目,遇到了不少坑,在这里说下引用elementui之后,使用v-on:click绑定点击事件无效的情况,如下图: 我想阻止冒泡事件,发现无效.这里将@click换成了@cli ...
- React Native超简单完整示例-tabs、页面导航、热更新、用户行为分析
初学React Native,如果没有人指引,会发现好多东西无从下手,但当有人指引后,会发现其实很简单.这也是本人写这篇博客的主要原因,希望能帮到初学者. 本文不会介绍如何搭建开发环境,如果你还没有搭 ...
- 【MySQL基础总结】索引的使用
索引的使用 概述 1.索引由数据库中一列或多列组合而成,其作用是提高对表中数据的查询速度 2.索引的优点是可以提高检索数据的速度 3.缺点是创建和维护索引需要耗费时间 4.所以索引可以提高查询速度,减 ...
- Mysql常用sql语句(九)- like 模糊查询
测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html 前言 ...