0

I have a module A, which contains below two functions:

def my_func():
    my_dict = {"key1":100, "key2": 100}
    send_result(dicts=my_dict)


def send_result(dicts):
    print(dicts)

I have written unit test case as below:

from unittest.mock import MagicMock
import A
def test_send_results(self, dicts):
    self.assertGreater(len(dicts), 0)

def test_my_func(self):
    A.send_result = MagicMock(wraps=self.test_send_results)
    A.my_func()

And when I am running unit test case, I am getting below error though dicts contains the value:

TypeError: test_send_results() missing 1 required positional argument: 'dicts'
user15051990
  • 1,835
  • 2
  • 28
  • 42
  • name your replacement function something other than `test_*` -- the test runner is trying to run your function as a test -- also I'd suggest using `with mock.patch.object(...):` instead of assigning directly (so the monkeypatch is torn down after your test) – anthony sottile Sep 30 '19 at 01:24

1 Answers1

0

As suggested by Anthony, use patch.object. An example given below:

import unittest
from unittest import mock
import A

class MyTestClass(unittest.TestCase):

    def test_send_results(self, dicts):
        self.assertGreater(len(dicts), 0)

    @mock.patch.object(self, 'test_send_results')
    def test_my_func(self, mock_func):
        mock_func.return_value = 'something'
        A.my_func()