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 ...
随机推荐
- puppet报告系统Dashboard部署及配置详解
Puppet Dasshboard是由支持Puppet开发的公司Puppetlabs创建的,是Ruby on Rails程序.可以作为一个ENC(外部节点分类器)以及一个报告工具,并且正在逐渐成为一个 ...
- 数据库SQL语言从入门到精通--Part 4--SQL语言中的模式、基本表、视图
数据库从入门到精通合集(超详细,学习数据库必看) 前言: 使用SQL语言时,要注意SQL语言对大小写并不敏感,一般使用大写.所有符号一定是西文标点符号(虽然是常识,但我还是提一嘴) 1.模式的定义与删 ...
- ST函数(ST表)RMQ O(1)查询 离线
ST算法是基于倍增的动态规划算法. #include<iostream> #include<cstdio> #include<cstdlib> #include&l ...
- Linux安装Git-两种方式详细教程)
一.Git介绍 Git --- The stupid content tracker, 傻瓜内容跟踪器.Linus Torvalds 是这样给我们介绍 Git 的. Git 是用于 Linux内核开发 ...
- 重新认识 Spring IOC
spring IOC 剖析 再品IOC与DI IOC(Inversion of Control) 控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创 建.依赖的代码,反转给容器来帮忙实现. ...
- DoNet:浅淡对delegate的理解
1 前言 C#的相关文档,MSDN上其实已经很详细了,关于delegate的使用可以参 考MSDN上的文档https://msdn.microsoft.com/zh-cn/library/900fyy ...
- 利用Asp.net和Sql Server实现留言板功能
本教程设及到:使用SQL Server查询分析器创建数据库:SQL查询语句常用的一些属性值:触发器创建和使用:存储过程的创建,ASP使用存储过程. 正文: 一.创建数据库: 创建一个feedback数 ...
- MySQL安装(linux)
Centos 安装mysql 安装mariadb yum install mariadb mariadb-server mariadb-devel 安装mysql rpm -qa | grep MyS ...
- 1008 Elevator (20分)
1008 Elevator (20分) 题目: The highest building in our city has only one elevator. A request list is ma ...
- springData表关系:一对一
一.编写两个实体类 1.一对一关系实现:a:使用外键关联 b:使用主键关联,两个表的主键相同 2.外键方案:配置关联关系:两个实体类互相关联,并且在关联的属性上添加一个@OneToOne代表一个对一个 ...