简体   繁体   中英

pytest-mock assert_called_with failed for class function

I am planning to use pytest and pytest-mock for validating the Python code. Being a newbie, wrote a sample code to validate the mock on class and seeing failure. I am wondering what went wrong.

src/main.py

class Main(object):
    def __init__(self, my_var=None):
        self.var = my_var

    def internal_func(self, var=10):
        my_var = var + 20
        return my_var

    def test_func(self):
        val = self.internal_func(20)
        return val + 40

tests/test_main.py

    import pytest
    from pytest_mock import mocker
    from src.main import Main

    def new_func(cls, *args, **kwargs):
        return 2

    def test_main_mock(mocker):
        mocker.patch.object(Main, 'internal_func')
        val = Main().test_func()
        assert Main.internal_func.assert_called_with(20)

It fails with the following error

    ======================================================================================== FAILURES ========================================================================================
    _____________________________________________________________________________________ test_main_mock _____________________________________________________________________________________

    mocker = <pytest_mock.MockFixture object at 0x7f34f490d8d0>

        def test_main_mock(mocker):
            mocker.patch.object(Main, 'internal_func')
            main = Main()
            val = main.test_func()
        #    assert val == 80
    >       assert Main.internal_func.assert_called_with(20)
    E       AssertionError: assert None
    E        +  where None = <bound method MagicMock.wrap_assert_called_with of <MagicMock name='internal_func' id='139865418160784'>>(20)
    E        +    where <bound method MagicMock.wrap_assert_called_with of <MagicMock name='internal_func' id='139865418160784'>> = <MagicMock name='internal_func' id='139865418160784'>.assert_called_with
    E        +      where <MagicMock name='internal_func' id='139865418160784'> = Main.internal_func

    tests/test_main.py:13: AssertionError

The return_value or side_effect must be set before the patched func take effect

def test_main_mock(mocker):
    # mock internal_func of class Main
    mocked_internal_func = mocker.patch.object(Main, 'internal_func')
    # assign return_value or side_effect
    mocked_internal_func.return_value = -10
    # class instance
    ma = Main()
    val = ma.test_func()

    assert ma.internal_func.assert_called_with(20)

Correction of mistake, the assert should not be used together with assert_called_with , they are independent assert.

    assert val == 30
    mocked_internal_func.assert_called
    ma.internal_func.assert_called_with(20)
    mocked_internal_func.assert_called_with(20)

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