pytestconfig 可以获取到 pytest.ini 里面的配置参数。
在写自动化用例的时候,有一些配置 参数希望能加到配置里面,如configid, productid, 以及测试环境的base_url地址,和账号相关信息。
addini的源码阅读
addini 有四个参数: name, help, type=None, default=None
def addini(self, name, help, type=None, default=None): """ register an ini-file option. :name: name of the ini-variable :type: type of the variable, can be ``pathlist``, ``args``, ``linelist`` or ``bool``. :default: default value if no ini-file option exists but is queried. The value of ini-variables can be retrieved via a call to :py:func:`config.getini(name) <_pytest.config.Config.getini>`. """ assert type in (None, "pathlist", "args", "linelist", "bool") self._inidict[name] = (help, type, default) self._ininames.append(name)
动态添加配置信息
添加命令行参数可以用 addoption 来添加命令行参数,这里我们是添加 pytest.ini 的配置信息
addini 里面参数说明
-
第一个'url'是参数的名称
-
type 是类型,默认 None, 可以设置:None, "pathlist", "args", "linelist", "bool"
-
default 是设置的默认值
-
help 是设置帮助文档,方便查阅
import pytest def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) parser.addini('url', type=None, default="http://www.baidu.com", help="添加 url 访问定制参数") # 获取pytest.ini 配置文件参数 @pytest.fixture(scope="session") def home_url(pytestconfig): url = pytestconfig.getini("url") print(" 读取配置文件的url地址:{}".format(url)) return url
参数用例传 home_url
def test_h(home_url): print("用例:{}".format(home_url))
运行结果
============================================================================================== test session starts ============================================================================================== platform win32 -- Python 3.7.0, pytest-3.9.3, py-1.7.0, pluggy-0.12.0 -- c:users93724appdatalocalprogramspythonpython37python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.0', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '3.9.3', 'py': '1.7.0', 'pluggy': '0.12.0'}, 'Plugins': {'allure-pytest': '2.7.0', 'forked': '0.2', 'html': '1.21.1' , 'metadata': '1.8.0', 'ordering': '0.6', 'repeat': '0.8.0', 'rerunfailures': '5.0', 'xdist': '1.24.1'}, 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_131'} rootdir: D:pythonProjectfixture_request, inifile: pytest.ini plugins: allure-pytest-2.7.0, forked-0.2, html-1.21.1, metadata-1.8.0, ordering-0.6, repeat-0.8.0, rerunfailures-5.0, xdist-1.24.1 collected 1 item test_03.py::test_h 读取配置文件的url地址:http://www.baidu.com 用例:http://www.baidu.com PASSED =========================================================================================== 1 passed in 0.03 seconds ============================================================================================
pytest.ini 配置 url 地址
如果有一天测试环境发生了改变,这时候不需要去改代码,只需要在 pytest.ini 配置一个环境地址
[pytest]
url = https://www.cnblogs.com/HandsUp/
重新运行,我们得到的结果是
============================================================================================== test session starts ============================================================================================== platform win32 -- Python 3.7.0, pytest-3.9.3, py-1.7.0, pluggy-0.12.0 -- c:users93724appdatalocalprogramspythonpython37python.exe cachedir: .pytest_cache metadata: {'Python': '3.7.0', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '3.9.3', 'py': '1.7.0', 'pluggy': '0.12.0'}, 'Plugins': {'allure-pytest': '2.7.0', 'forked': '0.2', 'html': '1.21.1' , 'metadata': '1.8.0', 'ordering': '0.6', 'repeat': '0.8.0', 'rerunfailures': '5.0', 'xdist': '1.24.1'}, 'JAVA_HOME': 'C:\Program Files\Java\jdk1.8.0_131'} rootdir: D:pythonProjectfixture_request, inifile: pytest.ini plugins: allure-pytest-2.7.0, forked-0.2, html-1.21.1, metadata-1.8.0, ordering-0.6, repeat-0.8.0, rerunfailures-5.0, xdist-1.24.1 collected 1 item test_03.py::test_h 读取配置文件的url地址:https://www.cnblogs.com/HandsUp/ 用例:https://www.cnblogs.com/HandsUp/ PASSED =========================================================================================== 1 passed in 0.03 seconds ============================================================================================