首页 > 其他分享 >pytest-2 之前后置及 conftest.py+fixture+yield实现用例前后置

pytest-2 之前后置及 conftest.py+fixture+yield实现用例前后置

时间:2023-02-25 15:55:42浏览次数:34  
标签:function 前后 py fixture --- 用例 pytest def

pytest测试用例及类级别的前置,可以和unittest一样进行定义,也可以把该前置方法或类定义到conftest.py里,而在需要前置的方法的参数里加上该前置名作为参数;

pytest有两种方式来设置用例的前后置;

一、方式一:直接使用pytest本身的前后置方法

 

模块级别的前后置  
def setup_module(module)

在一个模块内的测试用例前后,只运行一次 ;

which will usually be called once for all the functions

def teardown_module(module)
 

Class level setup/teardown

 
 
@classmethod
def setup_class(cls)
 Similarly, the following methods are called at class level before and after all test methods of the class are called: 
 
@classmethod
def teardown_class(cls)
 

Method and function level setup/teardown

 
def setup_method(self, method)
 the following methods are called around each method invocation 
   
def teardown_method(self, method):
   
def setup_function(function):
 
   
def teardown_function(function):
 
     
     

 

:

1、用例级别的前后置方法: setup() 和teardow() 按官方说法不是pytest原生的,是nose 支持的一部分;原生的是setup_method(),teardown_method();官方原话是:(Native pytest support uses setup_method and teardown_method)

2、类级别的前后置方法:setup_class(),teardown_class()

 

pytest官方文档:https://docs.pytest.org/en/latest/contents.html

 

二、方式二,pytest.py+fixture+yield ,实现用例的前后置

pytest的测试夹具,fixture的定义及作用域

相当于装饰器,需要@pytest.fixture()来装饰;不同的scope代表了不同的作用域

作用域

  1、@pytest.fixture(scope='function'),定义用例级别的前后置方法;每个用例前后都执行该前后置;默认使function

  2、@pytest.fixture(scope='class'),定义类级别的前后置方法;类内的所有方法执行前,只执行一次该前置;类内的所有方法执行之后,执行该后置

  3、scope=module,作用于整个模块,每个模块的所有test运行,只执行该夹具一次;(一个.py文件,就是一个module)

  4、scope=session,作用于整个session(慎用),每个session只运行一次;(多个文件调用一次)

scope值不同,代表了不同的作用域;作用域的大小 session>module>class>function

调用

  1、在用例方法的参数中,写上前后置的方法名

  2、在定义前后置方法的时候(设置参数autouse=True),这样设置,定义的前后置方法会自动执行(用的比较少)

  3、类级别的前后置,如果调用时需要调用的话,在第一个方法里,加上类名

注意点:前后置方法可以统一放到conftest.py文件中,而用例文件中可以直接使用,不需要导入;

 

 

pytest用例的执行顺序,同一个文件按照用例文件代码的前后顺序执行

多个文件的执行顺序,是按文件名的ASCII码排序

 

三、实例

import pytest

@pytest.fixture(scope='function') # 默认scope是function,是用例级别
def test_case():
    print('---用例前置代码---')
    yield
    print('---用例后置代码--')

@pytest.fixture(scope='class')
def cls_case():
    print('--类级别前置--')
    yield
    print('---类级别后置---')


# 类级别的前后置,如果调用时需要调用的话,在第一个方法里,加上类名
def test_01(test_case,cls_case):
    print('case01')
    assert 1==1
# @pytest.mark.parametrize()
def test_02(test_case):
    print('case02')
    assert 2==2

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.7.9, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\python35_zdh\py35_xxx\xxx_pytest, configfile: pytest.inicollected 2 items

test_login.py --类级别前置--
---用例前置代码---
.case01
---用例后置代码--
---类级别后置---
---用例前置代码---
.case02
---用例后置代码--
                                                         [100%]

============================== 2 passed in 0.03s ==============================
Process finished with exit code 0

 

标签:function,前后,py,fixture,---,用例,pytest,def
From: https://www.cnblogs.com/yxm-yxwz/p/16496998.html

相关文章

  • Python学习笔记(五)if分支语句
    一、if语法示例:1money=int(input('请输入余额:'))2ifmoney>=5:3print('买得起!')45ifTrue:6print('条件成立时要执行的代码1')7print('条......
  • PyMySQL连接
    title:Pymysql连接author:杨晓东permalink:Pymysql连接date:2021-10-0211:27:04categories:-投篮tags:-demoPymysqlMySQL数据库_连接"""一、python实......
  • python的驻留机制
    在某些情况下,python会使用现有的不可变对象,而不是总是创建一个新对象。驻留的对象在内部使用类似字典的结构(驻留池)进行驻留,节省内存。原理:系统维护interned字典类型记录已......
  • fatal error: Python.h: No such file or directory
    fatalerror:Python.h:Nosuchfileordirectory过程:通过pipinstall某个库,发生了一下错误,找不到Python.h的头文件解决方案搜索StackoverFlow,我的环境是CentOS,所以执......
  • 强行在CentOS上kill python进程 unix:///tmp/supervisor.sock
    问题:unix:///tmp/supervisor.sock解决方案:echo_supervisord_conf>/etc/supervisord.confsudosupervisord-c/etc/supervisord.confsudosupervisorctlstatus​​https:......
  • 解决Python Tornado的某个页面不需要进行xsrf的检查
    错误信息:‘_xsrf’argumentmissingfrompost我们总是对全站开启xsrf的功能,但是有时候想对单个页面不希望启用该功能的验证,那么我们可以进行重写​​check_xsrf_cookie()......
  • Python3的AES加密和解密
    此加密和解密的秘钥的长度必须是16位、24位或者32位importbase64importosfromCrypto.CipherimportAES#AESkeymustbeeither16,24,or32byteslongaes_key="1......
  • PyMySQL查询
    title:PyMySQL查询author:杨晓东permalink:PyMySQL查询date:2021-10-0211:27:04categories:-投篮tags:-demoPyMySQLMySQL数据库_查询"""数据库查询操......
  • PyMySQL插入
    title:PyMySQL插入author:杨晓东permalink:PyMySQL插入date:2021-10-0211:27:04categories:-投篮tags:-demoPyMySQLMySQL数据库_插入#!/usr/bin/pytho......
  • Pytest初识
    一、单元测试框架简介1.什么是单元测试单元测试是指在软件开发过程中,针对软件的最小单位(函数,方法)进行正确性的检查测试。2.常用单元测试框架2.1Java类别junit......