# Unit Tests in Twisted

Each unit test tests one bit of functionality in the software. Unit tests are entirely automated and complete quickly. Unit tests for the entire system are gathered into one test suite, and may all be run in a single batch. The result of a unit test is simple: either it passes, or it doesn't. All this means you can test the entire system at any time without inconvenience, and quickly see what passes and what fails.

## Unit Tests in the Twisted Philosophy

The Twisted development team adheres to the practice of Extreme Programming (XP), and the usage of unit tests is a cornerstone XP practice. Unit tests are a tool to give you increased confidence. You changed an algorithm -- did you break something? Run the unit tests. If a test fails, you know where to look, because each test covers only a small amount of code, and you know it has something to do with the changes you just made. If all the tests pass, you're good to go, and you don't need to second-guess yourself or worry that you just accidently broke someone else's program.

## What to Test, What Not to Test

> You don't have to write a test for every single method you write, only production methods that could possibly break.

-- Kent Beck, Extreme Programming Explained, p. 58.

## Running the Tests

### How

    $ Twisted/admin/runtests

You'll find that having something like this in your emacs init files is quite handy:

    (defun runtests () (interactive)
      (compile "python /somepath/Twisted/admin/runtests"))

    (global-set-key [(alt t)] 'runtests)

### When

Always always always be sure all the tests pass before committing any code. If someone else checks out code at the start of a development session and finds failing tests, they will not be happy and may decide to hunt you down.

Since this is a geographically dispersed team, the person who can help you get your code working probably isn't in the room with you. You may want to share your work in progress over the network, but you want to leave the main CVS tree in good working order. So use a branch, and merge your changes back in only after your problem is solved and all the unit tests pass again.

## Adding a Test

Please don't add new modules to Twisted without adding tests for them too. Otherwise we could change something which breaks your module and not find out until later, making it hard to know exactly what the change that broke it was, or until after a release, and nobody wants broken code in a release.

Tests go in Twisted/twisted/test/, and are named `test_foo.py`, where `foo` is the name of the module or package being tested. Extensive documentation on using the PyUnit framework for writing unit tests can be found in the links section below.

One deviation from the standard PyUnit documentation: To ensure that any variations in test results are due to variations in the code or environment and not the test process itself, Twisted ships with its own, compatible, testing framework. That just means that when you import the unittest module, you will `from twisted.trial import unittest` instead of the standard `import unittest`.

As long as you have followed the module naming and placement conventions, `runtests` will be smart enough to pick up any new tests you write.

## Links

- A chapter on Unit Testing in Mark Pilgrim's Dive Into Python.
- `unittest` module documentation, in the Python Library Reference.
- UnitTests on the PortlandPatternRepository Wiki, where all the cool ExtremeProgramming kids hang out.
- Unit Tests in Extreme Programming: A Gentle Introduction.
- Ron Jeffries espouses on the importance of Unit Tests at 100%.
- Ron Jeffries writes about the Unit Test in the Extreme Programming practices of C3.
- PyUnit's homepage.
- twisted.test's inline documentation.
- The twisted/test directory in CVS.

Index