diff --git a/_pytest/config.py b/_pytest/config.py index 837da7f21..9f579a7a9 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -833,6 +833,14 @@ def exists(path, ignore=EnvironmentError): return False def getcfg(args, inibasenames): + if "-c" in args: + n = len(args) + for i in range(1, n): + if args[i - 1] == "-c" and not str(args[i]).startswith("-"): + iniconfig = py.iniconfig.IniConfig(args[i]) + if 'pytest' in iniconfig.sections: + return iniconfig['pytest'] + return {} args = [x for x in args if not str(x).startswith("-")] if not args: args = [py.path.local()] diff --git a/_pytest/main.py b/_pytest/main.py index 246470f01..011e43275 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -38,6 +38,9 @@ def pytest_addoption(parser): help="exit after first num failures or errors.") group._addoption('--strict', action="store_true", help="run pytest in strict mode, warnings become errors.") + # This option is never used as such, see config.getcfg(). + group._addoption("-c", metavar="file", type=str, dest="_inifilename", + help="load configuration from `file` instead of trying to locate one of the implicit configuration files.") group = parser.getgroup("collect", "collection") group.addoption('--collectonly', '--collect-only', action="store_true", diff --git a/testing/test_config.py b/testing/test_config.py index c25de6ea4..aa96c1d56 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -79,6 +79,21 @@ class TestConfigCmdlineParsing: config = testdir.parseconfig() pytest.raises(AssertionError, lambda: config.parse([])) + def test_explicitly_specified_config_file_is_loaded(self, testdir): + testdir.makeconftest(""" + def pytest_addoption(parser): + parser.addini("custom", "") + """) + testdir.makeini(""" + [pytest] + custom = 0 + """) + testdir.makefile(".cfg", custom = """ + [pytest] + custom = 1 + """) + config = testdir.parseconfig("-c", "custom.cfg") + assert config.getini("custom") == "1" class TestConfigAPI: def test_config_trace(self, testdir):