How to: Write Automated Tests for Drupal

With an automated testing framework in core, Drupal is now far along the road to a practice of Test-driven development. But there's one thing missing: a complete, in-depth, up-to-date tutorial on how to actually write these tests. Although there is the SimpleTest Automator module to help you create these tests through the user interface, it is currently imperfect and under development, so this post will be a tutorial on writing these tests manually.

Testing resources

Here's just a general list of resources we should keep handy while writing our test:

  • Testing API functions - This is a quick cheat sheet of some functions that are very helpful during testing. These include controlling the internal testing browser, creating users with specific permissions, and simulating clicking on links that appear in the page.
  • Available assertions - Assertions are how you determine whether your code is working or not - you assert that it should be working, and let the testing framework handle the rest for you. This is a library on the assertions that are available to use in our testing framework.

Paris SimpleTest Sprint, Day 2

Only hours ago, after over three years of work on the SimpleTest module, Dries committed the SimpleTest module to core. This is an amazing achievement. Drupal is looking at getting 100% core test coverage, both unit and functional. A lot of work went into the SimpleTest module today before it was committed. We (the Drupal SimpleTest Sprinters) set up an SVN repository containing a version of Drupal with SimpleTest there as a module, and opened it up for all of us to commit. It was chaos (in a good way). Without the roadblock of patches, code was rapidly sucked into the SimpleTest module; finally Dries had to give us a SimpleTest "code freeze" so as to actually start to get stuff to work.

Current SimpleTest Status

SimpleTest is in core! What can be left to do? Actually, a lot, but first, let's go over what we've done today:

  • Got SimpleTest into Core!
  • Got all tests except for Trigger and Poll tests actually passing!
  • Got PHPCoverage to generate a 500 MB XML file on Rok's laptop! (Though we have yet to find a script that can deal with all that data without crashing).
  • Made SimpleTest's web interface pretty with the addition of icons and assertion groups!
  • Got the code necessary for working!

And then, there are the things that are left to do:

Paris SimpleTest Sprint, Day 1

Today we began the SimpleTest code sprint! This is an area of huge importance to Drupal, as one of our weaknesses has proven to be the number of testers we have, particularly how small that number is. Automated testing will provide an easy way to maintain the quality of core— each time a patch is created for core, it will be sent off to a testing server where the patch will be applied to a copy of the Drupal core and all the SimpleTests run on it. If there are failures, the patch would be marked as such and whoever submitted the patch would be forced to hang their head in shame.

What we accomplished today

Today, Dries Buytaert, Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Douglas Hubler, Miglius Alaburda and I got together for 10 hours for the sole purpose of improving automated testing in core. It was great. We bounced ideas off one another, and came up with a great plan for unit testing with mock functions and classes without runkit. We made our test coverage tests work, although they are still running :). We gave a presentation to the people at Drupal Camp Paris on how to write basic functional tests for both core and contributed modules. We talked to Dries about the possibility of getting SimpleTest into core.

Google sponsors two Drupal developers to Testing Sprint, Paris

Thanks to the amazing Leslie Hawthorn and the awesome Chris DiBona, we can add an additional two sprinters to the list for SimpleTest Sprint, Paris! Both Charlie Gordon (me) and Jimmy Berry (aka boombatower) are now going to be at the testing sprint! Let's all thank Google for this awesome act of kindness towards the Drupal community!

SimpleTest is critical to Drupal, not only for quality assurance, but also to postpone the code freeze so we can add more features to Drupal 7 and make it as awesome as possible.

As this beautiful graph clearly shows, after Drupal has made the switch to complete SimpleTest coverage for core, the quality of Drupal will skyrocket, as we will be able to ensure that each patch committed to core does not break any of the SimpleTests, therefore less bugs and more features! When we have complete core SimpleTest coverage for Drupal, we will be free to explore awesome new features instead of spending precious time on bug hunting and bug fixing.

Let's make that Drupal 7 release a killer! :)

The power of Drupal simpletests and the simpletest revolution

If you are looking for an up-to-date SimpleTest tutorial, please see my newer post.

The Drupal simpletest module is awesome! I discovered it while I was looking for a new GHOP task, and I loved the idea of it immediately. So, I claimed the task, and I'm writing now to share what I learned with you.

What it is

The simpletest module extends the php simpletest framework. Basically, it allows users to run a series of automated tests on a Drupal install.

Why it's useful

Often, new code is entered into Drupal contributed modules, and even the Drupal core, without being thoroughly tested - or even tested at all, in some situations. With simpletest, tests become so easy (without having to manually try maybe hundreds of situations) that finding and fixing bugs becomes a breeze.

Syndicate content