Pytest之测试命名规则的使用


Posted in Python onApril 16, 2021

背景:

pytest以特定规则搜索测试用例,所以测试用例文件、测试类以及类中的方法、测试函数这些命名都必须符合规则,才能被pytest搜索到并加入测试运行队列中。

默认搜索规则:

  • 如果pytest命令行有指定目录,则从该目录中开始查找测试用例文件,如果没有指定,则从当前运行目录开始查找文件。注意,该查找是递归查找,子目录中的文件也会被查找到。
  • 并不是能够查找到目录下的所有文件,只有符合命名规则的文件才会被查找。默认规则是以test_开头或者以_test结尾的.py文件。
  • 在测试文件中查找Test开头的类,以及类中以test_开头的方法,查找测试文件中test_开头的函数。

测试用例默认命名规则

  • 除非pytest命令指定到测试用例文件,否则测试用例文件命名应该以 test_开头或者以_test结尾。
  • 测试函数命名,测试类的方法命名应该以test_开头。
  • 测试类命名应当以Test开头。

tips: 测试类的不应该有构造函数。

笔者习惯装测试用例的文件夹,测试用例文件,测试函数,类中的测试方法都以test_开头。建议保持一种统一的风格。

示例:

# func.py
def add(a,b):
 return a+b

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

 #def __init__(self):
  #self.a = 1

 def test_add_by_class(self):
  assert add(2,3) == 5


def test_add_by_func():
 assert add(4,6) == 10

'''
# stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items

test_case\test_func.py ..                                                [100%]

============================== 2 passed in 0.04s ==============================
[Finished in 1.3s]
######################################################################
'''

测试结果中,test_case\test_func.py … 。两个点号代表两个测试用例。

错误示范,当测试类有构造函数时:

# func.py
def add(a,b):
 return a+b

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

 def __init__(self):
  self.a = 1

 def test_add_by_class(self):
  assert add(2,3) == 5


def test_add_by_func():
 assert add(4,6) == 10

'''
# stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 1 item

test_case\test_func.py .                                                 [100%]

============================== warnings summary ===============================
test_case\test_func.py:4
  D:\Python3.7\project\pytest\test_case\test_func.py:4: PytestCollectionWarning: cannot collect test class 'TestFunc' because it has a __init__ constructor (from: test_case/test_func.py)
    class TestFunc:

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 1 passed, 1 warning in 0.04s =========================
[Finished in 1.4s]
######################################################################
'''

会报错,pytest只能找到test_开头的函数,但是不能找到Test开头的含有构造函数的测试类。

自定义测试用例命名规则

如果因为某种需要,需要使用其他命名规则命名的测试文件、测试函数、测试类以及测试类的方法,可以通过pytest.ini配置文件做到。

在测试系统的顶层目录创建pytest.ini文件,在pytest.ini文件中写入如下配置:

[pytest]
# 更改测试文件命名规则
python_files = HG*

# 更改测试类命名规则
python_classes = HG*

# 更嗨测试函数命名规则
python_functions = HG*

示例:

# func.py
def add(a,b):
 return a+b

# ./test_case/HG_func.py
import pytest
from func import *

class HGFunc:

 #def __init__(self):
  #self.a = 1

 def HG_add_by_class(self):
  assert add(2,3) == 5


def HG_add_by_func():
 assert add(4,6) == 10

'''
stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/HG_func.py::HGFunc::HG_add_by_class PASSED                     [ 50%]
test_case/HG_func.py::HG_add_by_func PASSED                              [100%]

============================== 2 passed in 0.03s ==============================
[Finished in 1.3s]
'''

Tips:

  • pytest.ini是可以改变pytest运行方式的配置文件,但是正常情况下,测试系统里根本不需要存在pytest.ini文件,我们使用默认的运行方式即可工作。
  • pytest.ini还有许多其他个性化配置,当有需要时,可以在自动化测试项目的顶层目录里创建pytest.ini文件,添加配置,达到个性化运行的目的。

到此这篇关于Pytest之测试命名规则的使用的文章就介绍到这了,更多相关Pytest 命名规则内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python THREADING模块中的JOIN()方法深入理解
Feb 18 Python
python并发编程之线程实例解析
Dec 27 Python
tensorflow识别自己手写数字
Mar 14 Python
Python模拟登录的多种方法(四种)
Jun 01 Python
详细整理python 字符串(str)与列表(list)以及数组(array)之间的转换方法
Aug 30 Python
对django layer弹窗组件的使用详解
Aug 31 Python
Python上下文管理器类和上下文管理器装饰器contextmanager用法实例分析
Nov 07 Python
利用Python的sympy包求解一元三次方程示例
Nov 22 Python
在pycharm中实现删除bookmark
Feb 14 Python
Python如何使用队列方式实现多线程爬虫
May 12 Python
Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)
Dec 07 Python
python连接手机自动搜集蚂蚁森林能量的实现代码
Feb 24 Python
pycharm debug 断点调试心得分享
Python通过m3u8文件下载合并ts视频的操作
Python实现Telnet自动连接检测密码的示例
AI:如何训练机器学习的模型
python 用递归实现通用爬虫解析器
MATLAB 如何求取离散点的曲率最大值
用Python远程登陆服务器的步骤
You might like
PHP4(windows版本)中的COM函数
2006/10/09 PHP
Extjs列表详细信息窗口新建后自动加载解决方法
2010/04/02 Javascript
简单实用的js调试logger组件实现代码
2010/11/20 Javascript
jquery eval解析JSON中的注意点介绍
2013/08/23 Javascript
jquery中prop()方法和attr()方法的区别浅析
2013/09/06 Javascript
简单的js表单验证函数
2013/10/28 Javascript
js实现抽奖效果
2017/03/27 Javascript
JS库之Three.js 简易入门教程(详解之一)
2017/09/13 Javascript
AngularJS中的路由使用及实现代码
2017/10/09 Javascript
加载 vue 远程代码的组件实例详解
2017/11/20 Javascript
vue组件与复用详解
2018/04/08 Javascript
Vue.directive使用注意(小结)
2018/08/31 Javascript
vue中动态添加class类名的方法
2018/09/05 Javascript
Vue.extend 登录注册模态框的实现
2020/12/29 Vue.js
利用Python绘制MySQL数据图实现数据可视化
2015/03/30 Python
Python发送form-data请求及拼接form-data内容的方法
2016/03/05 Python
在Python的Flask框架中构建Web表单的教程
2016/06/04 Python
tensorflow创建变量以及根据名称查找变量
2018/03/10 Python
PyCharm安装第三方库如Requests的图文教程
2018/05/18 Python
python selenium 对浏览器标签页进行关闭和切换的方法
2018/05/21 Python
Python实用技巧之列表、字典、集合中根据条件筛选数据详解
2018/07/11 Python
python Django里CSRF 对应策略详解
2019/08/05 Python
基于h5py的使用及数据封装代码
2019/12/26 Python
Pycharm 2020年最新激活码(亲测有效)
2020/09/18 Python
jupyter notebook 实现matplotlib图动态刷新
2020/04/22 Python
平面设计师工作职责范文
2013/12/03 职场文书
单位实习证明怎么写
2014/01/17 职场文书
餐饮收银员岗位职责
2014/02/07 职场文书
五水共治一句话承诺
2014/05/30 职场文书
家具公司总经理岗位职责
2014/07/08 职场文书
小学生校园广播稿
2014/09/28 职场文书
老人节标语大全
2014/10/08 职场文书
2016关于军训的心得体会
2016/01/11 职场文书
实习报告范文之电话客服岗位
2019/07/26 职场文书
python状态机transitions库详解
2021/06/02 Python
SpringBoot接入钉钉自定义机器人预警通知
2022/07/15 Java/Android