An Introduction to Drupal Unit Testing

In an effort to improve our QA and debugging processes, Raincity Studios considered using unit testing. This topic is getting a lot of attention right now from the community and I must tell I am as much interested; I did not need more of an excuse to dig a bit more into it.
What is Unit Testing?
The basic principle of Unit Testing is to build scripts to check that code meets specific feature details. Generally speaking, unit tests are used to check the behavior of a particular function under specific conditions or circumstances. This could be for example checking that a function is correctly sorting integers with the biggest values at the end of the list.
Since the very one to benefit from these tests is the programmer himself, it usually make sense that he is in charge of implementing the Unit Tests. These will simply assert, or not, that the code works as expected.
Why Unit Testing?
Developers, including Drupal-ers, tend to run regularly their code to check that there is neither syntax nor execution error in their scripts. But validating code through the compiler or interpreter only means it is grammatically correct, while there is still no guarantee that it functions the way it is intended to. Fortunately, Unit Testing can make our life easier. Implementing Unit Tests we can make sure that code verify predefined use cases.
Advantages and benefits:
The following is widely inspired from testage.net:
- Validation: you can test every single function all along the development process. Even in most cases of function refactoring, Unit Tests are not to be modified.
- Design & planning: implementing Unit Tests before functions lead developers to consider use cases from an external point of view; by doing so, you tend to build easily callable and testable functions.
- It’s documentation: Unit Testing is a good way of generating user and functional documentation. This documentation exists as a compilable and runnable resource, which is updated along with the code.
Unit tests can be run automatically, integrating seamlessly into your development and deployment process. These can be run before code commit to the repository or after adding new modules, thus ensuring that previous functionalities haven't been compromised. You can also run tests after deploying changes from a development environment to a staging environment, thus cutting down on the dependency of trial and error to detect problems.
What testing tools does Drupal offer?
The principal tool for Unit Testing in Drupal is the Simpletest module. This module is a Drupal extension to the Simpletest project for general PHP unit testing. The Drupal module adds a bunch of features specific to Drupal Unit Testing. For example, it extends Simpletest to enable user and node creation, variable setting and form submission within the Drupal framework.
How to set up the Simpletest module?
Robert Douglass from lullabot already wrote a pretty nice introduction to SimpleTest, and I will allow myself to steal his words below:
To begin running unit tests on your Drupal installation, download the latest release of the Simpletest module and install it as you would any other module. Please note that you should not install this module on a production site. It is only designed for development and staging purposes, and running the tests will alter the state of your database. Running the simpletest unit tests on a production site could lead to lost data or unpredictable behavior.
The Drupal module depends on the Simpletest library, which you can download from Sourceforge.net. The latest release, as of this writing, is simpletest_1.0.1. The download from Sourceforge comes as a tarball which you need to extract into the simpletest module directory.
Awkwardly translated from Chinese by Ronan Berder.












