[파이썬] unittest mock 객체 호출 확인하기

Testing is an essential part of software development. In Python, the unittest module provides a built-in framework for writing tests. When writing tests, we often need to simulate certain behaviors or interactions with external dependencies. This is where the mock library comes in handy.

The mock library allows us to create mock objects that mimic the behavior of real objects. We can then use these mock objects to verify that specific methods or functions are called with the correct arguments. In this blog post, we will explore how to use mock to verify object method calls in Python.

Installation

Before we begin, make sure you have the mock library installed. You can install it using pip:

pip install mock

Creating a Mock object

To create a mock object, we can use the mock.Mock() function. Let’s consider a simple example where we have a Calculator class with a multiply() method:

class Calculator:
    def multiply(self, a, b):
        return a * b

To create a mock object that mimics the behavior of this class, we can do the following:

from unittest import mock

mock_calculator = mock.Mock(spec=Calculator)

Here, spec=Calculator ensures that the mock object has the same attributes and methods as the Calculator class.

Verifying Object Method Calls

Now that we have our mock object, we can use it to verify that specific methods are called with the correct arguments. In our example, let’s say we want to ensure that the multiply() method is called with arguments (2, 3).

# Execute code that calls the multiply method
result = mock_calculator.multiply(2, 3)

# Verify that the multiply method was called with arguments (2, 3)
mock_calculator.multiply.assert_called_with(2, 3)

Here, assert_called_with() is a method provided by the mock library that verifies that the method was called with the specified arguments.

Counting Method Calls

In addition to verifying method calls with specific arguments, we can also count how many times a method is called. Let’s modify our example to count the number of times the multiply() method is called:

# Execute code that calls the multiply method multiple times
result1 = mock_calculator.multiply(2, 3)
result2 = mock_calculator.multiply(4, 5)

# Verify that the multiply method was called twice
assert mock_calculator.multiply.call_count == 2

Here, call_count is an attribute provided by the mock library that gives us the count of method calls.

Conclusion

The mock library in Python provides a powerful mechanism for testing and verifying object method calls. By creating mock objects, we can simulate behaviors and interactions with external dependencies. We can then use the various methods and attributes provided by the mock library to verify that specific methods are called with the correct arguments and count the number of method calls.

By incorporating mock into our unit tests, we can ensure that our code is working correctly and handles interactions with external dependencies as expected.

I hope this blog post has given you a good introduction to verifying object method calls using unittest and mock in Python. Happy testing!