目录
- ini的介绍
- ini的简单和使用
- 自定义mark标签
- 自定义运行时的默认参数
- 格式化日志
- 自定义测试文件命名规则
- 自动以测试类的命名规则
- 自定义函数的命名规则
- 总结:
ini的介绍
pytest.ini
是pytest框架的主配置文件,实际生产中主要用来规范日志的格式或日志文件保存的位置,增加用例标签等等,总之简单易用,属于pytest学习的重要知识点。
ini的简单和使用
PS:pytest.ini文件命名不能修改,文件中第一行必须用【pytest】申明这是一个pytest的ini文件
自定义mark标签
我门在编写自动化测试用例时,会有各种类型的场景用例,我们又不想一次性运行全部,只想运行其中的几个,这时我们可以借助mark标签来管理测试用例,mark标签是任意取的,但是要避开Python和pytest关键字,运行标签用 - m 来运行,如:pytest -m div test_add.py::TestAdd
# 被测对象
def add(a, b):
return a + b
# 被测对象
def div(a, b):
return a / b
# 测试脚本
class TestAdd:
def test_add(self):
assert 3 == add(1, 2)
def test_add2(self):
assert 5 == add(1, 4)
@pytest.mark.div
def test_div(self):
assert 1 == div(1, 1)
E:\Home_Work\Home_Work2\pytest01\test>pytest -m div test_add.py::TestAdd
======================================================== test session starts ========================================================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
rootdir: E:\Home_Work\Home_Work2\pytest01, configfile: pytest.ini
plugins: allure-pytest-2.9.43, Faker-8.10.1, cov-2.12.1, forked-1.3.0, html-2.1.1, metadata-1.11.0, rerunfailures-10.1, testconfig-0.2
.0, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected
test_add.py .
E:\Home_Work\Home_Work2\pytest01\test\test_add.py:26: PytestUnknownMarkWarning: Unknown pytest.mark.add - is this a typo? You can r
egister custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
@pytest.mark.add
上面pytest.ini中不加markers场景,也能运行成功,但是会有警告
接下来我们在pytest.ini中加入标签,再次运行脚本
PS:多个标签要换行,且不能在顶格写,要有空格
[pytest]
markers = div
add
再次运行脚本 pytest -m div test_add.py::TestAdd,发现警告消失了
E:\Home_Work\Home_Work2\pytest01\test>pytest -m div test_add.py::TestAdd
======================================================== test session starts ========================================================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
rootdir: E:\Home_Work\Home_Work2\pytest01, configfile: pytest.ini
plugins: allure-pytest-2.9.43, Faker-8.10.1, cov-2.12.1, forked-1.3.0, html-2.1.1, metadata-1.11.0, rerunfailures-10.1, testconfig-0.2
.0, xdist-2.3.0
collected 3 items / 2 deselected / 1 selected
test_add.py . [100%]
================================================== 1 passed, 2 deselected in 0.88s ==================================================
E:\Home_Work\Home_Work2\pytest01\test>
自定义运行时的默认参数
pytest有多个经常使用的参数,如 - vs 来打印更详细的信息,但是每次都要手动输入很麻烦,我们可不可设置成默认详细打印呢?这是需要用到 addopts 配置默认运行参数
PS:addopts 运行时参数(可添加多个命令行参数,空格分隔,所有参数与命令行一致)
[pytest]
markers = div
add
addopts = -v -s
格式化日志
[pytest]
markers = div
add
addopts = -v -s --capture=no
log_cli = true
log_cli_level = info
log_cli_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_file = ./log/test.log
log_file_level = info
log_file_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format = %Y-%m-%d %H:%M:%S
自定义测试文件命名规则
PS:想控制多种文件类型,用空格隔开
[pytest]
markers = div
add
python_files = check_*.py update_**.py
注释控制后,就可以正常运行了
自动以测试类的命名规则
[pytest]
markers = div
add
python_classes = TestT_*
自定义函数的命名规则
[pytest]
markers = div
add
python_functions= testT_* check_*
总结:
[pytest]
markers 自定义mark 标签名
addopts 运行时参数(可添加多个命令行参数,空格分隔,所有参数与命令行一致)
python_files 自定义测试文件命名规则
python_classes = Test_* 自定义测试类命名规则
python_functions= test_* check_* 自定义测试方法命名规则
norecursedirs = result logs datas test_demo* 运行时忽略某些文件夹