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 ...
随机推荐
- showModalDialog使用讲解
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog ...
- INTERVIEW #3
菊厂的面试本来没打算记录,因为当时投的是非技术岗(技术支持).为了全面,就寥做记录. 菊厂的面试因为有口头保密协议,所以不能透露具体题目. 0 群面 简历通过筛选后,会有短信通知去面试. 非技术岗第一 ...
- HDU 1874 畅通工程续 2008浙大研究生复试热身赛(2)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- CodeForces - 1047CEnlarge GCD(这题很难,快来看题解,超级详细,骗浏览量)
C. Enlarge GCD time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...
- json格式总结
json格式分为两种: 1.键值对: 2.数组:其中元素可以是字符串.数字.数组,还可以相互嵌套 其中图片来源于:https://blog.csdn.net/huapenguag/article/de ...
- win10卸载多余应用-通过命令行卸载
Win10 APP 卸载方式和明细,网络最全.直接包含了编辑好的文件,可以打开编辑或直接删除,部份APP可卸载,但注释了,各位可以看情况是否删除.直接把代码复制保存扩展名为“ps1”即可直接运行,这是 ...
- redux中间件的理解
redux的中间件就是用来处理reducer和actions之间应用,常用的中间件有redux-thunk,redux-sage.在redux中通过applyMiddleware方法使用中间件 使用例 ...
- shell脚本传参执行spark-submit
读取多个目录下的本地文件,多个目录通过循环遍历的方式,以参数传递: #!/bin/bash i=0while [ $i -lt 10000 ] do echo "i=$i" spa ...
- 李婷华 201771010113 《面向对象程序设计(java)》第一周学习总结
第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com 薄荷蓝莓 程序设计评测:https://pintia.cn/ 1957877441@q ...
- # C#学习笔记(一)——准备工作
C#学习笔记(一)--准备工作 目录 C#学习笔记(一)--准备工作 1.1 下载安装.NET框架 1.2 创建源代码 1.3 一些基本名称 1.4 简单的命名建议 1.1 下载安装.NET框架 .N ...