前言
- 本篇来学习pytest内置fixture中的pytestconfig使用。
pytestconfig
- pytestconfig 的作用跟 request.config 是一样的,都是获取配置对象
getoption()
def pytest_addoption(parser):
parser.addoption(
"--cmdopt", action="store", default="type2", help="my option: type1 or type2")
import os
import pytest
@pytest.fixture
def cmdopt(pytestconfig):
return pytestconfig.getoption("--cmdopt")
def test_answer_1(pytestconfig):
kind = pytestconfig.getoption("--cmdopt")
print("获取到命令行参数:%s" % kind)
def test_answer_2(cmdopt):
print("获取到命令行参数:%s" % cmdopt)
if __name__ == '__main__':
os.system('pytest -s test_64.py --cmdopt type1')
getini()
# pytest.ini
[pytest]
log_cli = 1
addopts = -v -x
import os
def test_get_ini(pytestconfig):
"""读取ini配置信息"""
log_cli = pytestconfig.getini('log_cli')
print("获取到markers :%s" % log_cli)
addopts = pytestconfig.getini('addopts')
print("获取到addopts的配置:%s " % addopts)
if __name__ == '__main__':
os.system('pytest -s test_65.py')
|