Testing rocks, but debugging? Not so much. The fact that there’s no compiler to analyze your code
before Python executes it makes testing a critical part of development. The
goal of this chapter is to discuss some common problems related to testing, debugging,
and exception handling. It is not meant to be a gentle introduction to test-driven
development or the
unittest module. Thus, some familiarity with testing concepts
You have a program that has a method whose output goes to standard
sys.stdout). This almost always means that it emits text to
the screen. You’d like to write a test for your code to prove that,
given the proper input, the proper output is displayed.
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.
Consider, as an example, the following function in a module
sys.stdout. In order
to test that output is actually getting there, you can mock it out
using a stand-in object, and then make assertions about what happened. Using
patch() method makes it convenient to replace objects only within ...