From 7325a5fe2ee061c5ef48fabb1aa2c4abdf28b9a4 Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Fri, 23 Jan 2015 20:09:42 +0000 Subject: [PATCH 1/4] Support setting configuration using the PYTEST_ADDOPTS environment variable. --HG-- branch : env-addopts --- _pytest/config.py | 2 ++ testing/test_config.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/_pytest/config.py b/_pytest/config.py index c92374569..0d9beb3d3 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -705,6 +705,8 @@ class Config(object): def _preparse(self, args, addopts=True): self._initini(args) if addopts: + env_addopts = os.environ.get('PYTEST_ADDOPTS', '') + args[:] = env_addopts.replace('"', '').split() + args args[:] = self.getini("addopts") + args self._checkversion() self.pluginmanager.consider_preparse(args) diff --git a/testing/test_config.py b/testing/test_config.py index aa96c1d56..32e03f0d6 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1,3 +1,5 @@ +import os + import py, pytest from _pytest.config import getcfg @@ -19,11 +21,16 @@ class TestParseIni: getcfg([''], ['setup.cfg']) #happens on py.test "" def test_append_parse_args(self, testdir, tmpdir): + os.environ['PYTEST_ADDOPTS'] = '--color no -rs --tb="short"' tmpdir.join("setup.cfg").write(py.code.Source(""" [pytest] addopts = --verbose """)) config = testdir.parseconfig(tmpdir) + del os.environ['PYTEST_ADDOPTS'] + assert config.option.color == 'no' + assert config.option.reportchars == 's' + assert config.option.tbstyle == 'short' assert config.option.verbose #config = testdir.Config() #args = [tmpdir,] From 8f12269db7f03760ba0ba5bb2d291ab219c5461d Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Mon, 26 Jan 2015 10:39:21 +0000 Subject: [PATCH 2/4] Use shlex to split the arguments from PYTEST_ADDOPTS. --HG-- branch : env-addopts --- _pytest/config.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/_pytest/config.py b/_pytest/config.py index 0d9beb3d3..f4be2d062 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -705,8 +705,7 @@ class Config(object): def _preparse(self, args, addopts=True): self._initini(args) if addopts: - env_addopts = os.environ.get('PYTEST_ADDOPTS', '') - args[:] = env_addopts.replace('"', '').split() + args + args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args args[:] = self.getini("addopts") + args self._checkversion() self.pluginmanager.consider_preparse(args) From 912c8f054088aecb614429873e6d96ba374c0ec4 Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Thu, 29 Jan 2015 10:52:01 +0000 Subject: [PATCH 3/4] Use monkeypatch to set the PYTEST_ADDOPTS environment variable in the test. --HG-- branch : env-addopts --- testing/test_config.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/testing/test_config.py b/testing/test_config.py index 32e03f0d6..9dc5e3f23 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -1,5 +1,3 @@ -import os - import py, pytest from _pytest.config import getcfg @@ -20,14 +18,13 @@ class TestParseIni: def test_getcfg_empty_path(self, tmpdir): getcfg([''], ['setup.cfg']) #happens on py.test "" - def test_append_parse_args(self, testdir, tmpdir): - os.environ['PYTEST_ADDOPTS'] = '--color no -rs --tb="short"' + def test_append_parse_args(self, testdir, tmpdir, monkeypatch): + monkeypatch.setenv('PYTEST_ADDOPTS', '--color no -rs --tb="short"') tmpdir.join("setup.cfg").write(py.code.Source(""" [pytest] addopts = --verbose """)) config = testdir.parseconfig(tmpdir) - del os.environ['PYTEST_ADDOPTS'] assert config.option.color == 'no' assert config.option.reportchars == 's' assert config.option.tbstyle == 'short' From d1adbf4a5cd972e73d96f092c6a980cd5773a7d3 Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Mon, 9 Feb 2015 14:11:54 +0000 Subject: [PATCH 4/4] Added documentation for PYTEST_ADDOPTS environment variable, updated CHANGELOG and AUTHORS. --HG-- branch : env-addopts --- AUTHORS | 1 + CHANGELOG | 2 ++ doc/en/customize.txt | 5 +++++ 3 files changed, 8 insertions(+) diff --git a/AUTHORS b/AUTHORS index dfa9684b1..7ea65a0d8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -46,3 +46,4 @@ Trevor Bekolay David Mohr Nicolas Delaby Tom Viner +Dave Hunt diff --git a/CHANGELOG b/CHANGELOG index f03f80889..50813920c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ 2.7.0.dev (compared to 2.6.4) ----------------------------- +- add ability to set command line options by environment variable PYTEST_ADDOPTS. + - fix issue655: work around different ways that cause python2/3 to leak sys.exc_info into fixtures/tests causing failures in 3rd party code diff --git a/doc/en/customize.txt b/doc/en/customize.txt index 74d30f7ab..d323dd638 100644 --- a/doc/en/customize.txt +++ b/doc/en/customize.txt @@ -60,6 +60,11 @@ progress output, you can write it into a configuration file:: [pytest] addopts = -rsxX -q +Alternatively, you can set a PYTEST_ADDOPTS environment variable to add command +line options while the environment is in use:: + + export PYTEST_ADDOPTS="-rsxX -q" + From now on, running ``pytest`` will add the specified options. Builtin configuration file options