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

最新文章

  1. Winform绑定数据源的几种方式?
  2. 2017-1-2 nfs服务器配置
  3. 20161106PM-Fiddler
  4. 利用Hadoop实现超大矩阵相乘之我见(二)
  5. HDU 5828 Rikka with Sequence (线段树+剪枝优化)
  6. poj1584
  7. 【学习笔记】【C语言】循环结构-do while
  8. jquery validation ajax 验证
  9. Distributed locks with Redis--官方
  10. Linux开发工具之gdb(上)
  11. log4net编译后命名空间找不到的问题
  12. mac webstrom在线激活
  13. Tengine笔记3:Nginx的反向代理和健康状态检查
  14. 《Javascript权威指南》
  15. js 去除html标签
  16. CABasicAnimation 基础
  17. 关于数据库timestamp类型问题
  18. C#基础(三)--运算符及条件控制语句
  19. Cocos2D:塔防游戏制作之旅(三)
  20. git无法提交问题

热门文章

  1. app实现外部浏览器打开链接
  2. WinUI 剪裁发布中的一个小坑
  3. Hyperledger Fabric部署与测试(Ubuntu)
  4. Canvas:路径
  5. 基于C++的OpenGL 12 之多光源
  6. cximage菜单(Load Jpeg Resource)
  7. B站【挽救小白第一季】前端代码记录笔记
  8. VMware-实用网站
  9. 基于AD9361的双收双发射频FMC子卡
  10. VUE学习-列表渲染