[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
This commit is contained in:
parent
8a1ee954f8
commit
a1295015f1
|
@ -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.
|
|
@ -1,4 +1,4 @@
|
||||||
import py
|
import py, os
|
||||||
from conftesthandle import Conftest
|
from conftesthandle import Conftest
|
||||||
|
|
||||||
from py.__.test import parseopt
|
from py.__.test import parseopt
|
||||||
|
@ -44,6 +44,17 @@ class Config(object):
|
||||||
|
|
||||||
def _processopt(self, opt):
|
def _processopt(self, opt):
|
||||||
if hasattr(opt, 'default') and opt.dest:
|
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):
|
if not hasattr(self.option, opt.dest):
|
||||||
setattr(self.option, opt.dest, opt.default)
|
setattr(self.option, opt.dest, opt.default)
|
||||||
|
|
||||||
|
@ -147,6 +158,16 @@ class Config(object):
|
||||||
def addoption(self, *optnames, **attrs):
|
def addoption(self, *optnames, **attrs):
|
||||||
return self._parser.addoption(*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):
|
def getvalue(self, name, path=None):
|
||||||
""" return 'name' value looked up from the 'options'
|
""" return 'name' value looked up from the 'options'
|
||||||
and then from the first conftest file found up
|
and then from the first conftest file found up
|
||||||
|
|
|
@ -19,6 +19,26 @@ class TestConfigCmdlineParsing:
|
||||||
config = py.test.config._reparse(['-G', '17'])
|
config = py.test.config._reparse(['-G', '17'])
|
||||||
assert config.option.gdest == 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):
|
def test_config_cmdline_options_only_lowercase(self, testdir):
|
||||||
testdir.makepyfile(conftest="""
|
testdir.makepyfile(conftest="""
|
||||||
import py
|
import py
|
||||||
|
@ -79,6 +99,15 @@ class TestConfigAPI:
|
||||||
assert config.getvalue("x", o) == 1
|
assert config.getvalue("x", o) == 1
|
||||||
py.test.raises(KeyError, 'config.getvalue("y", o)')
|
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):
|
def test_config_overwrite(self, testdir):
|
||||||
o = testdir.tmpdir
|
o = testdir.tmpdir
|
||||||
o.ensure("conftest.py").write("x=1")
|
o.ensure("conftest.py").write("x=1")
|
||||||
|
|
Loading…
Reference in New Issue