Skip to content

addopts behavior with coverage flag #2165

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

Open
4 tasks done
miketheman opened this issue Dec 28, 2016 · 1 comment
Open
4 tasks done

addopts behavior with coverage flag #2165

miketheman opened this issue Dec 28, 2016 · 1 comment
Labels
topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch

Comments

@miketheman
Copy link

(uncertain if this is specific to py.test config, or pytest-cov, apologies if I'm putting it in the wrong place)

  • Include a detailed description of the bug or suggestion

It appears that the addopts section of config file appends positional arguments to option flags following, making specifying a path of tests to include dependent on the order of the option flags.

Note: I use the --collect-only flag as an example, in my project I've got others like --duration=10, etc - I tried to use a simple reproduction approach.

  • pip list of the virtual environment you are using
coverage (4.3.1)
pip (9.0.1)
py (1.4.32)
pytest (3.0.5)
pytest-cov (2.4.0)
setuptools (32.3.0)
wheel (0.30.0a0)
  • pytest and operating system versions
$ python --version
Python 2.7.13
$ py.test --version
This is pytest version 3.0.5, imported from ..../lib/python2.7/site-packages/pytest.pyc
setuptools registered plugins:
  pytest-cov-2.4.0 at ..../lib/python2.7/site-packages/pytest_cov/plugin.py
  • Minimal example if possible

Given a setup.cfg section like:

[tool:pytest]
addopts = --collect-only --cov

And a directory tree structure like:

.
├── app
│   ├── one
│   │   └── test_sample1.py
│   └── two
│       └── test_sample2.py
└── setup.cfg

3 directories, 3 files

And test_sample?.py contains this example:

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

When running py.test app/one/
Then both test cases are collected, instead of only one.

Output:

============================= test session starts ==============================
platform darwin -- Python 2.7.13, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /Users/miketheman/tmp/pytest-repro, inifile: setup.cfg
plugins: cov-2.4.0
collected 2 items
<Module 'app/one/test_sample1.py'>
  <Function 'test_answer'>
<Module 'app/two/test_sample2.py'>
  <Function 'test_answer'>


---------- coverage: platform darwin, python 2.7.13-final-0 ----------
Name                      Stmts   Miss  Cover
---------------------------------------------
app/one/test_sample1.py       4      2    50%

========================= no tests ran in 0.02 seconds =========================

It appears that the path specified on the commandline is being passed to --cov

Workaround

Changing the setup.cfg to reverse the options like so:

[tool:pytest]
addopts = --cov --collect-only

Produces the correct result:

============================= test session starts ==============================
platform darwin -- Python 2.7.13, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /Users/miketheman/tmp/pytest-repro, inifile: setup.cfg
plugins: cov-2.4.0
collected 1 items
<Module 'app/one/test_sample1.py'>
  <Function 'test_answer'>


---------- coverage: platform darwin, python 2.7.13-final-0 ----------
Name                      Stmts   Miss  Cover
---------------------------------------------
app/one/test_sample1.py       4      2    50%

========================= no tests ran in 0.01 seconds =========================
@nicoddemus
Copy link
Member

Hi @miketheman thanks for writing.

You assessment is correct, addopts just appends the arguments to the command line. So you get in the end addopts + command line, so your first example is running:

py.test --collect-only --cov app/one/

I agree it is confusing. Perhaps it would be possible to interpret the options in two separate passes?

@nicoddemus nicoddemus added topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch labels Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

2 participants