0

I have the following files -

summer.py

def sum(a, b):
    return a+b

base.py

class Base():
    def work(self, a, b):
        self.add(a, b)

calculator.py

from summer import sum
from base import Base
class Calculator(Base):
    def work(self, a, b):
        super().work(a, b)

    def add(self, a, b):
        return sum(a, b)

test_calculator.py

from unittest import mock
from calculator import Calculator
@mock.patch('calculator.sum')
def test_calc(mock_sum):
    a = 5
    b = 10
    mock_sum.return_value = 0
    Calculator().work(a, b)
    mock_sum.assert_called_once()

However, when I run test_calculator.py I get an AssertionError as follows -

AssertionError: Expected 'mock_sum' to have been called once. Called 0 times.

What is the correct way to mock sum()?

m0bi5
  • 8,900
  • 7
  • 33
  • 44
  • What's your Python version. I try in Python 3.6.8, It's OK. You code isn't complete, I just add `import` Clause. – hstk Aug 04 '22 at 01:25
  • 1
    @hstk my bad, I missed out one part of the code that seems to be causing the issue - the inheritance – m0bi5 Aug 04 '22 at 01:43
  • func `test_calc` not inherit the `unittest.TestCase` ? – hstk Aug 04 '22 at 03:12

0 Answers0