missleading error on getfuncargvalue for parameterized fixture #460

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

Comments

Projects
None yet
3 participants
@pytestbot

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

This comment has been minimized.

Show comment
Hide comment
@pytestbot

pytestbot Jun 23, 2014

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.

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

This comment has been minimized.

Show comment
Hide comment
@pytestbot

pytestbot Jun 23, 2014

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?

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

This comment has been minimized.

Show comment
Hide comment
@pytestbot

pytestbot Apr 2, 2015

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.

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

This comment has been minimized.

Show comment
Hide comment
@pytestbot

pytestbot May 19, 2015

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.

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.

@tomviner

This comment has been minimized.

Show comment
Hide comment
@tomviner

tomviner May 19, 2016

Contributor

@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'

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

This comment has been minimized.

Show comment
Hide comment
@RonnyPfannschmidt

RonnyPfannschmidt May 19, 2016

Member

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

Member

RonnyPfannschmidt commented May 19, 2016

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

@RonnyPfannschmidt RonnyPfannschmidt changed the title from AttributeError: 'SubRequest' object has no attribute 'param' to missleading error on getfuncargvalue for parameterized fixture May 19, 2016

@RonnyPfannschmidt

This comment has been minimized.

Show comment
Hide comment
@RonnyPfannschmidt

RonnyPfannschmidt May 19, 2016

Member

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

Member

RonnyPfannschmidt commented May 19, 2016

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

@tomviner

This comment has been minimized.

Show comment
Hide comment
@tomviner

tomviner May 24, 2016

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.

Contributor

tomviner commented May 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@RonnyPfannschmidt

RonnyPfannschmidt May 24, 2016

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

Member

RonnyPfannschmidt commented May 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@tomviner

tomviner May 24, 2016

Contributor

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

Contributor

tomviner commented May 24, 2016

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

@RonnyPfannschmidt

This comment has been minimized.

Show comment
Hide comment
Member

RonnyPfannschmidt commented May 24, 2016

correct

@tomviner

This comment has been minimized.

Show comment
Hide comment
@tomviner

tomviner Jun 25, 2016

Contributor

Fixed by #1620 Please close.

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

Contributor

tomviner commented Jun 25, 2016

Fixed by #1620 Please close.

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

@RonnyPfannschmidt

This comment has been minimized.

Show comment
Hide comment
Member

RonnyPfannschmidt commented Jun 25, 2016

thanks

@dependencies dependencies bot referenced this issue in tjamet/caduc Nov 4, 2017

Open

pytest versions available: 3.0.0 #3

This was referenced Nov 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment