pytest-dev / pytest-dev/pytest

Test case selection cannot work if parametrised id generation contains parentheses

Open
#6,626 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures topic: parametrize topic: selection type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Hi,

First of all, thanks for maintaining pytest.

When using fixture parametrisation with ids=repr, the resulting test case name may contain parentheses. Unfortunately, it doesn't look like I can select them using -k because that expects expressions.

Maybe there should be some restrictions on what the ids callable can output? If so, it'd be nice to have that documented or checked.

Minimal test case:

# test_pytest.py
import pytest


@pytest.fixture(params=[(1, 1), (2, 2)], ids=repr)
def a(request):
    return 1


@pytest.fixture(params=[1, 2])
def b(request):
    return 2


def test_1(a, b):
    assert True

Now, run:

pytest test_pytest.py -k "test_1[(2, 2)-2]"

You will observe:

...<snip>...
    return eval(keywordexpr, {}, mapping)
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'tuple' and 'int'

Environment:

Python 3.6.8 :: Anaconda, Inc.

pytest                        5.3.5               
pytest-asyncio                0.10.0              
pytest-bdd                    3.2.0               
pytest-cov                    2.8.1               
pytest-mock                   1.13.0

Workaround:

Something like this works

@pytest.fixture(ids=lambda t: ",".join(str(x) for x in t))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The reproduction is in test_pytest.py and uses pytest's -k selection with parametrized fixture IDs from repr; start by running the shown command and tracing the keyword-expression evaluation reported in the traceback. Determine whether the behavior should be constrained, documented, or handled differently, then add coverage showing the intended selection behavior without the TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.