Skip to content

Running tests generated by pytest_generate_tests with the -k flag fails. #2479

Closed
@darshakkakkad

Description

@darshakkakkad

I am generating testmethod names using pytest_generate_tests and then executing them with the keyword substring match (-k flag).

Simplified Example:

test_data = [
    {
        "application" : "foo",
        "page" : "foo_page"
    },
    {
        "application" : "bar",
        "page" : "bar_page"
    }
]

def pytest_generate_tests(metafunc):
    metafunc.parametrize("test_data", test_data)

def test_bug(param):
    assert 1 == 1

2 tests with the following names are generated - test_bug[test_data0] and test_bug[test_data1]

Error:
When I run these with pytest -k "test_bug[test_data0] or test_bug[test_data1]", I get a TypeError: 'bool' object is not subscriptable.

Code:
https://github.com/pytest-dev/pytest/blob/master/_pytest/mark.py#L215-L220

My Reason why it fails:
As I have 2 tests and none of them start with the not keyword, the execution comes to L#220 (return eval(keywordexpr, {}, mapping))
eval() thinks the keywordexpr string provided contains a dictionary with test_bug as the dictionary name and test_data0 & test_data1 as the dictionary keys.
So, it matches test_bug with the mapping. This step returns a bool.
Now, it tries to access the key test_data0 on this bool (True[test_data0] | False[test_data0]) at which place it complains saying TypeError: 'bool' object is not subscriptable.

If I run pytest -k "test_bug[test_data0]", it works fine because L215-L216 gets executed.

If I run pytest -k "test_bug1 or test_bug2", it works fine.

If I run pytest -k "test_bug[test_data0] ", it fails.

Versions
OS version: OSX Sierra, Win7
Pytest version: pytest-2.6.4
Python Version: 3.4.4, 3.5.2

Does this appear to be an issue when using pytest_generate_tests and the -k flag together? Is this a known issue)? Any suggestions would be appreciated.

Thanks,
Darshak

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions