introduce a pytest_cmdline_processargs hook to modify/add dynamically to command line arguments.

This commit is contained in:
holger krekel 2010-12-07 12:14:12 +01:00
parent e6541ed14e
commit 7db9e98b55
7 changed files with 58 additions and 7 deletions

View File

@ -1,6 +1,11 @@
Changes between 2.0.0 and 2.0.1.dev1 Changes between 2.0.0 and 2.0.1.dev1
---------------------------------------------- ----------------------------------------------
- introduce a pytest_cmdline_processargs(args) hook
to allow dynamic computation of command line arguments.
This fixes a regression because py.test prior to 2.0
allowed to set command line options from conftest.py
files which so far pytest-2.0 only allowed from ini-files now.
- fix issue7: assert failures in doctest modules. - fix issue7: assert failures in doctest modules.
unexpected failures in doctests will not generally unexpected failures in doctests will not generally
show nicer, i.e. within the doctest failing context. show nicer, i.e. within the doctest failing context.

View File

@ -305,6 +305,8 @@ class Config(object):
self.pluginmanager.consider_env() self.pluginmanager.consider_env()
self._setinitialconftest(args) self._setinitialconftest(args)
self.pluginmanager.do_addoption(self._parser) self.pluginmanager.do_addoption(self._parser)
if addopts:
self.hook.pytest_cmdline_processargs(config=self, args=args)
def _checkversion(self): def _checkversion(self):
minver = self.inicfg.get('minversion', None) minver = self.inicfg.get('minversion', None)

View File

@ -19,6 +19,9 @@ def pytest_cmdline_parse(pluginmanager, args):
"""return initialized config object, parsing the specified args. """ """return initialized config object, parsing the specified args. """
pytest_cmdline_parse.firstresult = True pytest_cmdline_parse.firstresult = True
def pytest_cmdline_processargs(config, args):
"""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 """add optparse-style options and ini-style config values via calls
to ``parser.addoption`` and ``parser.addini(...)``. to ``parser.addoption`` and ``parser.addini(...)``.
@ -202,7 +205,6 @@ def pytest_doctest_prepare_content(content):
""" return processed content for a given doctest""" """ return processed content for a given doctest"""
pytest_doctest_prepare_content.firstresult = True pytest_doctest_prepare_content.firstresult = True
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# error handling and internal debugging hooks # error handling and internal debugging hooks
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------

View File

@ -40,7 +40,7 @@ clean:
-rm -rf $(BUILDDIR)/* -rm -rf $(BUILDDIR)/*
install: clean html install: clean html
rsync -avz _build/html/ code:www-pytest/ rsync -avz _build/html/ code:www-pytest/2.0.1dev
html: html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html

View File

@ -138,11 +138,42 @@ let's run the full monty::
E assert 4 < 4 E assert 4 < 4
test_compute.py:3: AssertionError test_compute.py:3: AssertionError
1 failed, 4 passed in 0.03 seconds 1 failed, 4 passed in 0.02 seconds
As expected when running the full range of ``param1`` values As expected when running the full range of ``param1`` values
we'll get an error on the last one. we'll get an error on the last one.
dynamically adding command line options
--------------------------------------------------------------
.. regendoc:wipe
Through :confval:`addopts` you can statically add command line
options for your project. You can also dynamically modify
the command line arguments before they get processed::
# content of conftest.py
import sys
def pytest_cmdline_processargs(args):
if 'xdist' in sys.modules: # pytest-xdist plugin
import multiprocessing
num = max(multiprocessing.cpu_count() / 2, 1)
args[:] = ["-n", str(num)] + args
If you have the :ref:`xdist plugin <xdist>` installed
you will now always perform test runs using a number
of subprocesses close to your CPU. Running in an empty
directory with the above conftest.py::
$ py.test
=========================== test session starts ============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev3
gw0 I / gw1 I / gw2 I / gw3 I
gw0 [0] / gw1 [0] / gw2 [0] / gw3 [0]
scheduling tests via LoadScheduling
============================= in 0.29 seconds =============================
.. _`retrieved by hooks as item keywords`: .. _`retrieved by hooks as item keywords`:
@ -183,12 +214,12 @@ and when running it will see a skipped "slow" test::
$ py.test -rs # "-rs" means report details on the little 's' $ py.test -rs # "-rs" means report details on the little 's'
=========================== test session starts ============================ =========================== test session starts ============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.0 platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev3
collecting ... collected 2 items collecting ... collected 2 items
test_module.py .s test_module.py .s
========================= short test summary info ========================== ========================= short test summary info ==========================
SKIP [1] /tmp/doc-exec-479/conftest.py:9: need --runslow option to run SKIP [1] /tmp/doc-exec-25/conftest.py:9: need --runslow option to run
=================== 1 passed, 1 skipped in 0.02 seconds ==================== =================== 1 passed, 1 skipped in 0.02 seconds ====================
@ -196,7 +227,7 @@ Or run it including the ``slow`` marked test::
$ py.test --runslow $ py.test --runslow
=========================== test session starts ============================ =========================== test session starts ============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.0 platform linux2 -- Python 2.6.5 -- pytest-2.0.1.dev3
collecting ... collected 2 items collecting ... collected 2 items
test_module.py .. test_module.py ..

View File

@ -271,6 +271,7 @@ initialisation, command line and configuration hooks
.. currentmodule:: _pytest.hookspec .. currentmodule:: _pytest.hookspec
.. autofunction:: pytest_cmdline_processargs
.. autofunction:: pytest_cmdline_parse .. autofunction:: pytest_cmdline_parse
.. autofunction:: pytest_namespace .. autofunction:: pytest_namespace
.. autofunction:: pytest_addoption .. autofunction:: pytest_addoption

View File

@ -82,7 +82,6 @@ class TestConfigCmdlineParsing:
class TestConfigAPI: class TestConfigAPI:
def test_config_trace(self, testdir): def test_config_trace(self, testdir):
config = testdir.Config() config = testdir.Config()
l = [] l = []
@ -252,3 +251,14 @@ def test_plugin_preparse_prevents_setuptools_loading(testdir, monkeypatch):
config = testdir.parseconfig("-p", "no:mytestplugin") config = testdir.parseconfig("-p", "no:mytestplugin")
plugin = config.pluginmanager.getplugin("mytestplugin") plugin = config.pluginmanager.getplugin("mytestplugin")
assert plugin == -1 assert plugin == -1
def test_cmdline_processargs_simple(testdir):
testdir.makeconftest("""
def pytest_cmdline_processargs(args):
args.append("-h")
""")
result = testdir.runpytest()
result.stdout.fnmatch_lines([
"*pytest*",
"*-h*",
])