fix issue127 - improve pytest_addoption and related documentation
This commit is contained in:
		
							parent
							
								
									2637326782
								
							
						
					
					
						commit
						b8277bfed8
					
				|  | @ -7,6 +7,9 @@ Changes between 2.3.1 and 2.3.2.dev | ||||||
| 
 | 
 | ||||||
| - fix teardown-ordering for parametrized setups | - fix teardown-ordering for parametrized setups | ||||||
| 
 | 
 | ||||||
|  | - fix issue127 - better documentation for pytest_addoption | ||||||
|  |   and related objects. | ||||||
|  | 
 | ||||||
| - fix unittest behaviour: TestCase.runtest only called if there are | - fix unittest behaviour: TestCase.runtest only called if there are | ||||||
|   test methods defined |   test methods defined | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -19,7 +19,7 @@ def pytest_unconfigure(config): | ||||||
|         fin() |         fin() | ||||||
| 
 | 
 | ||||||
| class Parser: | class Parser: | ||||||
|     """ Parser for command line arguments. """ |     """ Parser for command line arguments and ini-file values.  """ | ||||||
| 
 | 
 | ||||||
|     def __init__(self, usage=None, processopt=None): |     def __init__(self, usage=None, processopt=None): | ||||||
|         self._anonymous = OptionGroup("custom options", parser=self) |         self._anonymous = OptionGroup("custom options", parser=self) | ||||||
|  | @ -38,9 +38,14 @@ class Parser: | ||||||
|     def getgroup(self, name, description="", after=None): |     def getgroup(self, name, description="", after=None): | ||||||
|         """ get (or create) a named option Group. |         """ get (or create) a named option Group. | ||||||
| 
 | 
 | ||||||
|         :name: unique name of the option group. |         :name: name of the option group. | ||||||
|         :description: long description for --help output. |         :description: long description for --help output. | ||||||
|         :after: name of other group, used for ordering --help output. |         :after: name of other group, used for ordering --help output. | ||||||
|  | 
 | ||||||
|  |         The returned group object has an ``addoption`` method with the same | ||||||
|  |         signature as :py:func:`parser.addoption | ||||||
|  |         <_pytest.config.Parser.addoption>` but will be shown in the | ||||||
|  |         respective group in the output of ``pytest. --help``. | ||||||
|         """ |         """ | ||||||
|         for group in self._groups: |         for group in self._groups: | ||||||
|             if group.name == name: |             if group.name == name: | ||||||
|  | @ -54,7 +59,19 @@ class Parser: | ||||||
|         return group |         return group | ||||||
| 
 | 
 | ||||||
|     def addoption(self, *opts, **attrs): |     def addoption(self, *opts, **attrs): | ||||||
|         """ add an optparse-style option. """ |         """ register a command line option. | ||||||
|  | 
 | ||||||
|  |         :opts: option names, can be short or long options. | ||||||
|  |         :attrs: same attributes which the ``add_option()`` function of the | ||||||
|  |            `optparse library | ||||||
|  |            <http://docs.python.org/library/optparse.html#module-optparse>`_ | ||||||
|  |            accepts. | ||||||
|  | 
 | ||||||
|  |         After command line parsing options are available on the pytest config | ||||||
|  |         object via ``config.option.NAME`` where ``NAME`` is usually set | ||||||
|  |         by passing a ``dest`` attribute, for example | ||||||
|  |         ``addoption("--long", dest="NAME", ...)``. | ||||||
|  |         """ | ||||||
|         self._anonymous.addoption(*opts, **attrs) |         self._anonymous.addoption(*opts, **attrs) | ||||||
| 
 | 
 | ||||||
|     def parse(self, args): |     def parse(self, args): | ||||||
|  | @ -75,7 +92,15 @@ class Parser: | ||||||
|         return args |         return args | ||||||
| 
 | 
 | ||||||
|     def addini(self, name, help, type=None, default=None): |     def addini(self, name, help, type=None, default=None): | ||||||
|         """ add an ini-file option with the given name and description. """ |         """ register an ini-file option. | ||||||
|  | 
 | ||||||
|  |         :name: name of the ini-variable | ||||||
|  |         :type: type of the variable, can be ``pathlist``, ``args`` or ``linelist``. | ||||||
|  |         :default: default value if no ini-file option exists but is queried. | ||||||
|  | 
 | ||||||
|  |         The value of ini-variables can be retrieved via a call to | ||||||
|  |         :py:func:`config.getini(name) <_pytest.config.Config.getini>`. | ||||||
|  |         """ | ||||||
|         assert type in (None, "pathlist", "args", "linelist") |         assert type in (None, "pathlist", "args", "linelist") | ||||||
|         self._inidict[name] = (help, type, default) |         self._inidict[name] = (help, type, default) | ||||||
|         self._ininames.append(name) |         self._ininames.append(name) | ||||||
|  | @ -246,8 +271,9 @@ class CmdOptions(object): | ||||||
| class Config(object): | class Config(object): | ||||||
|     """ access to configuration values, pluginmanager and plugin hooks.  """ |     """ access to configuration values, pluginmanager and plugin hooks.  """ | ||||||
|     def __init__(self, pluginmanager=None): |     def __init__(self, pluginmanager=None): | ||||||
|         #: command line option values, usually added via parser.addoption(...) |         #: command line option values, which must have been previously added | ||||||
|         #: or parser.getgroup(...).addoption(...) calls |         #: via calls like ``parser.addoption(...)`` or | ||||||
|  |         #: ``parser.getgroup(groupname).addoption(...)`` | ||||||
|         self.option = CmdOptions() |         self.option = CmdOptions() | ||||||
|         self._parser = Parser( |         self._parser = Parser( | ||||||
|             usage="usage: %prog [options] [file_or_dir] [file_or_dir] [...]", |             usage="usage: %prog [options] [file_or_dir] [file_or_dir] [...]", | ||||||
|  |  | ||||||
|  | @ -23,8 +23,10 @@ def pytest_cmdline_preparse(config, args): | ||||||
|     """modify command line arguments before option parsing. """ |     """modify command line arguments before option parsing. """ | ||||||
| 
 | 
 | ||||||
| def pytest_addoption(parser): | def pytest_addoption(parser): | ||||||
|     """add optparse-style options and ini-style config values via calls |     """use the parser to add optparse-style options and ini-style | ||||||
|     to ``parser.addoption`` and ``parser.addini(...)``. |     config values via calls, see :py:func:`parser.addoption(...) | ||||||
|  |     <_pytest.config.Parser.addoption>` | ||||||
|  |     and :py:func:`parser.addini(...) <_pytest.config.Parser.addini>`. | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
| def pytest_cmdline_main(config): | def pytest_cmdline_main(config): | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ Release announcements | ||||||
| .. toctree:: | .. toctree:: | ||||||
|    :maxdepth: 2 |    :maxdepth: 2 | ||||||
| 
 | 
 | ||||||
|  |    release-2.3.1 | ||||||
|    release-2.3.0 |    release-2.3.0 | ||||||
|    release-2.2.4 |    release-2.2.4 | ||||||
|    release-2.2.2 |    release-2.2.2 | ||||||
|  |  | ||||||
|  | @ -17,7 +17,7 @@ | ||||||
| # | # | ||||||
| # The full version, including alpha/beta/rc tags. | # The full version, including alpha/beta/rc tags. | ||||||
| # The short X.Y version. | # The short X.Y version. | ||||||
| version = release = "2.3.1" | version = release = "2.3.2.dev9" | ||||||
| 
 | 
 | ||||||
| import sys, os | import sys, os | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue