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

module has no attribute config when accessing pytest.config in conftest #1688

Closed
3 of 4 tasks
quodlibetor opened this issue Jun 29, 2016 · 5 comments
Closed
3 of 4 tasks
Labels
type: bug problem that needs to be addressed

Comments

@quodlibetor
Copy link
Contributor

  • Include a detailed description of the bug or suggestion

I am doing some one-time initialization in a conftest.py that I would like to have relative to the rootdir. Theoretically I should be able to do:

# file myproj/tests/conftest.py
import conftest
data_dir = conftest.config.rootdir.join('some/dir')

And indeed, this usually succeeds:

$ pytest --collect-only -k test_get_tasks
========================================================= test session starts
platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /Users/bwm/projects/Xxx, inifile: tox.ini
plugins: cov-1.7.0, env-0.6.0
collected .. items
<Module 'xxx/tests/resource/test_tasks.py'>
  <Function 'test_get_tasks_works_with_empty_tasks[/tasks]'>
  <Function 'test_get_tasks'>

============================================== .. tests deselected by '-ktest_get_tasks'
==================================================== .. deselected in 1.02 seconds

But when actually running with a fully-specified path this fails:

$ py.test xxx/tests/resource/test_tasks.py
Traceback (most recent call last):
  File "/Users/bwm/findable/virtualenvs/xxx/lib/python2.7/site-packages/_pytest/config.py", line 320, in _importconftest
    mod = conftestpath.pyimport()
  File "/Users/bwm/findable/virtualenvs/xxx/lib/python2.7/site-packages/py/_path/local.py", line 650, in pyimport
    __import__(modname)
  File "/Users/bwm/projects/Xxx/xxx/tests/conftest.py", line 13, in <module>
    datadir = pytest.config.rootdir.join('xxx/tests/data')
AttributeError: 'module' object has no attribute 'config'
ERROR: could not load /Users/bwm/projects/Xxx/xxx/tests/conftest.py
  • Minimal example if possible

Trying to reproduce this, I'm unable to get it to succeed in any way:

$ tree
.
├── tests
│   ├── conftest.py
│   └── test_blar.py
└── tox.ini

$ cat tox.ini
[pytest]
testpaths = ./tests

$ cat tests/conftest.py
import pytest
pytest.config

$ cat tests/test_bar.py
def test_something():
    assert 1

Gives me the same error:

$ py.test
Traceback (most recent call last):
  File "/Users/bwm/findable/virtualenvs/tmp-fb94e9e979285d9f/lib/python2.7/site-packages/_pytest/config.py", line 543, in importconftest
    mod = conftestpath.pyimport()
  File "/Users/bwm/findable/virtualenvs/tmp-fb94e9e979285d9f/lib/python2.7/site-packages/py/_path/local.py", line 650, in pyimport
    __import__(modname)
  File "/Users/bwm/findable/virtualenvs/tmp-fb94e9e979285d9f/anything/tests/conftest.py", line 2, in <module>
    pytest.config
AttributeError: 'module' object has no attribute 'config'
ERROR: could not load /Users/bwm/findable/virtualenvs/tmp-fb94e9e979285d9f/anything/tests/conftest.py

No combination of options seems to be able to make this work. I'm no longer sure that this is supposed to work, but it seems weird that it works for me in my previously most-common use-case.

  • pip list of the virtual environment you are using
$ pip list
pip (8.0.2)
py (1.4.31)
pytest (2.7.2)  # also tested with 2.8.7 and 2.9.1
setuptools (20.0)
wheel (0.29.0)
  • py.test and operating system versions

py.test 2.7.2, 2.8.7, 2.9.1
OSX 10.9.5, Darwin 13.4.0

@nicoddemus
Copy link
Member

Thanks for the report!

While I agree this should be fixed, keep in mind that using pytest.config is discouraged.

For your use case I would recommend using a fixture instead:

import pytest

@pytest.fixture(scope='session')
def data_dir(pytestconfig):
    return pytestconfig.rootdir.join('some/dir')

@nicoddemus nicoddemus added the type: bug problem that needs to be addressed label Jun 29, 2016
@quodlibetor
Copy link
Contributor Author

Thanks for the fast response!

Is there a way to automatically use a fixture before any tests are run? Really that's what I'm trying to fake.

@The-Compiler
Copy link
Member

Is there a way to automatically use a fixture before any tests are run? Really that's what I'm trying to fake.

Sure! You can use an autouse session-scoped fixture:

@pytest.fixture(scope='session', autouse=True)
def fixt():
    # ...

See the fixture documentation for details.

@quodlibetor
Copy link
Contributor Author

Interesting! The documentation seemed, too me, to suggest that autoused fixtures would be run once for every function, no matter what their scope was. This works, though, thanks!

quodlibetor added a commit to quodlibetor/pytest that referenced this issue Jun 29, 2016
I wouldn't have even attempted what I did to cause pytest-dev#1688 if this had
been there.
@nicoddemus
Copy link
Member

Nice, thanks for the PR! 😁

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

3 participants