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

Parametrized nodeid not matching with parametrized IDs #4142

Open
vbarbaresi opened this issue Oct 14, 2018 · 10 comments
Open

Parametrized nodeid not matching with parametrized IDs #4142

vbarbaresi opened this issue Oct 14, 2018 · 10 comments
Labels
topic: parametrize related to @pytest.mark.parametrize topic: selection related to test selection from the command line type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@vbarbaresi
Copy link
Member

This is about this issue in pytest-repeat: pytest-dev/pytest-repeat#21

It seems that the bug doesn't come from the plugin: I managed to reproduce with a simple test here.
It affects all pytest versions, at least since 2.8

I wrote a test and a fix attempt here: vbarbaresi@12769f0

The issue is the following: with parametrized IDs and parametrized fixtures, one can't run a test using its node ID with a [parameter] argument

I tracked it down to the x.name vs name matching, which is in my example:
test_foo[1-param] vs test_foo[param]

My fix attempt drops everything after [ on both sides. However, we lose the supplied parameter value so it's not satisfying. I don't know where to fix this properly.

@Zac-HD Zac-HD added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature topic: selection related to test selection from the command line labels Oct 19, 2018
@blueyed
Copy link
Contributor

blueyed commented Nov 9, 2018

@vbarbaresi
Can you please turn this into a PR (assuming the test is passing)?

@vbarbaresi
Copy link
Member Author

The test is passing but doesn't check that [param] is honored.
With my workaround it will just match the test name, ignoring everything inside []. That's not satisfying

Now that I look at it again, the issue seems fundamentally hard to fix.
Plus, there are no tests on running a parametrized nodeid with [parameter] from the command line so it's hard to know what should work.
Consider my branch as a failed attempt.


To sum up:

import pytest

@pytest.fixture(autouse=True)
def __bar(request):
    return request.param

def pytest_generate_tests(metafunc):
    metafunc.parametrize('__bar', [1], indirect=True)

class TestClass(object):
    @pytest.fixture(params=['param'])
    def foo(self, request):
        pass

    def test_foo(self, foo):
        pass

metafunc.parametrize doesn't play well with nodeid + parameter
The nodeid becomes TestClass::test_foo[1-param]


Note that there is an old TODO in _pytest/main.py saying

# TODO: remove parametrized workaround once collection structure contains parametrization
if x.name == name or x.name.split("[")[0] == name:

I feel like it has to do with this issue

@blueyed
Copy link
Contributor

blueyed commented Nov 13, 2018

Thanks for the update!

Now that I look at it again, the issue seems fundamentally hard to fix.
Plus, there are no tests on running a parametrized nodeid with [parameter] from the command line so it's hard to know what should work.

I feel your pain, having worked at collection recently.
(just as a side-note, the extra "::()" here and there annoyed me quite some time, until I dug into it to get it removed via #4358 - turned out to be not that hard then after all)

Given there are no tests I'd argue that you are free to clean this up to match your expectations.

Surely @RonnyPfannschmidt has a rant in this regard also.. :)

I'd say that a WIP-PR is still something good to have, where you could add your summarized example as a failing test to start from. Use [skip appveyor] in the commit title then to only get the baseline test reports from Travis - this can be very useful to see if and how much breaks from the current tests.

@blueyed blueyed added the topic: parametrize related to @pytest.mark.parametrize label Nov 13, 2018
@RonnyPfannschmidt
Copy link
Member

@blueyed i actually have a fix on the horizon, but it will at least need 3-4 major releases to sort nodes out - given that outline no more ranting needed

@DavidAntliff
Copy link

@RonnyPfannschmidt I'd be interested in the fix, if it's available?

@RonnyPfannschmidt
Copy link
Member

The node refactoring needed is still in very early stages

@bschnurr
Copy link

bschnurr commented Jun 5, 2020

Not sure if it helps but here is the work around I used to change the generated pytest id's when the parameter contained characters like "::" and "\n"

I need to change the Id so that both Visual Studio's Test Explorer could parse it correctly and pytest was having issues running a single test when trying to call it using the ID

microsoft/PTVS@caa5383

@1Mark
Copy link

1Mark commented Feb 11, 2021

Any progress on this?

@nicoddemus
Copy link
Member

The node refactoring has been progressing, but I don't think there's news about this issue in particular (@RonnyPfannschmidt might give more details here).

@RonnyPfannschmidt
Copy link
Member

nothing new there, after the rest of the path refactoring lands i'm going to work on setting up the function-definition node, afterwards we can do it more easily

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: parametrize related to @pytest.mark.parametrize topic: selection related to test selection from the command line type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

8 participants