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 prove that, given the proper input, the proper output is displayed.
Using the unittest.mock module’s
patch() function, it’s pretty simple to mock out sys.stdout for just a single test, and put it back again, without messy temporary variables or leaking mocked-out state between test cases.
print function, by default, sends output to
sys.stdout. In order to test that output is actually getting there, it is to be mocked out using a stand-in object, and then make assertions about what happened.
patch() method makes it convenient to replace objects only within the context of a running test, returning things to their original state immediately after the test is complete.
Code #2 : Test code for the above code
urlprint()function takes three arguments, and the test starts by setting up dummy arguments for each one. The expected_url variable is set to a string containing the expected output.
- To run the test, the
unittest.mock.patch()function is used as a context manager to replace the value of
sys.stdoutwith a StringIO object as a substitute.
- The fake_out variable is the mock object that’s created in this process. This can be used inside the body of the with statement to perform various checks. When the with statement completes, patch conveniently puts everything back the way it was before the test ever ran.
- It’s worth noting that certain C extensions to Python may write directly to standard output, bypassing the setting of sys.stdout.
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 - sys.stdout.flush()
- How to print to stderr and stdout in Python?
- sys.stdout.write in Python
- Unit Testing in Python - Unittest
- Testing in Python using doctest module
- Python | Exceptional Conditions Testing in Unit Tests
- Automated software testing with Python
- Mutation Testing using Mutpy Module in Python
- Getting Started With Testing in Python
- Object oriented testing in Python
- Load testing using LOCUST
- PyQt5 QColorDialog - Testing Color Dialog Options
- Performing BVA Testing using Pytest
- 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)
- Python | Output using print() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.