Skip to content

Conflicting custom arguments does not raise obvious error #12824

Open
@BenjaminCarpenter480

Description

@BenjaminCarpenter480

Tests found but deselected when running pytest when attempting to use addoption command line parsing for custom argument --keyword.

Test selection would note that tests were found but not selected. I believe this is a bug/undefined behaviour that occurs due to --keyword being shortened to -k (by convention and hence probably by pytest?). Created a basic working example to show the behaviour.

conftest.py

# content of conftest.py
import pytest


def pytest_addoption(parser):
    parser.addoption("--keyword", action="store", default="False", help="The buggy option")

@pytest.fixture
def getKeyword(request):
    return request.config.getoption("--keyword")

pytest_bug.py

# content of test_sample.py
def test_answer(getKeyword):
    assert True # Just pass 

Gives the following input and output

(test_env) ~/Desktop/pytestBug$> pip list
Package   Version
--------- -------
iniconfig 2.0.0
packaging 24.1
pip       24.2
pluggy    1.5.0
pytest    8.3.3
(test_env) ~/Desktop/pytestBug$> pytest pytest_bug.py 
============================= test session starts ==============================
platform linux -- Python 3.12.5, pytest-8.3.3, pluggy-1.5.0
rootdir: /home/bpc/Desktop/pytestBug
collected 1 item                                                               

pytest_bug.py .                                                          [100%]

============================== 1 passed in 0.01s ===============================
(test_env) ~/Desktop/pytestBug$> pytest pytest_bug.py --keyword=blah
============================= test session starts ==============================
platform linux -- Python 3.12.5, pytest-8.3.3, pluggy-1.5.0
rootdir: /home/bpc/Desktop/pytestBug
collected 1 item / 1 deselected / 0 selected                                   

============================ 1 deselected in 0.00s =============================

Given the confusing output it seems to me that this is buggy behaviour that should be warned about.

I think this requires an extension of code at pytest/src/_pytest/config/argparsing.py#L379

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: configrelated to config handling, argument parsing and config filetype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions