From a1295015f1448601607c74f7bafdfba92c145cb9 Mon Sep 17 00:00:00 2001 From: hpk Date: Mon, 2 Mar 2009 12:14:59 +0100 Subject: [PATCH] [svn r62348] add support fo setting command line options from PYTEST_OPTION_NAME environment vars add a first bit of documentation (merge of 62304:HEAD from the hostmanage branch) --HG-- branch : trunk --- py/doc/test-config.txt | 15 +++++++++++++++ py/test/config.py | 23 ++++++++++++++++++++++- py/test/testing/test_config.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 py/doc/test-config.txt diff --git a/py/doc/test-config.txt b/py/doc/test-config.txt new file mode 100644 index 000000000..9bde7f910 --- /dev/null +++ b/py/doc/test-config.txt @@ -0,0 +1,15 @@ + +Test configuration +======================== + +test option values +----------------------------- + +py.test will lookup the value of an option "NAME" in this order: + +* option value supplied at command line +* content of environment variable ``PYTEST_OPTION_NAME=...`` +* ``name = ...`` setting in the nearest ``conftest.py`` file. + +This means that you can specify default options per-run, +per shell session or per project directory. diff --git a/py/test/config.py b/py/test/config.py index 02170bccb..d2d16e469 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -1,4 +1,4 @@ -import py +import py, os from conftesthandle import Conftest from py.__.test import parseopt @@ -44,6 +44,17 @@ class Config(object): def _processopt(self, opt): if hasattr(opt, 'default') and opt.dest: + val = os.environ.get("PYTEST_OPTION_" + opt.dest.upper(), None) + if val is not None: + if opt.type == "int": + val = int(val) + elif opt.type == "long": + val = long(val) + elif opt.type == "float": + val = float(val) + elif not opt.type and opt.action in ("store_true", "store_false"): + val = eval(val) + opt.default = val if not hasattr(self.option, opt.dest): setattr(self.option, opt.dest, opt.default) @@ -147,6 +158,16 @@ class Config(object): def addoption(self, *optnames, **attrs): return self._parser.addoption(*optnames, **attrs) + def getvalueorskip(self, name, path=None): + """ return getvalue() or call py.test.skip if no value exists. """ + try: + val = self.getvalue(name, path) + if val is None: + raise KeyError(name) + return val + except KeyError: + py.test.skip("no %r value found" %(name,)) + def getvalue(self, name, path=None): """ return 'name' value looked up from the 'options' and then from the first conftest file found up diff --git a/py/test/testing/test_config.py b/py/test/testing/test_config.py index 607ba647e..817925d55 100644 --- a/py/test/testing/test_config.py +++ b/py/test/testing/test_config.py @@ -19,6 +19,26 @@ class TestConfigCmdlineParsing: config = py.test.config._reparse(['-G', '17']) assert config.option.gdest == 17 + def test_parser_addoption_default_env(self, testdir, monkeypatch): + import os + config = testdir.Config() + group = config._parser.addgroup("hello") + + monkeypatch.setitem(os.environ, 'PYTEST_OPTION_OPTION1', 'True') + group.addoption("--option1", action="store_true") + assert group.options[0].default == True + + monkeypatch.setitem(os.environ, 'PYTEST_OPTION_OPTION2', 'abc') + group.addoption("--option2", action="store", default="x") + assert group.options[1].default == "abc" + + monkeypatch.setitem(os.environ, 'PYTEST_OPTION_OPTION3', '32') + group.addoption("--option3", action="store", type="int") + assert group.options[2].default == 32 + + group.addoption("--option4", action="store", type="int") + assert group.options[3].default == ("NO", "DEFAULT") + def test_config_cmdline_options_only_lowercase(self, testdir): testdir.makepyfile(conftest=""" import py @@ -79,6 +99,15 @@ class TestConfigAPI: assert config.getvalue("x", o) == 1 py.test.raises(KeyError, 'config.getvalue("y", o)') + def test_config_getvalueorskip(self, testdir): + from py.__.test.outcome import Skipped + config = testdir.parseconfig() + py.test.raises(Skipped, "config.getvalueorskip('hello')") + verbose = config.getvalueorskip("verbose") + assert verbose == config.option.verbose + config.option.hello = None + py.test.raises(Skipped, "config.getvalueorskip('hello')") + def test_config_overwrite(self, testdir): o = testdir.tmpdir o.ensure("conftest.py").write("x=1")