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中字符串的格式化方法小结
May 03 Python
用virtualenv建立多个Python独立虚拟开发环境
Jul 06 Python
Python自动化运维之IP地址处理模块详解
Dec 10 Python
Python中elasticsearch插入和更新数据的实现方法
Apr 01 Python
详解Python locals()的陷阱
Mar 26 Python
对pyqt5中QTabWidget的相关操作详解
Jun 21 Python
PyQt5根据控件Id获取控件对象的方法
Jun 25 Python
Python 音频生成器的实现示例
Dec 24 Python
python 将dicom图片转换成jpg图片的实例
Jan 13 Python
Django DRF路由与扩展功能的实现
Jun 03 Python
Python基于template实现字符串替换
Nov 27 Python
用sleep间隔进行python反爬虫的实例讲解
Nov 30 Python
pycharm debug 断点调试心得分享
Python通过m3u8文件下载合并ts视频的操作
Python实现Telnet自动连接检测密码的示例
AI:如何训练机器学习的模型
python 用递归实现通用爬虫解析器
MATLAB 如何求取离散点的曲率最大值
用Python远程登陆服务器的步骤
You might like
第1次亲密接触PHP5(1)
2006/10/09 PHP
PHP怎么实现网站保存快捷方式方便用户随时浏览
2013/08/15 PHP
PHP文件读取功能的应用实例
2015/05/08 PHP
如何实现动态删除javascript函数
2007/05/27 Javascript
Javascript变量函数浅析
2011/09/02 Javascript
jQuery ui插件的使用方法代码实例
2013/05/08 Javascript
jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮
2013/10/11 Javascript
jQuery中extend函数详解
2015/02/13 Javascript
JS识别浏览器类型(电脑浏览器和手机浏览器)
2016/11/18 Javascript
JQuery 又谈ajax局部刷新
2017/11/27 jQuery
详解angularjs 学习之 scope作用域
2018/01/15 Javascript
JS实现二维数组横纵列转置的方法
2018/04/17 Javascript
基于vue-cli3和element实现登陆页面
2019/11/13 Javascript
vue prop属性传值与传引用示例
2019/11/13 Javascript
javascript的delete运算符知识点总结
2019/11/19 Javascript
vue实现图书管理系统
2020/12/29 Vue.js
vue3弹出层V3Popup实例详解
2021/01/04 Vue.js
[46:03]LGD vs VGJ.T 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
修复CentOS7升级Python到3.6版本后yum不能正确使用的解决方法
2018/01/26 Python
Python基础教程之内置函数locals()和globals()用法分析
2018/03/16 Python
使用python telnetlib批量备份交换机配置的方法
2019/07/25 Python
Python学习笔记之lambda表达式用法详解
2019/08/08 Python
django2.2安装错误最全的解决方案(小结)
2019/09/24 Python
Python在后台自动解压各种压缩文件的实现方法
2020/11/10 Python
解决python3中os.popen()出错的问题
2020/11/19 Python
使用python爬取抖音app视频的实例代码
2020/12/01 Python
50道外企软件测试面试题
2014/08/18 面试题
实习自我评价怎么写
2013/12/02 职场文书
大学竞选班长演讲稿
2014/04/24 职场文书
物理教育专业求职信
2014/06/25 职场文书
社区党的群众路线教育实践活动剖析材料
2014/10/09 职场文书
会计工作能力自我评价
2015/03/05 职场文书
收入证明范本
2015/06/12 职场文书
《合作意向书》怎么写?
2019/08/20 职场文书
python开发的自动化运维工具ansible详解
2021/08/07 Python
Win11 KB5015814遇安装失败 影响开始菜单性能解决方法
2022/07/15 数码科技