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 :
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 :
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.TestLoaderinstance created in the solution is used to assemble a test suite.
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.
loadTestsFromTestCase()method (not shown) can be used to pull test methods from an individual class that inherits from TestCase.
TextTestRunnerclass 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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Logging in Python
- Create an Exception Logging Decorator in Python
- Python VLC Instance - Unsetting Logging Callbacks
- reStructuredText | .rst file to HTML file using Python for Documentations
- Create a GUI to convert CSV file into excel file using Python
- Python - Get file id of windows file
- Python program to reverse the content of a file and store it in another file
- MoviePy – Getting Original File Name of Video File Clip
- PYGLET – Opening file using File Location
- PyCairo - Saving SVG Image file to PNG file
- Kolmogorov-Smirnov Test (KS Test)
- Reading Python File-Like Objects from C | Python
- Output of Python Program | Set 1
- Output of Python programs | Set 9 (Dictionary)
- Output of Python Programs | Set 22 (Loops)
- Output of Python Programs | Set 24 (Dictionary)
- Generate two output strings depending upon occurrence of character in input string in Python
- Output of Python Programs | (Dictionary)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.