Prev/Next links

Running SimpleTests' TestCases within Bash

Unit Testing is the way to roll nowadays, so after a few years of lacking behind on the unit testing part I decided to catch up and use it properly with Drupal. It's fairly easy to install the basics, just download the stable simpletest module. Extract the archive and copy it to your favorite directory. (in my case it's sites/default/modules/contrib)

And here the heavy lifting starts, you've to apply at least 1 patch to the Drupal Core. This ain't to hard when using a linux/unix system with patch installed, but I'm left with a very modern Windows 7 machine ... After some looking around I decided to download an vmware image with debian. There are a few ports of the most important GNU Tools for the Windows system but that just feels awkward. So I grepped my image, booted it and installed all the minor tools I needed (wget, links2, screen, patch, less, vim, mysql-client, and so on)

In order to let you all appreciate my current development environment, envision 2 monitors, 1 machine, a basic Windows 7 installation (running IIS 7.x), the windows php 5.2.14 with xdebug extension, some gnu tools like curl and a vmware Debian Lenny with a handy Gnome Installation. I also installed the vmware tools, so I can easily share files between my host system (Windows) and my Development System (Lenny). Yeah I know, sounds pretty awesome, nerdy and overly complicated ... I need my windows system in order to easily develop with C# and I use the Debian system in order to make normal use of the Drupal Shell (Drush), easy MySQL Management from the Console. I would love to attain the same level of powershell abuse as I have with bash ... But for now, I just have to stick with bash in order to make my life less miserable. It's also possible to run the all your implemented simpletests from a shell script , kindly provided by the simpletests maintainer. This script is also only really working when used alongside bash. (For the record, I love the idea behind cygwin but with the ease of vmware I would recommand tooling up a fully working unix/linux environment)

So after about an hour I had my Lenny working the way I wanted it and decided to wrap up the first TestCase. It took me about an hour to create a working testCase, you can do this much faster when just following the guidelines. I got myself some coffee and started the simpletest command, sudo -u www-data php ./scripts/run-tests.sh --list; I run this command from the root drupal directory. (Keep in mind, you should have applied a patch to the Drupal Core and copied the run-tests.sh to the Core Scripts directory and installed the php curl extension)

My custom TestCase didn't show up, and I almost lost my interest. The results for 2 hours work, were kinda meager but I decided to give it another go. I remember reading something about rebuilding the cache, so I fired drush up and gave the cache-clear all command. I retried the listing command and noticed my own little TestCase residing in the options list and felt some joy.

So I threw in another command, I wanted to see my TestCase run; the manual told me to use sudo -u www-data php ./scripts/run-tests.sh MyTestCase in order to make it happen. I never got any results. The command started, finished but didn't show any failed TestCases. I felt kinda betrayed, I had allready invested a good part of my day and didn't got any results. I fired up my debugger (xDebug saved the day, again) ... after a few more test runs I found out, again that the devil is in the in the details. I also had to provide the --url parameter.

I'll provide a patch in the forthcoming days that will at least check for the required parameters and provide you kindly with an error message that explains how to run the command you want to start.

Short checklist in order to Run Simpletests TestCases from the Commandline (bash)

  1. Use a Linux/Unix-based system
  2. Use Drupal 6.17
  3. Make sure you've patch installed
  4. Make sure you've applied the D6 Core Patch
  5. Make sure you've installed the curl extension for Php
  6. Make sure you've a working Drush (at least when you want to run your TestCases from the Command line)
  7. Make sure you've copied the run-tests.sh script to the Core Scripts Directory, located within you Drupal Root Directory
  8. Make sure you've enabled the simpletest module (drush pm-enable simpletest)
  9. Make sure you've configured Simpletest (they need an user in order to login to the system when running a TestCase)
  10. Make sure you've created a MyModule.Test file next to the MyModule.Module and the MyModule.install files
  11. Make sure you've followed the Guidelines when naming and createing the TestCase class (yes here we're using OO)
  12. Make sure your TestCase shows up, after a cache clean (drush cache-clear, all) when using the --list command
  13. Make sure you run your TestCase and provide a --url or you won't have a proper HTTP_HOST and Drupal ain't capable of bootstrapping itself. (It wouldn't know which site to start)
  14. Don't forget to bring enough coffee ... A simple TestCase takes about 1 minute to start, run and finish; each time you run your testcases Drupal Generates a clean Working Environment!