diff --git a/_pytest/config.py b/_pytest/config.py index 4fe8bd10682..12be0c49660 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -1055,9 +1055,10 @@ def _warn_about_missing_assertion(self, mode): "(are you using python -O?)\n") def _preparse(self, args, addopts=True): - self._initini(args) if addopts: args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args + self._initini(args) + if addopts: args[:] = self.getini("addopts") + args self._checkversion() self._consider_importhook(args) diff --git a/changelog/2824.feature b/changelog/2824.feature new file mode 100644 index 00000000000..690ca939a0b --- /dev/null +++ b/changelog/2824.feature @@ -0,0 +1 @@ +Allow setting ``file_or_dir``, ``-c``, and ``-o`` in PYTEST_ADDOPTS. diff --git a/testing/test_config.py b/testing/test_config.py index d049725cdc1..9881a5d4135 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -843,3 +843,11 @@ def test_with_existing_file_in_subdir(self, tmpdir): rootdir, inifile, inicfg = determine_setup(None, ['a/exist']) assert rootdir == tmpdir assert inifile is None + + def test_addopts_before_initini(self, testdir, tmpdir, monkeypatch): + cache_dir = '.custom_cache' + monkeypatch.setenv('PYTEST_ADDOPTS', '-o cache_dir=%s' % cache_dir) + from _pytest.config import get_config + config = get_config() + config._preparse([], addopts=True) + assert config._override_ini == [['cache_dir=%s' % cache_dir]]