Skip to content

Question: How can I import some variable from parent conftest.py when I have another conftest.py in local directory. #3272

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

Closed
thakkardharmik opened this issue Feb 28, 2018 · 4 comments
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: question general question, might be closed after 2 weeks of inactivity

Comments

@thakkardharmik
Copy link

thakkardharmik commented Feb 28, 2018

Lets say I have the directory structure as follow -

Parent
|________ __init__.py
|________ conftest.py
|________ A
|________ B
               |______ __init__.py
               |______ conftest.py
               |______ test_a.py

Is there a way I can import some variable from Parent conftest.py in test_a.py.

@RonnyPfannschmidt
Copy link
Member

your in your example normal python import ruls apply

so in test_a.py a from ..conftest import * sould suffice

note that commonly its considered "bad" to do imports from conftests - if you could outline your use-case in more detail we might be able to propose a solution which is considered more elegant by the pytest developers

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Feb 28, 2018
@thakkardharmik
Copy link
Author

@RonnyPfannschmidt If I try to use ...conftest it throws error saying ValueError: Attempted relative import beyond toplevel package. This error is seen for with n number of dots. If I dont put the dots its says variable not found.

@nicoddemus
Copy link
Member

This works for me:

├──parent
│  ├──B
│  │  ├  conftest.py
│  │  ├  test_a.py
│  │  └  __init__.py
│  ├  conftest.py
│  └  __init__.py
└  pytest.ini
# parent/B/test_a.py
from ..conftest import V

def test_a():
    assert V == 1
# parent/conftest.py
V = 1
================================================= test session starts =================================================
platform win32 -- Python 3.6.3, pytest-3.4.2.dev23+g14a9b1e, py-1.5.2, pluggy-0.6.1.dev
rootdir: C:\pytest\.tmp\import, inifile: pytest.ini
plugins: forked-0.2, hypothesis-3.38.9
collected 1 item
parent\B\test_a.py .                                                                                             [100%]

============================================== 1 passed in 0.01 seconds ===============================================
Process finished with exit code 0

Can you provide a minimal reproducible example?

@RonnyPfannschmidt RonnyPfannschmidt added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Mar 1, 2018
@thakkardharmik
Copy link
Author

@nicoddemus @RonnyPfannschmidt It worked for me. I had missing init.py in parent directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants