简体   繁体   中英

How to use patch decorator while using mark.parametrize decorator?

I have a test function that patches a few things. Once I reached more than 2 or 3 patches, the function started looking very bad

def test_bla():
    with patch(...):
        with patch(...):
            with patch(...):
                # test

so I tried to start using decorators. Here is my example test which breaks when I introduce the patch decorator

import pytest
from unittest.mock import patch

class Example:
    def _run(self, x):
        return x

    def run(self, x):
        return self._run(x) + 1


@pytest.mark.parametrize('x', [1])
def test_example(x):
    assert Example().run(x) == 2


@pytest.mark.parametrize('x', [1])
@patch('Example._run', return_value=0)
def test_example_failure(x):
    with pytest.raises(AssertionError):
        assert Example().run(x) == 2

This is the error I get:

platform linux -- Python 3.6.13, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: ..., configfile: pytest.ini
plugins: metadata-1.11.0
collected 0 items / 1 error                                                                                                                                                                                    

==================================================================================================== ERRORS ====================================================================================================
________________________________________________________________________________________ ERROR collecting test_patch.py ________________________________________________________________________________________
In test_example_failure: function uses no argument 'x'
=========================================================================================== short test summary info ============================================================================================
ERROR test_patch.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=============================================================================================== 1 error in 0.12s ===============================================================================================

I tried changing the decorators order (as I saw in some SO answers) but that didn't help.


Another thing I just realized is that having a test parameter without a parameterization works! (I adjusted a bit the code because I couldn't patch a class from my own module as it turns out)

import pytest                                                                                                                                                                                                   
import random                                                                                                                                                                                                   
from unittest.mock import patch                                                                                                                                                                                 
                                                                                                                                                                                                                
class Example:                                                                                                                                                                                                  
    def _run(self, x):                                                                                                                                                                                          
        return random.randint(0, 6)                                                                                                                                                                             
                                                                                                                                                                                                                
    def run(self, x):                                                                                                                                                                                           
        return self._run(x) + 1                                                                                                                                                                                 
                                                                                                                                                                                                                
                                                                                                                                                                                                                
@pytest.mark.parametrize('x', [1])                                                                                                                                                                              
def test_example(x):                                                                                                                                                                                            
    assert Example().run(x) < 7                                                                                                                                                                                 
                                                                                                                                                                                                                
                                                                                                                                                                                                                
# @pytest.mark.parametrize('x', [1])                                                                                                                                                                            
@patch('random.randint', return_value=0)                                                                                                                                                                        
def test_example_failure(x):                # <<< How the heck does this work? 'x' is not defined!!                                                                                                                                                                   
    with pytest.raises(AssertionError):                                                                                                                                                                         
        assert Example().run(x) == 2    

@patch is sending a parameter to the test, MagicMock object. If you want to send another parameter from @pytest.mark.parametrize you need to add another parameter to the test

@pytest.mark.parametrize('x', [1])
@patch('Example._run', return_value=0)
def test_example_failure(mocked_class, x):
    with pytest.raises(AssertionError):
        assert Example().run(x) == 2

# randint = <MagicMock name='randint' id='2158546057008'>
# x = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM