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

Conflicting custom arguments does not raise obvious error #12824

Open
BenjaminCarpenter480 opened this issue Sep 16, 2024 · 2 comments
Open

Conflicting custom arguments does not raise obvious error #12824

BenjaminCarpenter480 opened this issue Sep 16, 2024 · 2 comments
Labels
topic: config related to config handling, argument parsing and config file type: bug problem that needs to be addressed

Comments

@BenjaminCarpenter480
Copy link

BenjaminCarpenter480 commented Sep 16, 2024

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

@BenjaminCarpenter480
Copy link
Author

After some more looking into this issue I see I have not read very closely and --keyword (which in my use case was being used as part of test setup) is in fact the full length match of '-k'. Bit of a silly mistake on my part!

Perhaps the fact that no error was raised in addoption is also an issue however?

@mikigo
Copy link

mikigo commented Sep 24, 2024

哥们儿,官方只说了可以用 -k ,没有说 -k 和 --keyword是一个东西,因此你自定义添加一个 --keyword 参数没有报错,这是正常现象
image

@Zac-HD Zac-HD added type: bug problem that needs to be addressed topic: config related to config handling, argument parsing and config file labels Oct 30, 2024
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: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants