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 相关文章推荐
如何搜索查找并解决Django相关的问题
Jun 30 Python
Python使用functools模块中的partial函数生成偏函数
Jul 02 Python
python中函数传参详解
Jul 03 Python
Python matplotlib绘图可视化知识点整理(小结)
Mar 16 Python
Flask 让jsonify返回的json串支持中文显示的方法
Mar 26 Python
windows下添加Python环境变量的方法汇总
May 14 Python
Python API自动化框架总结
Nov 12 Python
python 实现查询Neo4j多节点的多层关系
Dec 23 Python
TensorFlow获取加载模型中的全部张量名称代码
Feb 11 Python
Python 爬取必应壁纸的实例讲解
Feb 24 Python
TensorFlow固化模型的实现操作
May 26 Python
python初步实现word2vec操作
Jun 09 Python
pycharm debug 断点调试心得分享
Python通过m3u8文件下载合并ts视频的操作
Python实现Telnet自动连接检测密码的示例
AI:如何训练机器学习的模型
python 用递归实现通用爬虫解析器
MATLAB 如何求取离散点的曲率最大值
用Python远程登陆服务器的步骤
You might like
php缓存技术详细总结
2013/08/07 PHP
Laravel学习基础之migrate的使用教程
2017/10/11 PHP
jQuery AJAX 调用WebService实现代码
2010/03/24 Javascript
js实现不重复导入的方法
2016/03/02 Javascript
深入浅析Bootstrap列表组组件
2016/05/03 Javascript
BOM系列第三篇之定时器应用(时钟、倒计时、秒表和闹钟)
2016/08/17 Javascript
nodejs个人博客开发第二步 入口文件
2017/04/12 NodeJs
Angular中的interceptors拦截器
2017/06/25 Javascript
bootstrap table表格插件之服务器端分页实例代码
2018/09/12 Javascript
解决angularjs service中依赖注入$scope报错的问题
2018/10/02 Javascript
微信小程序使用swiper组件实现层叠轮播图
2018/11/04 Javascript
Handtrack.js库实现实时监测手部运动(推荐)
2021/02/08 Javascript
python 换位密码算法的实例详解
2017/07/19 Python
Python从使用线程到使用async/await的深入讲解
2018/09/16 Python
对python中Json与object转化的方法详解
2018/12/31 Python
python按比例随机切分数据的实现
2019/07/11 Python
Python 批量刷博客园访问量脚本过程解析
2019/08/30 Python
利用Python的turtle库绘制玫瑰教程
2019/11/23 Python
Python爬虫实现百度翻译功能过程详解
2020/05/29 Python
HTML5本地存储之Web Storage详解
2016/07/04 HTML / CSS
欧洲、亚洲、非洲和拉丁美洲的度假套餐:Great Value Vacations
2019/03/30 全球购物
软件工程师岗位职责
2013/11/16 职场文书
英文自我鉴定
2013/12/10 职场文书
办公室主任主任岗位责任制
2014/02/11 职场文书
省级优秀班集体申报材料
2014/05/25 职场文书
授权委托书格式
2014/07/31 职场文书
离职证明标准格式
2014/09/15 职场文书
2014年最新版离婚协议书范本
2014/11/25 职场文书
中国合伙人观后感
2015/06/02 职场文书
2016年寒假学习心得体会
2015/10/09 职场文书
远程教育学习心得体会
2016/01/23 职场文书
提升Nginx性能的一些建议
2021/03/31 Servers
python实现三阶魔方还原的示例代码
2021/04/28 Python
利用python Pandas实现批量拆分Excel与合并Excel
2021/05/23 Python
Python Pygame实战在打砖块游戏的实现
2022/03/17 Python
唤醒紫霞仙子,携手再游三界!大话手游X《大话西游》电影合作专属剧情任务
2022/04/03 其他游戏