[파이썬] unittest patch 메서드 활용

Unit testing is an essential part of software development. It involves testing individual units of code to ensure they function correctly. In Python, the unittest module provides a robust framework for conducting unit tests.

One convenient feature of unittest is the patch method. It allows you to replace or mock objects or functions during testing, so you can test different scenarios or isolate specific parts of your code. This blog post will demonstrate the various ways you can use the patch method in your Python unit tests.

Patching Objects

Let’s start by patching objects in Python unit tests. Assume you have a simple class Calculator that performs mathematical operations. And you want to test a method that depends on a specific attribute of the calculator object.

class Calculator:
    def __init__(self):
        self.value = 0

    def add(self, num):
        self.value += num

To test the add method, you can use the patch method to replace the value attribute with a specific value:

import unittest
from unittest.mock import patch

class TestCalculator(unittest.TestCase):
    @patch('module.Calculator.value', 100)
    def test_add_method(self):
        calculator = Calculator()
        calculator.add(10)
        self.assertEqual(calculator.value, 110)

In this example, we import the patch method from the unittest.mock module. Then, we use the @patch decorator to replace the value attribute of the Calculator class with 100. Inside the test method, we create an instance of the Calculator class, call the add method, and assert that the value attribute is equal to 110. By patching the object, we can focus on testing just the method without worrying about the actual attribute.

Patching Functions

Patching functions is also straightforward using the patch method. Suppose you have a function get_weather that fetches the current weather from an external API.

import requests

def get_weather():
    response = requests.get('https://api.weather.com')
    return response.json()['temperature']

To test this function without making actual API calls, you can use the patch method to replace requests.get with a mock function:

import unittest
from unittest.mock import patch

class TestWeather(unittest.TestCase):
    @patch('module.requests.get')
    def test_get_weather(self, mock_get):
        mock_get.return_value.json.return_value = {'temperature': 25}
        temperature = get_weather()
        self.assertEqual(temperature, 25)

In this example, we patch the requests.get function using @patch. Then, in the test method, we set the return value of json to a mock JSON response containing the temperature. Finally, we call the get_weather function and assert that the returned temperature is 25. With function patching, we can test the logic of our code without relying on external services.

Conclusion

The patch method in Python’s unittest module is a powerful tool for replacing or mocking objects and functions during unit testing. It allows you to isolate specific parts of your code and test them in isolation, leading to more robust and reliable tests.

In this blog post, we explored examples of patching objects and patching functions. However, the patch method can be used in various other scenarios, such as patching decorators, patching class methods, or even patching multiple objects simultaneously.

By utilizing the patch method effectively, you can enhance your unit tests and ensure the stability and correctness of your code. Happy testing!