Writing the specification

As usual, the process starts with a written specification. The specification is a doctest, (which we learned about in Chapters 2 and 3), so the computer can use it to check the implementation. The specification isn't strictly a set of unit tests though the discipline of unit testing has been sacrificed (for the moment) in exchange for making the document more accessible to a human reader. That's a common trade-off, and it's fine as long as you make up for it by also writing unit tests covering...

Time for action testing PID with unittest

We'll revisit the PID class or at least the tests for the PID class from Chapter 3. We'll rewrite the tests so that they operate within the unittest framework. Before moving on, take a moment to refer back to the final version of the pid.txt file from Chapter 3. We'll be implementing the same tests using the unittest framework. 1. Create a new file called test_pid.py in the same directory as pid.py. Notice that this is a .py file unittest tests are pure python source code, rather than being...

Time for action browsing the web with Twill

We'll take Twill for a spin, using its interactive interpreter. 1. Start the interactive Twill interpreter If you're using a version of Python older than 2.6, you won't be able to do a - -user installation, which means you'll need to have write access to the Python installation's site-packages directory. You may notice a couple of warnings about the deprecated md5 module when you start Twill. You may safely ignore them. 2. Get a list of Twill commands. You can get further information about a...

Twill commands

Twill has a number of commands, and so far we've only covered a few of them. In this section you'll find a brief discussion of each of Twill's commands. The help command prints out a list of all of Twill's commands, or tells you the details of a specific command. For example, to get the details of the add_auth command, you should type Help for command add_auth add_auth lt realm gt lt uri gt lt user gt lt passwd gt Add HTTP Basic Authentication information for the given realm uri. If you want to...

Replacing library objects with mocks

Several times, we've seen a need to replace something outside of our own code with a mock object for example, time.time needed to be replaced with something that produced predictable results, in order for the tests on our PID controller to be meaningful. Mocker provides us with a tool to address this common need, and it's quite simple to use. Mocker's mocking contexts contain a method called replace which behaves pretty much like mock from our point of view, but which is able to completely...