What is the state of unit testing with Drupal today?

mrchrisadams's picture

Hi there,

I'm coming back to Drupal after a brief hiatus, and although I've looked around, I can't seem to find any up to date information about how best to unit test Drupal (I'm using D6 for now, but I haven't found anything for D7 either).

I'm asking about Unit testing, because my previous experiments with Integration Testing seemed unusably slow, and although there is this article here on lullabot, it's about 4 years old now, and it seems somewhat out of date.

A recent post here by Jacob Singh on the subject makes me start to think that it's just something that you can't do in any reasonable fashion with Drupal, but before I give up, I'd really like to ask here first.

There's a lot to like about Drupal as a platform, and I'm aware that Drupal's design can make unit testing hard, because you end up mocking up so much extra code to account for all the various hooks being fired everywhere that any test cases become extremely unwieldy.

Quoting Jacob Sing in the comments:

Everytime I've sat down to write unit tests in Drupal, my code ends up become so convoluted to avoid touch any piece of Drupal itself that I'm not testing anything. Couple that with the fact that because we are not using classes, I can't extend and mock up calls that would use Drupal.

I'm really wary about doing development on large projects without the safety net of tests - are there any resources people on the list here could point me to, to make unit testing with Drupal a reality?

Right now, the best bet seems to be using PHPUnit for unit tests, and Simpletest for Integration tests, which seems crazy.

Sorry if I sound a little negative, I'm aware there are large gaps in my knowledge of Drupal, and I'm welcome to be put straight if anything I've said above doesn't ring true.




mongolito404's picture

I never used it, but SimpleTest includes the DrupalUnitTestCase class for unit testing without database access. No database access is a major drawback since most of Drupal core isn't designed to work without a database. But if you want to unit-test your code with PHPUnit, you are in the same situation and cannot rely on having Drupal's database available.

To run individual test, Drush's test-run command allow running only a few selected test methods.

To speed-up testing, I'm using a SQLite database in a RAM disk (/dev/shm on Ubuntu). That's easy to setup compared to running MySQL in RAM as I only have to do a drush --yes site-install && drush --yes pm-enable simpletest at reboot to get my test local environment ready.

Overview of PHPUnit in relation to Drupal

VinceW's picture

I'm working on a wikipage in this group. http://groups.drupal.org/node/157164

You might be interested in the link about the CiviCRM forum. They use PHPUnit for there testing and the forum has interesting, and recent, information.


-=[ Your Information Matters ]=-

Testing and Quality Assurance

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: