Drupal 6 and PHPUnit testing
http://kristiannissen.wordpress.com/2009/04/30/drupal-6-and-phpunit-test...
Drupal 6 and PHPUnit testing
I know that Drupal officially is using simpletest which is also a great testing framework, I like PHPUnit though, not for any particula reason, it was the first testing framework I used for PHP and I’ve stuck with it. So I decided to figure out how I could use PHPUnit with Drupal and it’s possible and did not take more than a couple of minutes to setup.Why even bother using unit testing when working with a CMS?
Easy. When you work on projects like dinepenge.dk, or aok.dk which are the projects I’m currently working on, having to load the entire website each time you want to test your modules is time consuming since you have to load the entire website with all it’s graphics and ads as well as external scripts and everything. Som being able to do initial tests using your command line will save you lots and lots of time, and it will make your code more stable as well. Trust me on this one, it will save you a lot of time!
So how is it done? Well as I mentioned it didn’t take me long to get up and running, remember, this is PHP and everything is about includes and file paths.
First of all, you have to run your tests from the same folder as Drupals index.php file is located since the bootstrapper is depending on it. Then you have to include Drupals bootstrapper file in your test cases or test suite otherwise nothing will work – all about file paths, remember?
Now your ready to test your module like this:
<?php
require_once 'includes/bootstrap.inc';
$_SERVER['REQUEST_METHOD'] = 'get';
$_SERVER['REMOTE_ADDR'] = '10.86.194.17';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
class FancyModuleTests extends PHPUnit_Framework_TestCase {
function test_fancymodule() {
$this->assertType("string", fancymodule_path_autocomplete('living'));
}
}
I have added my IP and a HTTP get method as part of the $_SERVER hash, I did this because Drupal threw a few PHP notices at me when I ran the test without.
PHP Notice: Undefined index: REMOTE_ADDR in includes\bootstrap.inc on line 927
PHP Notice: Undefined index: REMOTE_ADDR in includes\bootstrap.inc on line 868
To run this test, run the following command from your command line:
phpunit FancyModuleTests sites/all/modules/custom/fancymodule/FancyMo
- Login to post comments