add and document new parser.addini(name, description) method to describe
ini-values. Also document the parser object with its public methods. --HG-- branch : trunk
This commit is contained in:
parent
2d8bcbdf55
commit
1280041f0c
|
@ -354,6 +354,9 @@ Reference of important objects involved in hooks
|
|||
.. autoclass:: pytest.plugin.config.Config
|
||||
:members:
|
||||
|
||||
.. autoclass:: pytest.plugin.config.Parser
|
||||
:members:
|
||||
|
||||
.. autoclass:: pytest.plugin.session.Item
|
||||
:inherited-members:
|
||||
|
||||
|
@ -367,6 +370,7 @@ Reference of important objects involved in hooks
|
|||
:members:
|
||||
|
||||
|
||||
|
||||
conftest.py configuration files
|
||||
=================================================
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ function arguments:
|
|||
|
||||
- :ref:`mysetup`
|
||||
- `application setup in test functions with funcargs`_
|
||||
- `making funcargs dependendent on command line options`_
|
||||
- `monkey patching done right`_ (blog post, consult `monkeypatch
|
||||
plugin`_ for actual 1.0 API)
|
||||
|
||||
|
@ -40,7 +39,6 @@ plugin specific examples:
|
|||
- `many examples in the docs for plugins`_
|
||||
|
||||
.. _`skipping slow tests by default in py.test`: http://bruynooghe.blogspot.com/2009/12/skipping-slow-test-by-default-in-pytest.html
|
||||
.. _`making funcargs dependendent on command line options`: funcargs.html#tut-cmdlineoption
|
||||
.. _`many examples in the docs for plugins`: plugin/index.html
|
||||
.. _`monkeypatch plugin`: plugin/monkeypatch.html
|
||||
.. _`application setup in test functions with funcargs`: funcargs.html#appsetup
|
||||
|
|
|
@ -10,6 +10,10 @@ def pytest_cmdline_parse(pluginmanager, args):
|
|||
config.parse(args)
|
||||
return config
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addini('addargs', 'extra command line arguments')
|
||||
parser.addini('minversion', 'minimally required pytest version')
|
||||
|
||||
class Parser:
|
||||
""" Parser for command line arguments. """
|
||||
|
||||
|
@ -18,6 +22,7 @@ class Parser:
|
|||
self._groups = []
|
||||
self._processopt = processopt
|
||||
self._usage = usage
|
||||
self._inidict = {}
|
||||
self.hints = []
|
||||
|
||||
def processoption(self, option):
|
||||
|
@ -29,6 +34,12 @@ class Parser:
|
|||
self._notes.append(note)
|
||||
|
||||
def getgroup(self, name, description="", after=None):
|
||||
""" get (or create) a named option Group.
|
||||
|
||||
:name: unique name of the option group.
|
||||
:description: long description for --help output.
|
||||
:after: name of other group, used for ordering --help output.
|
||||
"""
|
||||
for group in self._groups:
|
||||
if group.name == name:
|
||||
return group
|
||||
|
@ -61,6 +72,14 @@ class Parser:
|
|||
setattr(option, name, value)
|
||||
return args
|
||||
|
||||
def addini(self, name, description):
|
||||
""" add an ini-file option with the given name and description. """
|
||||
self._inidict[name] = description
|
||||
|
||||
def setfromini(self, inisection, option):
|
||||
for name, value in inisection.items():
|
||||
assert name in self._inidict
|
||||
return setattr(option, name, value)
|
||||
|
||||
class OptionGroup:
|
||||
def __init__(self, name, description="", parser=None):
|
||||
|
@ -254,7 +273,6 @@ class Config(object):
|
|||
if not hasattr(self.option, opt.dest):
|
||||
setattr(self.option, opt.dest, opt.default)
|
||||
|
||||
|
||||
def _getmatchingplugins(self, fspath):
|
||||
allconftests = self._conftest._conftestpath2mod.values()
|
||||
plugins = [x for x in self.pluginmanager.getplugins()
|
||||
|
@ -305,13 +323,14 @@ class Config(object):
|
|||
minver, pytest.__version__))
|
||||
|
||||
def parse(self, args):
|
||||
# cmdline arguments into this config object.
|
||||
# parse given cmdline arguments into this config object.
|
||||
# Note that this can only be called once per testing process.
|
||||
assert not hasattr(self, 'args'), (
|
||||
"can only parse cmdline args at most once per Config object")
|
||||
self._preparse(args)
|
||||
self._parser.hints.extend(self.pluginmanager._hints)
|
||||
args = self._parser.parse_setoption(args, self.option)
|
||||
self._parser.setfromini(self.inicfg, self.option)
|
||||
if not args:
|
||||
args.append(py.std.os.getcwd())
|
||||
self.args = args
|
||||
|
|
|
@ -7,7 +7,7 @@ import inspect, sys
|
|||
def pytest_addoption(parser):
|
||||
group = parser.getgroup('debugconfig')
|
||||
group.addoption('--version', action="store_true",
|
||||
help="display py lib version and import information.")
|
||||
help="display pytest lib version and import information.")
|
||||
group._addoption("-h", "--help", action="store_true", dest="help",
|
||||
help="show help message and configuration info")
|
||||
group._addoption('-p', action="append", dest="plugins", default = [],
|
||||
|
@ -40,12 +40,12 @@ def showhelp(config):
|
|||
tw.write(config._parser.optparser.format_help())
|
||||
tw.line()
|
||||
tw.line()
|
||||
tw.sep( "=", "ini-settings")
|
||||
tw.sep( "=", "config file settings")
|
||||
tw.line("the following values can be defined in [pytest] sections of")
|
||||
tw.line("setup.cfg or tox.ini files:")
|
||||
tw.line()
|
||||
|
||||
for name, help in ini_settings:
|
||||
for name, help in sorted(config._parser._inidict.items()):
|
||||
line = " %-15s %s" %(name, help)
|
||||
tw.line(line[:tw.fullwidth])
|
||||
|
||||
|
@ -59,10 +59,6 @@ def showhelp(config):
|
|||
tw.line()
|
||||
tw.sep( "=")
|
||||
|
||||
ini_settings = (
|
||||
('addargs', 'extra command line arguments'),
|
||||
('minversion', 'minimally required pytest version'),
|
||||
)
|
||||
|
||||
conftest_options = (
|
||||
('pytest_plugins', 'list of plugin names to load'),
|
||||
|
|
|
@ -15,7 +15,7 @@ def test_help(testdir):
|
|||
assert result.ret == 0
|
||||
result.stdout.fnmatch_lines([
|
||||
"*-v*verbose*",
|
||||
"*ini-settings*",
|
||||
"*settings*",
|
||||
"*conftest.py*",
|
||||
])
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import py
|
||||
from pytest.plugin import config as parseopt
|
||||
from textwrap import dedent
|
||||
|
||||
class TestParser:
|
||||
def test_no_help_by_default(self, capsys):
|
||||
|
@ -100,6 +101,18 @@ class TestParser:
|
|||
assert option.hello == "world"
|
||||
assert option.this == 42
|
||||
|
||||
def test_parser_addini(self, tmpdir):
|
||||
parser = parseopt.Parser()
|
||||
parser.addini("myname", "my new ini value")
|
||||
cfg = py.iniconfig.IniConfig("tox.ini", dedent("""
|
||||
[pytest]
|
||||
myname=hello
|
||||
"""))['pytest']
|
||||
class option:
|
||||
pass
|
||||
parser.setfromini(cfg, option)
|
||||
assert option.myname == "hello"
|
||||
|
||||
@py.test.mark.skipif("sys.version_info < (2,5)")
|
||||
def test_addoption_parser_epilog(testdir):
|
||||
testdir.makeconftest("""
|
||||
|
|
Loading…
Reference in New Issue