Pytest测试的命名规则是怎样的,测试实现方法是什么
Admin 2022-07-08 群英技术资讯 1485 次浏览
这篇文章给大家分享的是“Pytest测试的命名规则是怎样的,测试实现方法是什么”,对大家学习和理解有一定的参考价值和帮助,有这方面学习需要的朋友,接下来就跟随小编一起学习一下吧。pytest以特定规则搜索测试用例,所以测试用例文件、测试类以及类中的方法、测试函数这些命名都必须符合规则,才能被pytest搜索到并加入测试运行队列中。
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:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
CGI 程序可以是 Python 脚本,PERL 脚本,SHELL 脚本,C 或者 C++ 程序等。CGI 目前由 NCSA 维护,NCSA 定义 CGI 如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端 HTML 页面的接口。
这篇文章主要介绍了Python迭代器的实现原理,文章基于python的相关资料展开对Python迭代器的详细介绍,需要的小伙伴可以参考一下
我们在Django项目中可能会使用到render()函数,但是很多新手可能不知道render()函数的使用,对此,下面小编就给大家分享一个render()函数的用法,感兴趣的朋友可以看看。
在玩python学习机器时,对于那种对随机性不太敏感的模型,理论上说可以不打乱。但敏感不敏感也跟数据量级,复杂度,算法内部计算机制都有关,目前并没有一个经纬分明的算法随机度敏感度列表。既然打乱数据并不会得到一个更差的结果,一般推荐的做法就是打乱全量数据。那怎么打乱呢?
这篇文章主要为大家介绍了OpenCV学习记录python实现连通域处理函数cv2.connectedComponentsWithStats()和cv2.connectedComponents()的使用示例详解
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008