Pytest查找测试策略
默认情况下,pytest会递归查找当前目录下所有以test开始或结尾的Python脚本,并执行文件内的所有以test开始或结束的函数和方法。
标记测试函数
由于某种原因(如test_func2的功能尚未开发完成),我们只想执行指定的测试函数。在pytest中有几种方式可以解决:
第一种,显式指定函数名,通过::标记
pytest test_no_mark.py::test_func1
第二种,使用模糊匹配,使用-k选项标识
pytest -k func1 test_no_mark.py
示例
test02.py
import pytest
def test_01():
print('the test01')
assert True
def test_02():
print('the test02')
assert True
- 执行全部用例
pytest test02.py
- 执行test_01()函数
pytest test02.py::test_01
- 执行带test的函数
pytest -k test test02.py
- 执行带01的函数
pytest -k 01 test02.py
第三种,使用pytest.mark在函数上进行标记
示例
test03.py
import pytest
class Test_03:
@pytest.mark.do
def test_01(self):
print('the test01')
assert True
@pytest.mark.undo
def test_02(self):
print('the test02')
assert True
@pytest.mark.do
def test_03(self):
print('the test03')
assert True
用过.ini配置文件,注册标签名
[pytest]
markers=
do:do
undo:undo
- 执行do标记的用例
pytest -m do test03.py