[py-svn] r62171 - in py/branch/pytestplugin/py/test: . plugin testing
hpk at codespeak.net
hpk at codespeak.net
Thu Feb 26 01:02:59 CET 2009
Author: hpk
Date: Thu Feb 26 01:02:58 2009
New Revision: 62171
Added:
py/branch/pytestplugin/py/test/parseopt.py
py/branch/pytestplugin/py/test/testing/test_parseopt.py
Modified:
py/branch/pytestplugin/py/test/plugin/pytest_doctest.py
Log:
beginning a lean option parser as a small wrapper around optparse
Added: py/branch/pytestplugin/py/test/parseopt.py
==============================================================================
--- (empty file)
+++ py/branch/pytestplugin/py/test/parseopt.py Thu Feb 26 01:02:58 2009
@@ -0,0 +1,85 @@
+"""
+thin wrapper around Python's optparse.py
+adding some extra checks and ways to systematically
+have Environment variables provide default values
+for options. basic usage:
+
+ >>> parser = Parser()
+ >>> parser.addoption("--hello", action="store_true", dest="hello")
+ >>> option, args = parser.parse(['--hello'])
+ >>> option.hello
+ True
+ >>> args
+ []
+
+"""
+import py
+from py.compat import optparse
+
+class Parser:
+ """ Parser for command line arguments. """
+
+ def __init__(self):
+ self._anonymous = OptionGroup("misc")
+ self._groups = [self._anonymous]
+
+ def addgroup(self, name, description=""):
+ for group in self._groups:
+ if group.name == name:
+ raise ValueError("group %r already exists" % name)
+ group = OptionGroup(name, description)
+ self._groups.append(group)
+ return group
+
+ def getgroup(self, name):
+ for group in self._groups:
+ if group.name == name:
+ return group
+ raise ValueError("group %r not found" %(name,))
+
+ def addoption(self, *opts, **attrs):
+ """ add an optparse-style option. """
+ self._anonymous.addoption(*opts, **attrs)
+
+ def parse(self, args):
+ optparser = optparse.OptionParser()
+ for group in self._groups:
+ if group.options:
+ optgroup = optparse.OptionGroup(optparser, group.name)
+ optgroup.add_options(group.options)
+ optparser.add_option_group(optgroup)
+ return optparser.parse_args(args)
+
+ def addoptions(self, *specs):
+ """ add a named group of options to the current testing session.
+ This function gets invoked during testing session initialization.
+ """
+ for spec in specs:
+ for shortopt in spec._short_opts:
+ if not shortopt.isupper():
+ raise ValueError(
+ "custom options must be capital letter "
+ "got %r" %(spec,)
+ )
+ return self._addoptions(groupname, *specs)
+
+
+ def _addoptions(self, groupname, *specs):
+ optgroup = optparse.OptionGroup(self._parser, groupname)
+ optgroup.add_options(specs)
+ self._parser.add_option_group(optgroup)
+ for opt in specs:
+ if hasattr(opt, 'default') and opt.dest:
+ if not hasattr(self.option, opt.dest):
+ setattr(self.option, opt.dest, opt.default)
+ return self.option
+
+class OptionGroup:
+ def __init__(self, name, description=""):
+ self.name = name
+ self.description = description
+ self.options = []
+
+ def addoption(self, *optnames, **attrs):
+ option = py.compat.optparse.Option(*optnames, **attrs)
+ self.options.append(option)
Modified: py/branch/pytestplugin/py/test/plugin/pytest_doctest.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_doctest.py (original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_doctest.py Thu Feb 26 01:02:58 2009
@@ -50,7 +50,8 @@
doctestfailure.got, REPORT_UDIFF).split("\n")
return ReprFailDoctest(reprlocation, lines)
elif excinfo.errisinstance(py.compat.doctest.UnexpectedException):
- return # XXX
+ excinfo = py.code.ExceptionInfo(excinfo.value.exc_info)
+ return super(DoctestItem, self).repr_failure(excinfo, outerr)
else:
return super(DoctestItem, self).repr_failure(excinfo, outerr)
@@ -109,7 +110,6 @@
ev, = events.getnamed("itemtestreport")
assert ev.failed
- @py.test.keywords(xfail=True)
def test_doctest_unexpected_exception(self, testdir):
from py.__.test.outcome import Failed
Added: py/branch/pytestplugin/py/test/testing/test_parseopt.py
==============================================================================
--- (empty file)
+++ py/branch/pytestplugin/py/test/testing/test_parseopt.py Thu Feb 26 01:02:58 2009
@@ -0,0 +1,34 @@
+import py
+from py.__.test import parseopt
+
+class TestParser:
+ def test_group_add_and_get(self):
+ parser = parseopt.Parser()
+ group = parser.addgroup("hello", description="desc")
+ assert group.name == "hello"
+ assert group.description == "desc"
+ py.test.raises(ValueError, parser.addgroup, "hello")
+ group2 = parser.getgroup("hello")
+ assert group2 is group
+ py.test.raises(ValueError, parser.getgroup, 'something')
+
+ def test_group_addoption(self):
+ group = parseopt.OptionGroup("hello")
+ group.addoption("--option1", action="store_true")
+ assert len(group.options) == 1
+ assert isinstance(group.options[0], py.compat.optparse.Option)
+
+ def test_parser_addoption(self):
+ parser = parseopt.Parser()
+ group = parser.getgroup("misc")
+ assert len(group.options) == 0
+ group.addoption("--option1", action="store_true")
+ assert len(group.options) == 1
+
+ def test_parse(self):
+ parser = parseopt.Parser()
+ parser.addoption("--hello", dest="hello", action="store")
+ option, args = parser.parse(['--hello', 'world'])
+ assert option.hello == "world"
+ assert not args
+
More information about the pytest-commit
mailing list