Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skipif documentation has broken example using pytest.config #472

Closed
pytestbot opened this issue Mar 4, 2014 · 1 comment
Closed

skipif documentation has broken example using pytest.config #472

pytestbot opened this issue Mar 4, 2014 · 1 comment
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Wichert Akkerman (BitBucket: wichert, GitHub: wichert)


The skip and xfail documentation has an example at the end of the page showing how to use pytest.config to make a skip conditional on the test runner configuration:

@pytest.mark.skipif(not pytest.config.getvalue("db"),
                    reason="--db was not specified")
def test_function(...):
    pass

I tried to use that mechanism to make a test conditional on the use of PostgreSQL with tests:

def pytest_generate_tests(metafunc):
    if 'sqlalchemy_url' in metafunc.fixturenames:
        metafunc.parametrize('sqlalchemy_url', [metafunc.config.option.database])

require_postgres = pytest.mark.skipif(
        not pytest.config.getvalue('database').startswith('postgresql'),
        reason='PostgreSQL database required')

When I try to run my tests with this I get the following error:

Traceback (most recent call last):
  File "bin/py.test", line 62, in <module>
    sys.exit(pytest.main())
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 19, in main
    config = _prepareconfig(args, plugins)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 63, in _prepareconfig
    pluginmanager=pluginmanager, args=args)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 377, in __call__
    return self._docall(methods, kwargs)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 388, in _docall
    res = mc.execute()
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 289, in execute
    res = method(**kwargs)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/helpconfig.py", line 27, in pytest_cmdline_parse
    config = __multicall__.execute()
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 289, in execute
    res = method(**kwargs)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 618, in pytest_cmdline_parse
    self.parse(args)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 711, in parse
    self._preparse(args)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 691, in _preparse
    args=args, parser=self._parser)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 377, in __call__
    return self._docall(methods, kwargs)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 388, in _docall
    res = mc.execute()
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 289, in execute
    res = method(**kwargs)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/capture.py", line 83, in pytest_load_initial_conftests
    return __multicall__.execute()
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/core.py", line 289, in execute
    res = method(**kwargs)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 674, in pytest_load_initial_conftests
    self._conftest.setinitial(args)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 487, in setinitial
    self._try_load_conftest(anchor)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 493, in _try_load_conftest
    self._path2confmods[None] = self.getconftestmodules(anchor)
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 512, in getconftestmodules
    clist.append(self.importconftest(conftestpath))
  File "/Users/wichert/Library/buildout/eggs/pytest-2.5.2-py2.7.egg/_pytest/config.py", line 538, in importconftest
    self._conftestpath2mod[conftestpath] = mod = conftestpath.pyimport()
  File "/Users/wichert/Library/buildout/eggs/py-1.4.20-py2.7.egg/py/_path/local.py", line 608, in pyimport
    __import__(pkgpath.basename)
  File "/Users/wichert/myapp/tests/__init__.py", line 5, in <module>
    not pytest.config.getvalue('database').startswith('postgres'),
AttributeError: 'module' object has no attribute 'config'

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


fix issue472: clarify that pytest.config.getvalue() cannot work
if it's triggered ahead of command line parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant