Skip to content

Parameterized autouse fixtures don't work with unittest TestCase subclasses #3095

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
4 tasks done
paultiplady opened this issue Jan 8, 2018 · 9 comments
Closed
4 tasks done
Labels
plugin: unittest related to the unittest integration builtin plugin topic: fixtures anything involving fixtures directly or indirectly

Comments

@paultiplady
Copy link

paultiplady commented Jan 8, 2018

(Follow-up from discussion in #2535)

I'm trying to get parameterization to work with unittest TestCase tests. In the linked issue, it was suggested that since fixtures can be injected with autouse, it might be possible to inject parameterized fixtures with autouse as well.

Unfortunately this doesn't work:

import unittest
import pytest


class TestFixtureParameterization(unittest.TestCase):
    @pytest.fixture(autouse=True, params=['foo', 'bar'])
    def inject_data(self, request):
        self.test_data = request.param

    def test_injected_data(self):
        # Expect first parameterized run to succeed, and the second one to fail.
        assert self.test_data == 'foo'

Gives the error Failed: The requested fixture has no parameter defined for the current test.. This is the same error that's given if a normal fixture is injected into a unittest TestCase method.

Full test output:

=========================================================================================================================================================== ERRORS ===========================================================================================================================================================
______________________________________________________________________________________________________________________________ ERROR at setup of TestFixtureParameterization.test_injected_data ______________________________________________________________________________________________________________________________

self = <FixtureRequest for <TestCaseFunction 'test_injected_data'>>, fixturedef = <FixtureDef name='inject_data' scope='function' baseid='tests/pytest/test_parameterized.py::TestFixtureParameterization' >                                                                                                                  

    def _compute_fixture_value(self, fixturedef):                              
        """                                                                    
            Creates a SubRequest based on "self" and calls the execute method of the given fixturedef object. This will                                        
            force the FixtureDef object to throw away any previous results and compute a new fixture value, which                                              
            will be stored into the FixtureDef object itself.                  
                                                                               
            :param FixtureDef fixturedef:                                      
            """                                                                
        # prepare a subrequest object before calling fixture function          
        # (latter managed by fixturedef)                                       
        argname = fixturedef.argname                                           
        funcitem = self._pyfuncitem                                            
        scope = fixturedef.scope                                               
        try:                                                                   
>           param = funcitem.callspec.getparam(argname)                        
E           AttributeError: 'TestCaseFunction' object has no attribute 'callspec'                                                                              

../../.virtualenvs/pytest/lib/python3.5/site-packages/_pytest/fixtures.py:481: AttributeError                                                                  

During handling of the above exception, another exception occurred:            

msg = "The requested fixture has no parameter defined for the current test.\n\nRequested fixture 'inject_data' defined in:\n...meterized.py:7\n\nRequested here:\n/Users/paul/.virtualenvs/pytest/lib/python3.5/site-packages/_pytest/fixtures.py:384", pytrace = True                                                        

    def fail(msg="", pytrace=True):                                            
        """ explicitly fail an currently-executing test with the given Message.                                                                                
                                                                               
        :arg pytrace: if false the msg represents the full failure information 
                      and no python traceback will be reported.                
        """                                                                    
        __tracebackhide__ = True                                               
>       raise Failed(msg=msg, pytrace=pytrace)                                 
E       Failed: The requested fixture has no parameter defined for the current test.                                                                           
E                                                                              
E       Requested fixture 'inject_data' defined in:                            
E       tests/pytest/test_parameterized.py:7                                   
E                                                                              
E       Requested here:                                                        
E       /Users/paul/.virtualenvs/pytest/lib/python3.5/site-packages/_pytest/fixtures.py:384                                                                    

../../.virtualenvs/pytest/lib/python3.5/site-packages/_pytest/outcomes.py:92: Failed                                                                           

System details:

$ pip list
attrs (17.4.0)
pip (9.0.1)
pluggy (0.6.0)
py (1.5.2)
pytest (3.3.2)
setuptools (38.4.0)
six (1.11.0)
wheel (0.30.0)

$ pytest --version
This is pytest version 3.3.2

$ system_profiler SPSoftwareDataType

Software:

    System Software Overview:

      System Version: OS X 10.11.6 (15G1611)
      Kernel Version: Darwin 15.6.0
...
  • Include a detailed description of the bug or suggestion
  • pip list of the virtual environment you are using
  • pytest and operating system versions
  • Minimal example if possible
@pytestbot
Copy link
Contributor

GitMate.io thinks the contributor most likely able to help you is @nicoddemus.

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jan 8, 2018
@nicoddemus nicoddemus added topic: fixtures anything involving fixtures directly or indirectly plugin: unittest related to the unittest integration builtin plugin labels Jan 9, 2018
@nicoddemus
Copy link
Member

Thanks @paultiplady for creating the issue!

@RonnyPfannschmidt
Copy link
Member

thats per design - unittest test classes do not participate in parametrization, we should have better warnings

@RonnyPfannschmidt RonnyPfannschmidt added type: docs documentation improvement, missing or needing clarification and removed type: bug problem that needs to be addressed labels Jan 10, 2018
@nicoddemus
Copy link
Member

@RonnyPfannschmidt I asked @paultiplady to open this issue to separate it from the discussion about better messages in #2535 because I wanted to at least investigate the error.

But you are right, they are not supported by design and probably won't be easy to just make them work. Let's leave it open for now, as soon as I have some time I want to take a look.

@nicoddemus nicoddemus removed the type: docs documentation improvement, missing or needing clarification label Jan 10, 2018
@nicoddemus
Copy link
Member

(Btw, the issue is about parametrized-autouse fixtures, not parametrization of normal test functions)

@RonnyPfannschmidt
Copy link
Member

from the pytest side indirect parameterization via autouse fixtures and parameterization via direct means is the same,

@nicoddemus
Copy link
Member

But the parametrization in this case is for the fixture, not for the test function. Given that autouse fixtures work, it seems it might be possible to get parametrized-autouse fixtures to work (but I'm not very familiar with that part of the code base that's what I wanted to take a look at).

@RonnyPfannschmidt
Copy link
Member

all parameterization happens via generate_tests, which is not applied to legacy unittest functions

@nicoddemus
Copy link
Member

I see, thanks for the explanation.

@paultiplady, as @RonnyPfannschmidt explained the matter of this issue can't be done by design, so I'm closing this for now. The improvement to the error message is already being tracked in #2535.

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 topic: fixtures anything involving fixtures directly or indirectly
Projects
None yet
Development

No branches or pull requests

4 participants