[py-svn] r62348 - in py/trunk/py: doc test test/testing

hpk at codespeak.net hpk at codespeak.net
Mon Mar 2 12:15:00 CET 2009


Author: hpk
Date: Mon Mar  2 12:14:59 2009
New Revision: 62348

Added:
   py/trunk/py/doc/test-config.txt
      - copied unchanged from r62347, user/hpk/branch/hostmanage/py/doc/test-config.txt
Modified:
   py/trunk/py/test/config.py
   py/trunk/py/test/testing/test_config.py
Log:
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)


Modified: py/trunk/py/test/config.py
==============================================================================
--- py/trunk/py/test/config.py	(original)
+++ py/trunk/py/test/config.py	Mon Mar  2 12:14:59 2009
@@ -1,4 +1,4 @@
-import py
+import py, os
 from conftesthandle import Conftest
 
 from py.__.test import parseopt
@@ -44,6 +44,17 @@
 
     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 @@
     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 

Modified: py/trunk/py/test/testing/test_config.py
==============================================================================
--- py/trunk/py/test/testing/test_config.py	(original)
+++ py/trunk/py/test/testing/test_config.py	Mon Mar  2 12:14:59 2009
@@ -19,6 +19,26 @@
         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 @@
         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")



More information about the pytest-commit mailing list