[파이썬] unittest 테스트 결과 출력 제어하기

Unittest is a popular testing framework in Python that allows developers to write and run tests for their code. When running tests using unittest, the framework outputs the test results to the console. Sometimes, you may want to control how the test results are displayed, such as hiding certain output or customizing the format.

In this blog post, we will explore different ways to control the test result output in unittest.

1. Verbose Mode

By default, unittest displays detailed information about each test case and their results. However, you can enable the verbose mode to get more output, including the names of all the tests being run.

python -m unittest -v my_module_test.py

The -v flag is used to enable verbose mode. This can be helpful when you want to see the detailed output of each individual test case during test runs.

2. Quiet Mode

On the other hand, if you prefer a cleaner and more concise test result output, you can use the quiet mode. This mode suppresses most of the detailed information, resulting in a more minimalistic output.

python -m unittest -q my_module_test.py

The -q flag is used to enable quiet mode. This is particularly useful when you have a large number of tests and want to focus on the overall test results without being overwhelmed by excessive output.

3. Test Result Output Redirect

In some cases, you may want to redirect the test result output to a file instead of displaying it on the console. This can be done by using the unittest.TextTestRunner class and specifying the output file.

import unittest

class MyTestCase(unittest.TestCase):
    def test_something(self):
        # ...

if __name__ == '__main__':
    with open('test_results.txt', 'w') as f:
        runner = unittest.TextTestRunner(stream=f)
        unittest.main(testRunner=runner)

In this example, the stream argument of TextTestRunner is set to a file object created using open(). The test result output will be written to the specified file instead of the console.

4. Custom Test Result Output Format

If you need more control over the format of the test result output, you can implement a custom test result class by subclassing unittest.TestResult. This allows you to capture and format the test results in any way you want.

import unittest

class MyTestResult(unittest.TestResult):
    def startTest(self, test):
        super().startTest(test)
        # Custom logic to handle test start

    def addSuccess(self, test):
        super().addSuccess(test)
        # Custom logic to handle successful test

    def addFailure(self, test, err):
        super().addFailure(test, err)
        # Custom logic to handle failed test

    # ...

if __name__ == '__main__':
    runner = unittest.TextTestRunner(resultclass=MyTestResult)
    unittest.main(testRunner=runner)

In this example, the MyTestResult class is created by subclassing unittest.TestResult. By overriding its different methods, you can customize the behavior for handling test start, success, failure, and other events.

Once the custom test result class is implemented, you can pass it to unittest.TextTestRunner using the resultclass argument to use the custom output format.

Conclusion

Controlling the test result output in unittest provides flexibility and customization options to suit your specific testing needs. By using verbose mode, quiet mode, output redirection, or even implementing a custom test result class, you can control and format the test results to your liking.

Happy testing!