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

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

 

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

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

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

@pytest.fixture() #默认是function级别,每个function(方法/函数)都会执行

作用域

  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
复制代码

 

posted @   袁小文子  阅读(367)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示