Skip to content

unable to add cmd line option dynamically #4384

@revesansparole

Description

@revesansparole

I want to run pytest with coverage option only when the command is not launched inside pycharm. Previously I just added a hook in conftest.py and it was working well:

def pytest_cmdline_preparse(args):
    if 'PYCHARM_HOSTED' not in os.environ:
        args.append("--cov=waloupkg")

However the latest pytest calls this hook only after pytest-cov hooks have been already called and I'm unable to launch the cov pluggin as demonstrated below. In case 1) I do not get a coverage report while in case 2) I get one (not displayed here).

  1. This is what happen when I do not specify '--cov' on the command line expecting my hook to do it for me:
(toto) C:\Users\jchopard\Documents\eval\pkging\waloupkg
>pytest -s
cov: pytest_load_initial_conftests ['--maxfail=2', '-rf', '-s']
mine: pytest_cmdline_preparse
=============================================== test session starts ================================================
platform win32 -- Python 3.7.1, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: C:\Users\jchopard\Documents\eval\pkging\waloupkg, inifile: setup.cfg
plugins: cov-2.6.0
collected 1 item
  1. This is what happen when I explicitly specify '--cov' on the command line
(toto) C:\Users\jchopard\Documents\eval\pkging\waloupkg
>pytest -s --cov
cov: pytest_load_initial_conftests ['--maxfail=2', '-rf', '-s', '--cov']
cov: __init__
cov: start
mine: pytest_cmdline_preparse
=============================================== test session starts ================================================
platform win32 -- Python 3.7.1, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: C:\Users\jchopard\Documents\eval\pkging\waloupkg, inifile: setup.cfg
plugins: cov-2.6.0
collected 1 item

PS: I tried to use pytest_load_initial_conftestsinstead of pytest_cmdline_preparse but the hook was never called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: configrelated to config handling, argument parsing and config file

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions