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

Error with own 'conf' argument with 'tests/ANY' value #2077

Closed
ondratu opened this issue Nov 23, 2016 · 6 comments
Closed

Error with own 'conf' argument with 'tests/ANY' value #2077

ondratu opened this issue Nov 23, 2016 · 6 comments
Labels
type: bug problem that needs to be addressed

Comments

@ondratu
Copy link

ondratu commented Nov 23, 2016

Hi,

there is problem/bug, when i create own --conf argument, and i set to this argument value, starts with tests/ANY value.

I attached simple example of tests directory. There is conftest.py, when I define two own tests options. One --conf and one --konf. All arguments have default value tests/file.ini. When I use this arguments from command line, all work, but --conf tests/anyfile not. If the value string starts with absolute path, all work, only tests/X do the problem.

~$ pytest --konf=tests/file.ini  # this work
=========================== test session starts ============================
platform linux2 -- Python 2.7.12+, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: /home/mcbig/tmp/pytest.err, inifile: 
collected 1 items 

tests/test_pytest.py .

========================= 1 passed in 0.00 seconds =========================

$ pytest --conf=tests/file.ini  # this does not work
=========================== test session starts ============================
platform linux2 -- Python 2.7.12+, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
rootdir: /home/mcbig/tmp/pytest.err, inifile: 
collected 1 items 

tests/test_pytest.py F

================================= FAILURES =================================
_______________________________ test_params ________________________________

    def test_params():
>       conf = pytest.config.option.conf
E       AttributeError: 'CmdOptions' object has no attribute 'conf'

tests/test_pytest.py:4: AttributeError
========================= 1 failed in 0.01 seconds =========================

My environment is:

  • OS: Linux Debian Stretch (error work on Linux Debian Wheezy)
  • Python: 2.7.12+
  • pytest (3.0.3) (same problem on older versions)

~$ pip list:

beautifulsoup4 (4.5.1)
chardet (2.3.0)
configparser (3.3.0.post2)
cryptography (1.5.2)
cycler (0.10.0)
docutils (0.12)
enum34 (1.1.6)
future (0.15.2)
GDAL (2.1.2)
html5lib (0.999)
httplib2 (0.9.2)
idna (2.1)
ipaddress (1.0.17)
Jinja2 (2.8)
lxml (3.6.4)
MarkupSafe (0.23)
matplotlib (1.5.3)
ndg-httpsclient (0.4.2)
numpy (1.11.2)
pexpect (4.2.0)
Pillow (3.4.2)
pip (8.1.2)
psycopg2 (2.6.2)
ptyprocess (0.5.1)
py (1.4.31)
pyasn1 (0.1.9)
pycrypto (2.6.1)
pycurl (7.43.0)
Pygments (2.1.3)
pygobject (3.22.0)
pyOpenSSL (16.2.0)
pyparsing (2.1.10)
pyspatialite (3.0.1)
pytest (3.0.3)
python-apt (1.1.0b5)
python-dateutil (2.5.3)
python-debian (0.1.29)
python-debianbts (2.6.1)
pytz (2015.7)
PyYAML (3.12)
reportbug (6.6.6)
requests (2.11.1)
setuptools (28.7.1)
Shapely (1.5.17)
six (1.10.0)
urllib3 (1.16)
wheel (0.29.0)

tests/conftest.py

def pytest_addoption(parser):
    parser.addoption("--conf", type=str,
                     default="tests/file.ini",
                     help="use config file")
    parser.addoption("--konf", type=str,
                     default="tests/file.ini",
                     help="use config file")

tests/test_pytest.py

import pytest

def test_params():
    conf = pytest.config.option.conf
    print conf
@nicoddemus
Copy link
Member

Thanks for the detailed report!

It seems your option is being swallowed by pytest's --confcutdir option:

import pytest

def test_params():
    print('confcutdir', pytest.config.option.confcutdir)
$ pytest tests\test_pytest.py --conf=tests/file.ini -s -q
confcutdir tests/file.ini

Could be a bug in pytest or in argparse, but it seems it is in pytest:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--confcutdir', dest="confcutdir", default=None,
                    metavar="dir",
                    help="only load conftest.py's relative to specified dir.")
parser.add_argument("--conf", type=str,
                    default="tests/file.ini",
                    help="use config file")
args = parser.parse_args()
print('conf=', args.conf)
print('confcutdir=', args.confcutdir)
$ python args.py --conf=tests/file.ini
conf= tests/file.ini
confcutdir= None

@nicoddemus nicoddemus added the type: bug problem that needs to be addressed label Nov 23, 2016
@nicoddemus
Copy link
Member

It seems it is related to the fact that pytest_addoption in a conftest.py file is a problem. pytest_addoption is only executed on conftest.py files near the root. From the docs:

Note that pytest does not find conftest.py files in deeper nested sub directories at tool startup. It is usually a good idea to keep your conftest.py file in the top level test or project root directory.

If I change your conftest.py file to:

def pytest_addoption(parser):
    assert 0
    ...

You will see the hook is not called when we pass --conf=tests/file.ini:

$ pytest tests\test_pytest.py --conf=tests/file.ini -q -s
.
1 passed in 0.00 seconds
$ pytest tests\test_pytest.py --konf=tests/file.ini -q -s
  ...
  File "C:\pytest\.tmp\tests\conftest.py", line 2, in pytest_addoption
    assert 0
AssertionError: assert 0

So the problem is that your option is being interpreted as --confcutdir, which limits at which point conftest.py in the file hierarchy conftest files can be loaded, and that causes your conftest.py file to not be loaded at all.

But that's strange, I think changing the confcutdir should include the directory which is being cut to, so there might be a bug there.

@RonnyPfannschmidt
Copy link
Member

thus is a duplicate
#1149 was created due to this issue, its a upstream python bug thats fixed in 3.6

@nicoddemus
Copy link
Member

Thanks @RonnyPfannschmidt.

Option parsing aside, by setting --confcutdir=tests/file.ini, shouldn't we be able to load tests/conftest.py, or at least warn the user that tests/file.ini is not a directory?

@RonnyPfannschmidt
Copy link
Member

yes, that one should be a new issue/pr

@nicoddemus
Copy link
Member

Agreed, done: #2078

@ondratu unfortunately there's not that can be done at the moment until we fix the argument parsing library to handle partial options better, see #1149.

Closing this for now then, feel free to follow up with any questions.

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