背景:
pytest以特定规则搜索测试用例,所以测试用例文件、测试类以及类中的方法、测试函数这些命名都必须符合规则,才能被pytest搜索到并加入测试运行队列中。
默认搜索规则:
如果pytest命令行有指定目录,则从该目录中开始查找测试用例文件,如果没有指定,则从当前运行目录开始查找文件。注意,该查找是递归查找,子目录中的文件也会被查找到。 并不是能够查找到目录下的所有文件,只有符合命名规则的文件才会被查找。默认规则是以test_开头或者以_test结尾的.py文件。 在测试文件中查找Test开头的类,以及类中以test_开头的方法,查找测试文件中test_开头的函数。
测试用例默认命名规则
除非pytest命令指定到测试用例文件,否则测试用例文件命名应该以 test_开头或者以_test结尾。 测试函数命名,测试类的方法命名应该以test_开头。 测试类命名应当以Test开头。
tips: 测试类的不应该有构造函数。
笔者习惯装测试用例的文件夹,测试用例文件,测试函数,类中的测试方法都以test_开头。建议保持一种统一的风格。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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 … 。两个点号代表两个测试用例。
错误示范,当测试类有构造函数时:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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文件中写入如下配置:
1
2
3
4
5
6
7
8
9
[pytest]
# 更改测试文件命名规则
python_files
=
HG
*
# 更改测试类命名规则
python_classes
=
HG
*
# 更嗨测试函数命名规则
python_functions
=
HG
*
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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文件,添加配置,达到个性化运行的目的。
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】