[파이썬] unittest 테스트 케이스 실행 시간 측정

In software development, unit testing is an essential practice to ensure the correctness and reliability of code. Python provides a built-in module called unittest that allows us to write and run test cases.

While writing test cases, it is crucial to consider the execution time of each test case. By measuring the execution time, we can identify potential bottlenecks and optimize our code accordingly. In this blog post, we will explore how to measure the execution time of unittest test cases in Python.

Using the time module

The simplest way to measure the execution time of a test case is by using the time module in Python. The time module provides a time() function that returns the current time in seconds.

Here’s an example of how we can measure the execution time of a test case using the time module:

import unittest
import time

class MyTestCase(unittest.TestCase):
    def test_something(self):
        start_time = time.time()
        # Perform some operations or function calls
        end_time = time.time()
        
        execution_time = end_time - start_time
        print(f"Execution time: {execution_time} seconds")

        # Perform assertions based on the test case

In this example, we start measuring the execution time by calling time.time() and storing the result in start_time. After performing the necessary operations or function calls, we call time.time() again and store the result in end_time. We then calculate the execution time by subtracting start_time from end_time. Finally, we print the execution time.

Custom TestRunner

If we want a more structured approach for measuring execution time, we can create a custom TestRunner class that extends the unittest.TextTestRunner class. By overriding the run() method, we can add code to measure the execution time before and after running each test case.

Here’s an example of how to create a custom TestRunner class:

import unittest
import time

class CustomTestRunner(unittest.TextTestRunner):
    def run(self, test):
        start_time = time.time()
        result = super().run(test)
        end_time = time.time()

        execution_time = end_time - start_time
        print(f"Total execution time: {execution_time} seconds")

        return result

if __name__ == "__main__":
    unittest.main(testRunner=CustomTestRunner())

In this example, we create a CustomTestRunner class that extends the unittest.TextTestRunner class. We override the run() method and add code to measure the execution time before and after running the test cases. We then calculate the total execution time and print it.

To use this custom TestRunner, we call unittest.main() and pass testRunner=CustomTestRunner() as an argument.

Conclusion

Measuring the execution time of unittest test cases can provide valuable insights into the performance of our code. By identifying slow-running test cases, we can focus on optimizing those areas and improve overall efficiency.

In this blog post, we explored two approaches for measuring execution time in unittest test cases. The first approach used the time module to measure the time before and after each test case. The second approach involved creating a custom TestRunner class to measure the total execution time.

Remember that measuring execution time is just one aspect of performance testing. It is essential to consider other factors, such as memory usage and scalability, when optimizing code for production environments.