pytest初始化与清除fixture(二)
@pytest.fixture用法
1.导入pytest模块:import pytest
2.调用装饰器函数:@pytest.fixture(callable_or_scope=None,*args,scope='function',params=None,autouse=False,ids=None,name=None)
scope=function(默认值),表示作用于每一个测试用例
scope=class,表示每一个类调用一次,一个类中可以有多个方法
scope=moudle,表示每一个.py文件调用一次
scope=session,表示多个文件调用一次
autouse=True,表示每个测试用例都会执行初始化清除操作
import pytest @pytest.fixture(autouse=True)
def before():
print("this is setup")
yield
after() '''以下是初始化配套清除操作的第二种写法,代替yield'''
@pytest.fixture(autouse=True)
def before(request):
print("this is setup")
request.addfinalizer(after()) def after():
print("this is teardown") def test_001():
print("this is test_001()") def test_002():
print("this is test_002()")
C:\Users\cale\checkapi\test>pytest -s test_gy.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items
test_gy.py this is setup
this is test_001()
.this is teardown
this is setup
this is test_002()
.this is teardown
autouse=False(默认值),不会作用于每个测试用例,哪些测试用例要执行初始化清除操作需在测试用例里面传入对应的参数
from api.compare_result import CompareResult
from api.gy import gy_v4
import pytest class TestSuite():
def test_gy1(self,start):
'''start:调用start()方法作为该测试用例的初始化清除方法'''
inputxml1 = 'C:/Users/cale/checkapi/data/input/gyopt.xml'
outputxml1 = 'C:/Users/cale/checkapi/data/output/gyopt.xml'
cmpr=CompareResult()
cmpr.compareXML(gy_v4,inputxml1,outputxml1) @pytest.fixture(autouse=False)
def start(self):
'''调用fixture()装饰器,将此方法装饰成初始化清除方法'''
print("执行测试套件前会先执行此方法")
yield
self.end() def end(self):
print('执行完测试套件后会执行此方法') 执行结果:
test_gy.py 执行测试套件前会先执行此方法
.<?xml version="1.0" encoding="UTF-8"?>
<root><result><base><hospital_code>H0003</hospital_code><event_no></event_no><patient_id>004026990100aa</patient_id><source>门诊</source></base><btnStatus></btnStatus><invoke_result><invoke_status>0</invoke_status><invoke_message>调用失败</invoke_message></invoke_result><message><recipe_id></recipe_id><infos><info><info_id>1585813554277</info_id><recipe_item_id></recipe_item_id><group_no></group_no><drug_id></drug_id><drug_name></drug_name><error_info>干预异常!</error_info><advice></advice><source></source><rt></rt><source_id></source_id><severity></severity><message_id></message_id><type></type><analysis_type></analysis_type><analysis_result_type></analysis_result_type><info_type>0</info_type></info></infos></message></result></root>
比标准出参多出来的节点如下>>>>>>>>>>>>>>>>>
没有多余的节点
比标准出参少的节点如下>>>>>>>>>>>>>>>>>
没有缺少的节点
与标准出参类型不一致的节点如下>>>>>>>>>>>>>>>>>
节点名称:event_no ,标准出参类型:str ,实际出参类型:<class 'NoneType'>
节点名称:source ,标准出参类型:str ,实际出参类型:<class 'NoneType'>
执行完测试套件后会执行此方法
[100%] ============================== 1 passed in 1.71s ==============================
用fixture decorator调用fixture(autouse=False的情况下)
import pytest @pytest.fixture(autouse=True)
def before():
print("this is setup")
yield
after() def after():
print("this is teardown") class TestSuite():
@pytest.mark.usefixtures("before")
def test_003(self):
print("test_003()") @pytest.mark.usefixtures("before")
def test_004(self):
print("test_004()") @pytest.mark.usefixtures("before")
class TestSuite2():
def test_004(self):
print("test_004()") def test_005(self):
print("test_005()")
C:\Users\cale\checkapi\test>pytest -s test_gy.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 4 items
test_gy.py this is setup
test_003()
.this is teardown
this is setup
test_004()
.this is teardown
this is setup
test_004()
.this is teardown
this is setup
test_005()
.this is teardown
用autos调用fixture (autouse=True的情况下)
import pytest @pytest.fixture(scope='module',autouse=True)
def mod_header(request):
print("module:%s"%(request.module.__name__)) @pytest.fixture(scope='function',autouse=True)
def func_header(request):
print("function:%s"%(request.function.__name__)) def test_006():
print("test_006") def test_007():
print("test_007")
运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -s
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items
test_gy.py module:test_gy
function:test_006
test_006
.function:test_007
test_007
.
================================================== 2 passed in 0.12s ==================================================
以上mod_header()函数运行了一次,在所有测试用例执行前运行了一次;func_header()运行了两次,每个测试用例执行前都会运行一次
fixture返回值的使用
import pytest @pytest.fixture(params=[1,2])
def test_data(request):
return request.param def test_not_2(test_data):
print("test_data:%s"%(test_data))
assert test_data!=2
运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -s
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items
test_gy.py test_data:1
.test_data:2
F
====================================================== 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_gy.py:49: AssertionError
============================================= 1 failed, 1 passed in 0.47s =============================================
可以看到test_not_2这个测试用例执行了两次,test_data函数中params这个变量传了几个参数,测试用例就会运行几次
链接:https://www.jianshu.com/p/54b0f4016300
目录下的初始化(session):https://blog.csdn.net/qq_36502272/article/details/100776789
pytest初始化与清除fixture(二)的更多相关文章
- pytest初始化与清除(一)
一.初始化函数 1.测试用例级别:def setup() 2.套件级别(在模块文件中定义):def setup_module() 3.套件级别(在类中定义): @classmethod def set ...
- Pytest(3)fixture的使用
fixture的优势 Pytest的fixture相对于传统的xUnit的setup/teardown函数做了显著的改进: 命名方式灵活,不局限于 setup 和teardown 这几个命名 conf ...
- 《C++编程思想》第四章 初始化与清除(原书代码+习题+解答)
相关代码: 1. #include <stdio.h> class tree { int height; public: tree(int initialHeight); ~tree(); ...
- pytest进阶之xunit fixture
前言 今天我们再说一下pytest框架和unittest框架相同的fixture的使用, 了解unittest的同学应该知道我们在初始化环境和销毁工作时,unittest使用的是setUp,tearD ...
- pytest自动化3:fixture之conftest.py实现setup
出处:https://www.cnblogs.com/yoyoketang/p/9390073.html 前言: 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作 ...
- Pytest高级进阶之Fixture
From: https://www.cnblogs.com/feiyi211/p/6626314.html 一. fixture介绍 fixture是pytest的一个闪光点,pytest要精通怎么能 ...
- Pytest单元测试框架之FixTure基本使用
前言: 在单元测试框架中,主要分为:测试固件,测试用例,测试套件,测试执行及测试报告: 测试固件不难理解,也就是我们在执行测试用例前需要做的动作和测试执行后的需要做的事情: 比如在UI自动化测试中,我 ...
- bss段为什么要初始化,清除
我们都知道bss段需要初始化,但是这是为什么呢? 通过浏览资料,我们都会发现,bss段是不会出现在程序下载文件(*.bin *.hex)中的,因为全都是0.如果把它们出现在程序下载文件中,会增加程序下 ...
- pytest.5.参数化的Fixture
From: http://www.testclass.net/pytest/parametrize_fixture/ 背景 继续上一节的测试需求,在上一节里,任何1条测试数据导致断言不通过后测试用例就 ...
随机推荐
- 推荐算法-聚类-K-MEANS
对于大型的推荐系统,直接上协同过滤或者矩阵分解的话可能存在计算复杂度过高的问题,这个时候可以考虑用聚类做处理,其实聚类本身在机器学习中也常用,属于是非监督学习的应用,我们有的只是一组组数据,最终我们要 ...
- 19.Vuex详细使用说明-一篇文章涵盖所有知识点
vuex官网: https://vuex.vuejs.org/zh/ 一. 前言 不管是Vue,还是 React,都需要管理状态(state),比如组件之间都有共享状态的需要. 什么是共享状态? 比如 ...
- scrapy爬虫案例--爬取阳关热线问政平台
阳光热线问政平台:http://wz.sun0769.com/political/index/politicsNewest?id=1&page=1 爬取最新问政帖子的编号.投诉标题.投诉内容以 ...
- 一、HttpRunner学习汇总
HttpRunner是一款面向Http和HTTPS协议的通用测试框架,只需编写维护一份YAML/JSON脚本即可实现自动化测试.性能测试.线上监控.持续集成等多种测试需求,是基于关键字驱动的框架,基于 ...
- c语言编程学习之字符串
字符串字面量与字符变量 1.字符串字面量 字符串字面量是一对双引号括起来的字符序列.当c语言编译器在程序中遇到长度为n的字符串字面量时,它会为字符串字面量分配长度为n+1的内存空间.这块内存空间用来存 ...
- 如何理解PaaS平台,与SaaS、IaaS有什么区别?
我们经常会看到SaaS.PaaS.IaaS,但总是会摸不着头脑,有的人甚至会以为是恐怖组织的代号.其实,无论是SaaS.PaaS还是IaaS,都代表的是某一种服务,比如SaaS的含义为"软件 ...
- 透过“锁”事看InnoDB对并发的处理?
一. 并发场景下的问题 相对于串行处理方式,并发的事务处理可显著提升数据库的事务吞吐量.提高资源利用率.在MySQL实际应用中,根据场景的不同,可以分为以下几类: 读读并发 读写并发 写写并发 在这些 ...
- [Java] 数据分析--数据预处理
数据结构 键-值对:HashMap 1 import java.io.File; 2 import java.io.FileNotFoundException; 3 import java.util. ...
- [Web] 计算机网络课程(一)
局域网 覆盖范围小,自己花钱买设备,自己单位维护 线长不超过100米,带宽固定(10M 100M 1000M) 星形结构,上层交换机口少,但每个口带宽高 广域网 距离远 如在家通过ADSL拨号上网,或 ...
- CentOS/Linux内存占用大,用Shell脚本自动定时清除/释放内存
CentOS/Linux内存占用大,用Shell脚本自动定时清除/释放内存来自:互联网 时间:2020-03-22 阅读:114以下情况可能造成Linux内存占用过高服务配置存在直接分配错误,或隐性分 ...