Python | Logging Test Output to a File Last Updated : 12 Jun, 2019 Comments Improve Suggest changes Like Article Like Report Problem - Writing the results of running unit tests to a file instead of printed to standard output. A very common technique for running unit tests is to include a small code fragment (as shown in the code given below) at the bottom of your testing file. Code #1 : Python3 1== import unittest class MyTest(unittest.TestCase): ... if __name__ == '__main__': unittest.main() This makes the test file executable, and prints the results of running tests to standard output. To redirect this output, unwind the main() call a bit and write own main() function as shown in the code given below : Code #2 : Python3 1== import sys def main(out = sys.stderr, verbosity = 2): loader = unittest.TestLoader() suite = loader.loadTestsFromModule(sys.modules[__name__]) unittest.TextTestRunner(out, verbosity = verbosity).run(suite) if __name__ == '__main__': with open('testing.out', 'w') as f: main(f) How it works : The interesting thing about the code is not so much the task of getting test results redirected to a file, but the fact that doing so exposes some notable inner workings of the unittest module. At a basic level, the unittest module works by first assembling a test suite. This test suite consists of the different testing methods you defined. Once the suite has been assembled, the tests it contains are executed. These two parts of unit testing are separate from each other. The unittest.TestLoader instance created in the solution is used to assemble a test suite. The loadTestsFromModule() is one of several methods it defines to gather tests. In this case, it scans a module for TestCase classes and extracts test methods from them. The loadTestsFromTestCase() method (not shown) can be used to pull test methods from an individual class that inherits from TestCase. The TextTestRunner class is an example of a test runner class. The main purpose of this class is to execute the tests contained in a test suite. This class is the same test runner that sits behind the unittest.main() function. Comment More infoAdvertise with us Next Article Python | Logging Test Output to a File M manikachandna97 Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads Python | Add Logging to a Python Script In this article, we will learn how to have scripts and simple programs to write diagnostic information to log files. Code #1 : Using the logging module to add logging to a simple program Python3 1== import logging def main(): # Configure the logging system logging.basicConfig(filename ='app.log', le 2 min read Python | Add Logging to Python Libraries In this article, we will learn how to add a logging capability to a library, but donât want it to interfere with programs that donât use logging. For libraries that want to perform logging, create a dedicated logger object, and initially configure it as shown in the code below - Code #1 : Python3 # 2 min read Open a File in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, each line of text is terminated with a special character called EOL 6 min read How to Disable Output Buffering in Python Output buffering is a mechanism used by the Python interpreter to collect and store output data before displaying it to the user. While buffering is often helpful for performance reasons there are situations where you might want to disable it to ensure that output is immediately displayed as it is g 2 min read Python | Testing Output to stdout Testing is a critical part of development as there is no compiler to analyze the code before Python executes it. Given a program that has a method whose output goes to standard Output (sys.stdout). This almost always means that it emits text to the screen. One likes to write a test for the code to p 2 min read Like