Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 679
suri Avatar asked May 04 '18 00:05

suri


People also ask

What is Assert_called_with in Python?

According to the python documentation https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_called_with. 'This method is a convenient way of asserting that calls are made in a particular way' so it tests whether the parameters are being used in the correct way.

What is the difference between mock and MagicMock?

With Mock you can mock magic methods but you have to define them. MagicMock has "default implementations of most of the magic methods.". If you don't need to test any magic methods, Mock is adequate and doesn't bring a lot of extraneous things into your tests.

What is Side_effect in mock python?

side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT , the return value of this function is used as the return value.

How do you mock a function in Python?

How do we mock in Python? Mocking in Python is done by using patch to hijack an API function or object creation call. When patch intercepts a call, it returns a MagicMock object by default. By setting properties on the MagicMock object, you can mock the API call to return any value you want or raise an Exception .


1 Answers

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)
like image 189
Gang Avatar answered Nov 08 '22 10:11

Gang