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

missleading error on getfuncargvalue for parameterized fixture #460

Closed
pytestbot opened this issue Feb 12, 2014 · 13 comments
Closed

missleading error on getfuncargvalue for parameterized fixture #460

pytestbot opened this issue Feb 12, 2014 · 13 comments
Labels
status: critical grave problem or usability issue that affects lots of users topic: collection related to the collection phase type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Nikolaus Rath (BitBucket: nikratio, GitHub: nikratio)


The attached meta-test-case results in

#!

    @pytest.fixture(params=(0,1,2))
    def param1(request):
>       return request.param
E       AttributeError: 'SubRequest' object has no attribute 'param'

with pytest from Mercurial tip.


@pytestbot
Copy link
Contributor Author

Original comment by Matthias Geier (BitBucket: geier, GitHub: geier):


I get the same error when trying to create a fixture that concatenates two or more other fixtures (see my question on stackoverflow).

It's possible to have a fixture using a fixture using a fixture and so on, but it doesn't seem to be possible if parametrized fixtures are involved.

@pytestbot
Copy link
Contributor Author

Original comment by Matthias Geier (BitBucket: geier, GitHub: geier):


Here's my code example (without classes) that shows the same error:

#!python

import pytest

@pytest.fixture(params=[1, 2, 3])
def lower(request):
    return "i" * request.param

@pytest.fixture(params=[1, 2])
def upper(request):
    return "I" * request.param

@pytest.fixture(params=['lower', 'upper'])
def all(request):
    return request.getfuncargvalue(request.param)

def test_all(all):
    assert 0, all

Is there a way to make this work?

@pytestbot
Copy link
Contributor Author

Original comment by Fayaz Khan (BitBucket: fayaz, GitHub: fayaz):


In my case, the fixture seems to be failing because it shares name with a parameter on the test.

#!python

import pytest


@pytest.fixture(params=(0, 1, 2))
def param1(request):
    return request.param


@pytest.mark.parametrize('param1b', (1, 2))
def test_method(param1, param1b):
    assert param1 >= 0

Try changing the name param1b to param2 or something else, and it would work.

@pytestbot
Copy link
Contributor Author

Original comment by Markus Unterwaditzer (BitBucket: untitaker, GitHub: untitaker):


@Fayaz I have just discovered this issue as well and filed #745 because it doesn't seem to have anything to do with nested fixture requests.

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 15, 2015
@RonnyPfannschmidt RonnyPfannschmidt added topic: collection related to the collection phase type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog labels Apr 20, 2016
@tomviner
Copy link
Contributor

tomviner commented May 19, 2016

@RonnyPfannschmidt any reason this is listed as backward compatibility? Is request.getfuncargvalue deprecated? I'm hitting this issue with an on/off settings flipper fixture being used by a fixture that uses request.getfuncargvalue.

A minimal test case, equivalent of the one above:

import pytest

@pytest.fixture(params=[0])
def fix_with_param(request):
    print request.param

@pytest.fixture
def get_named_fixture(request):
    return request.getfuncargvalue('fix_with_param')

def test_thing(get_named_fixture):
    pass

Result is same as above: AttributeError: 'SubRequest' object has no attribute 'param'

@RonnyPfannschmidt
Copy link
Member

that kind of usage should raise an error
'getfuncargvalue` cannnot ever work for parameter requests

@RonnyPfannschmidt RonnyPfannschmidt changed the title AttributeError: 'SubRequest' object has no attribute 'param' missleading error on getfuncargvalue for parameterized fixture May 19, 2016
@RonnyPfannschmidt RonnyPfannschmidt added the status: critical grave problem or usability issue that affects lots of users label May 19, 2016
@RonnyPfannschmidt
Copy link
Member

this is a rather bad case,
since its also possible to hit this one indirectly

@tomviner
Copy link
Contributor

Yeah I can see that using getfuncargvalue in a fixture can't start multiplying the cases, like params do. In fact it seems as difficult as supporting unittest subtests (#1367).

I think a better error than AttributeError: 'SubRequest' object has no attribute 'param' would be good.

@RonnyPfannschmidt
Copy link
Member

its a completely different dimension than subtests ^^

subtests happen by design within a test

this case is requesting something that should have been parametrized before the test within the test and without parameters

@tomviner
Copy link
Contributor

Fair enough. But we're agreed on needing a better error message?

@RonnyPfannschmidt
Copy link
Member

correct

@tomviner
Copy link
Contributor

Fixed by #1620 Please close.

(I'll do the magic commit message that closes issues next time)

@RonnyPfannschmidt
Copy link
Member

thanks

fkohlgrueber pushed a commit to fkohlgrueber/pytest that referenced this issue Oct 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: critical grave problem or usability issue that affects lots of users topic: collection related to the collection phase type: backward compatibility might present some backward compatibility issues which should be carefully noted in the changelog type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants