简体   繁体   中英

How to mock the return value of a class's function

I've seen some articles here related to mocking functions within classes, but none seem to help this scenario that I'm hitting. For a simple example, I have 2 files for my source code and 1 for testing.

myclass.py

class ProductionClass:

    def my_function(self, num):
        return num + 1

main.py

from myclass import ProductionClass


def main_function():
    me = ProductionClass()
    response = me.my_function(3)
    return response


if __name__ == '__main__':
    main_function()

I am trying to control the value returned by ProductionClass.my_function . As such as I create the following test:

test_main.py

from main import main_function
from mock import patch


@patch("main.ProductionClass")
def test_main_function(mock_ProductionClass):
    expected_value = 5
    mock_ProductionClass.my_function.return_value = expected_value
    response = main_function()
    assert response == expected_value

This gives me the following error when I run pytest . :

collected 1 item                                                                                                                                  

test_main.py F                                                                                                                              [100%]

==================================================================== FAILURES ====================================================================
_______________________________________________________________ test_main_function _______________________________________________________________

mock_ProductionClass = <MagicMock name='ProductionClass' id='62692896'>

    @patch("main.ProductionClass")
    def test_main_function(mock_ProductionClass):
        expected_value = 5
        mock_ProductionClass.my_function.return_value = expected_value
        response = main_function()
>       assert response == expected_value
E       AssertionError: assert <MagicMock name='ProductionClass().my_function()' id='62958320'> == 5

test_main.py:10: AssertionError
============================================================ short test summary info =============================================================
FAILED test_main.py::test_main_function - AssertionError: assert <MagicMock name='ProductionClass().my_function()' id='62958320'> == 5
=============================================================== 1 failed in 0.34s ================================================================

What am I doing wrong here?

I am able to resolve this issue by making a small change in your code as follows:

from main import main_function
from mock import patch


def test_main_function():
    expected_value = 5
    with patch(
            "main.ProductionClass.my_function", return_value=expected_value
    ):
        response = main_function()

    assert response == expected_value

with patch syntax is much more easy to mock and have side effects defined.

Please let me know if it helps

Felix K Jose's answer works and I was also able to resolve it like this by adding () after my mock_ProductionClass :

from main import main_function
from mock import patch


@patch("main.ProductionClass")
def test_main_function(mock_ProductionClass):
    expected_value = 5
    mock_ProductionClass().my_function.return_value = expected_value
    response = main_function()
    assert response == expected_value

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