1 下面以如下需求为例实例演示如何自定义测试文件、测试用例,测试类的命名规则
- 测试文件以 test_, check_开头,或者_test, _check结尾
- 测试用例以test_,check_开头,或者_test, _check结尾
- 测试类以Test,Check开头,或者Test,Check结尾
2 实现步骤:
(1)在根目录创建 pytest.ini文件
(2) 在pytest.ini文件中编写如下内容:
[pytest]
python_files = test_* *_test.py check_* *_check.py
python_classes = Test* Check* *Test *Check
python_functions = test_* check_* *_test *_check
(3)依次创建 test_demo.py,demo_test.py,check_demo.py,demo_check.py
(4)在上述每个文件中均写入如下测试用例代码
def test_demo():
print("in test_demo...")
def demo_test():
print("in demo_test...")
def check_demo():
print("in test_demo...")
def demo_check():
print("in demo_test...")
class CheckDemo:
def test_demo(self):
print("in test_demo...")
def demo_test(self):
print("in demo_test...")
def check_demo(self):
print("in test_demo...")
def demo_check(self):
print("in demo_test...")
class DemoCheck:
def test_demo(self):
print("in test_demo...")
def demo_test(self):
print("in demo_test...")
def check_demo(self):
print("in test_demo...")
def demo_check(self):
print("in demo_test...")
(5)执行 pytest 查看结果如下:即均已生效
G:\redrose2100\src\demo>pytest
========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: G:\redrose2100\src\demo, configfile: pytest.ini
plugins: allure-pytest-2.9.43, rerunfailures-10.0
collected 48 items
check_example.py ............ [ 25%]
example_check.py ............ [ 50%]
example_test.py ............ [ 75%]
test_example.py ............ [100%]
=========================================================================== 48 passed in 0.12s ===========================================================================
G:\redrose2100\src\demo>