13 Debugging and testing

 

This chapter covers

  • Reading tracebacks
  • Debugging your application interactively
  • Testing functions
  • Testing a class

Completing a programming project from scratch to production is like building a house. After you lay out the foundation, set up the frames and walls, complete the roof, and install the doors and windows, you feel that most of the house is done. But when you proceed to interior decoration, such as flooring, lights, furniture, and closets, you’ll realize that it’s still far from completion.

You’ve worked hard on your application for three months, feeling that you’ve completed 90% of the project. Before you push it into production, however, you must ensure its performance by debugging and testing it rigorously. It wouldn’t surprise me if the last estimated 10% costs you another three months—the same amount of time you needed for the first 90%. The debugging and testing phase is analogous to the interior decoration of a house—it’s so essential that your application can’t live without it—and you don’t want to hear your client’s complaints after launch day. Thus, let’s tackle the debugging and testing jobs while the application is still in our possession. In this chapter, you’ll learn the essential techniques that you can apply to conduct a rigorous final touch-up of your application: debugging and testing.

13.1 How do I spot problems with tracebacks?

13.1.1 Understanding how a traceback is generated

13.1.2 Analyzing a traceback when running code in a console

13.1.3 Analyzing a traceback when running a script

13.1.4 Focusing on the last call in a traceback

13.1.5 Discussion

13.1.6 Challenge

13.2 How do I debug my program interactively?

13.2.1 Activating the debugger with a breakpoint

13.2.2 Running code line by line

13.2.3 Stepping into another function

13.2.4 Inspecting pertinent variables

13.2.5 Discussion

13.2.6 Challenge

13.3 How do I test my functions automatically?