Skip to content

AttributeError: 'SubRequest' object has no attribute 'param' #2162

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

Closed
renewang opened this issue Dec 27, 2016 · 2 comments
Closed

AttributeError: 'SubRequest' object has no attribute 'param' #2162

renewang opened this issue Dec 27, 2016 · 2 comments
Labels
plugin: unittest related to the unittest integration builtin plugin type: question general question, might be closed after 2 weeks of inactivity

Comments

@renewang
Copy link

renewang commented Dec 27, 2016

I have a code using cascaded fixtures that is the first fixture will create raw data with different parameters and the second fixtures will use the first fixture and create parameterized test cases based on the exhaustive enumeration of parameters specified in first and second.

A oversimplified version of aforementioned problem is listed below:

@pytest.fixture(scope='session')
def treesampler():
      trees = get_trees()
      return(trees)

@pytest.fixture(scope="module", params=[True, False], ids=["ordered", "unordered"])
def expect_raw(request, treesampler):
       trees = treesampler
       #AttributeError: 'SubRequest' object has no attribute 'param'
       #when executing TestDemo::test_expect_stream
       is_sorted = request.param  
       if is_sorted:  
          print('is_sorted')
       else: 
         print('not sorted')

@pytest.fixture(scope="function", params=list(range(1, 11)))
def expect_batch(request, expect_raw):
      is_sorted = expect_raw
     bsize = request.param
     print(is_sorted, bsize)
     return(bsize, is_sorted)

# testing unbound function
def test_expect_stream(expect_batch):
       bsize, is_sorted = expect_batch
       assert True

# testing bound function
@pytest.mark.usefixtures(expect_batch)
class TestDemo(unittest.TestCase):
     def test_expect_stream(self):
           bsize, is_sorted = expect_batch
           assert True

The cascaded fixtures work well when the test case is unbound (not bound to any TestCase instance) but not when being an instance method of a TestCase. The TestCase will complain SubRequest has no param attribute for the first fixture. I wonder what’s the logics behind SubRequest and what causes the differentiate behaviors in bound / unbound test function.

Would you be so kind as to sharing insights or tips about the pytest code design and maybe indicate which part of code I should look into in order to shed some light of this problem? Or maybe a better design to solve this problem.
Many thanks in advance.

pytest version 2.9.1
OS: Darwin Kernel Version 15.6.0: x86_64
sandbox_pip_list_output.txt

@nicoddemus
Copy link
Member

Hi @renewang sorry for the lack of answers.

request.param has been changed some time ago to at least provide a good error message when it is used in an invalid situation. Can you try your example with the latest version and see if that improves your understanding? Thanks.

@nicoddemus nicoddemus added status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity type: question general question, might be closed after 2 weeks of inactivity labels Sep 29, 2017
@RonnyPfannschmidt RonnyPfannschmidt added plugin: unittest related to the unittest integration builtin plugin and removed status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity labels Sep 30, 2017
@RonnyPfannschmidt
Copy link
Member

@renewang the unittest support does not support parameterization, the error message on that should be more clear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: unittest related to the unittest integration builtin plugin type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants