Skip to content

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

Closed
@paultiplady

Description

@paultiplady

(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

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: unittestrelated to the unittest integration builtin plugintopic: fixturesanything involving fixtures directly or indirectly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions