Pytest之测试命名规则的使用示例

发布时间:2021-04-16 10:47:49 作者:小新
来源:亿速云 阅读:174

这篇文章主要介绍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:

以上是“Pytest之测试命名规则的使用示例”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. python中pytest进阶之xunit fixture的示例分析
  2. python中pytest进阶之fixture的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pytest

上一篇:怎么在CocosCreator中使用http和WebSocket

下一篇:CocosCreator制作射击游戏的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》