Parameter matching

Sometimes, we would like our mocked functions and methods to accept a whole domain of parameters, instead of limiting itself to the accepting objects that compare equal to the parameters we specifically told it about. This can be useful for any number of reasons perhaps the mock needs to accept an external variable as a parameter (the current time, or available disk space, for example), or maybe the mock example will be invoked multiple times (which we'll discuss soon), or maybe the parameters...

Specifying that an expectation should occur multiple times

As you may have noticed in some of the preceding examples, sometimes telling Mocker what to expect can get repetitive. The example of the IN parameter matcher show this well We did a lot of repetitive work telling Mocker that we expected three calls to the func function. That makes the test long (which reduces its readability) and it violates the DRY (Don't Repeat Yourself) principle of programming, making it harder to modify the test later on. Besides which, it's annoying to write all those...

Time for action embedding a doctest in a docstring

We'll embed a test right inside the Python source file that it tests, by placing it inside a docstring. 1. Create a file called test.py with the following contents The testable function returns the square root of its parameter, or 3, whichever is larger. > > > testable(10) 10 ** 0.5 2. At the command prompt, change to the directory where you saved test.py and then run the tests by typing As mentioned earlier before, if you have an older version of Python, this isn't going to work for you....

Time for action identifying units

Imagine that you're responsible for testing the following code def method1(self, number) number + 4 number ** 0.5 number * 7 return number return self.method1(number) + self.method2(number) return 1.713 * self.method3(id(self)) 1. In this example, what are the units Is the whole class a single unit, or is each method a separate unit. How about each statement, or each expression Keep in mind that the definition of a unit is somewhat subjective (although never bigger than a single class), and...

Time for action installing Nose as a Darcs postrecord hook

Each local repository has its own set of hooks, so the first thing we need to do is create a repository. Make a directory to work in, and execute the following command in it 2. We need to put some test code in the repository directory so that Nose has something to do. Place the following (useless) code into a file called class test_simple(TestCase) def test_one(self) Hooks) self.assertEqual(Same, Same) 3. Run the following command to add the test file to the repository 4. Darcs hooks are...

Time for action installing Python Mocker

At the time of this writing, Python Mocker's home page is located at http labix.org mocker, while its downloads are hosted at Go ahead and download the newest version, and we'll see about installing it. 2. The first thing that needs to be done is to unzip the downloaded file. It's a .tar.bz2, which should just work for Unix, Linux, or OSX users. Windows users will need a third-party program (7-Zip works well http www.7-zip.org ) to uncompress the archive. Store the uncompressed file in some...

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...

A nifty trick

If you're using Python 2.4 or greater, you can define a function decorator that makes it simple to write Twill tests as Python functions. from twill.parse import execute_string from twill.errors import TwillAssertionError def twill_test func execute_string func._doc_, no_reset False if args and hasattr args 0 , 'fail' If you put that code in a Python module here called twill_decorator and then import twill_test into your testing code, you can write Twill tests like so from unittest import...

Time for action using Buildbot with Bazaar

To set up a buildmaster, create a directory for it to operate in and then run the command buildbot create-master lt directory gt where lt directory gt is the directory you just created for buildbot to work in. 2. Similarly, to set up a buildslave, create a directory for it to operate in and then run the command buildbot create-slave lt directory gt lt host port gt lt name gt lt password gt where lt directory gt is the directory you just created for the buildbot to work in, lt host port gt...

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...