Starbow Consulting

Starbow Consulting

Home Tao's Blog Contact Search

Home

Tao's Blog

Contact

Search

Home » Blogs » tao's blog SimpleTest tips Submitted by tao on Thu, 2007-03-29 12:05 Drupal testing Enough ranting about testing, here are some useful tips for enjoying SimpleTest. Getting Rolling If this is your first time, start by downloading the SimpleTest module, and the Simple Test framework. Then create a directory in your module called tests, and add a file your_module.test. Here is a template: <?php class YourModuleTest extends DrupalTestCase { function get_info () { return array( 'name' => t ( 'Your Module Test' ), 'desc' => t ( 'blah, blah' ), 'group' => 'Your Tests' ); } function setUp () { // do any set up } function testRawFragment () { // get rolling with $this->asserts } ?> Then add this to your module: <?php function your_module_simpletest () { $dir = drupal_get_path ( 'module' , 'your_module' ). DIRECTORY_SEPARATOR . 'tests' ; $tests = file_scan_directory ( $dir , '\.test' ); return array_keys ( $tests ); } ?> There are other ways to do it, but this will get you up and running. Custom Path I like functionality of the SimpleTest but I don't care for the interface. So I just create a custom drupal path to run my tests directly: <?php function your_module_menu ( $may_cache ) { if( $may_cache ) { $items [] = array( 'path' => 'your_module/test' , 'callback' => 'your_module_test' , 'access' => true , 'type' => MENU_CALLBACK ); } return $items ; } function your_module_test () { simpletest_load (); $output = simpletest_run_tests ( array( 'YourModuleTest' ) ); print theme ( 'page' , $output ); } ?> Validating HTML Fragments Saving the best for last. I am currently working on code that generates html fragments. Simple Test out-of-the-box does a great job helping you test entire http pages, but only it if goes and gets them itself. I could not find any easy way to load in a local page/fragment. So I did it the hard way: <?php function setUp () { require_once( SIMPLE_TEST . '/mock_objects.php' ); Mock :: generate ( 'SimpleHttpResponse' ); } function setSource ( $html ) { $this -> _mockResponse = new MockSimpleHttpResponse (); $this -> _mockResponse -> setReturnValue ( 'isError' , false ); $this -> _mockResponse -> setReturnValue ( 'getContent' , $html ); $this -> _browser -> _page = $this -> _browser -> _parse ( $this -> _mockResponse ); } ?> Now, in my tests, I can say great things like this: <?php $html = drupal_get_form ( $form_id ); $this -> assertNull ( form_get_errors () ); $this -> setSource ( $html ); $this -> assertText ( 'bar' ); $this -> assertField ( 'setme' , 'one' ); $this -> assertNull ( $this -> _browser -> getField ( 'added' ) ); ?> tao's blog Add new comment

SimpleTest tips Submitted by tao on Thu, 2007-03-29 12:05 Drupal testing Enough ranting about testing, here are some useful tips for enjoying SimpleTest. Getting Rolling If this is your first time, start by downloading the SimpleTest module, and the Simple Test framework. Then create a directory in your module called tests, and add a file your_module.test. Here is a template: <?php class YourModuleTest extends DrupalTestCase { function get_info () { return array( 'name' => t ( 'Your Module Test' ), 'desc' => t ( 'blah, blah' ), 'group' => 'Your Tests' ); } function setUp () { // do any set up } function testRawFragment () { // get rolling with $this->asserts } ?> Then add this to your module: <?php function your_module_simpletest () { $dir = drupal_get_path ( 'module' , 'your_module' ). DIRECTORY_SEPARATOR . 'tests' ; $tests = file_scan_directory ( $dir , '\.test' ); return array_keys ( $tests ); } ?> There are other ways to do it, but this will get you up and running. Custom Path I like functionality of the SimpleTest but I don't care for the interface. So I just create a custom drupal path to run my tests directly: <?php function your_module_menu ( $may_cache ) { if( $may_cache ) { $items [] = array( 'path' => 'your_module/test' , 'callback' => 'your_module_test' , 'access' => true , 'type' => MENU_CALLBACK ); } return $items ; } function your_module_test () { simpletest_load (); $output = simpletest_run_tests ( array( 'YourModuleTest' ) ); print theme ( 'page' , $output ); } ?> Validating HTML Fragments Saving the best for last. I am currently working on code that generates html fragments. Simple Test out-of-the-box does a great job helping you test entire http pages, but only it if goes and gets them itself. I could not find any easy way to load in a local page/fragment. So I did it the hard way: <?php function setUp () { require_once( SIMPLE_TEST . '/mock_objects.php' ); Mock :: generate ( 'SimpleHttpResponse' ); } function setSource ( $html ) { $this -> _mockResponse = new MockSimpleHttpResponse (); $this -> _mockResponse -> setReturnValue ( 'isError' , false ); $this -> _mockResponse -> setReturnValue ( 'getContent' , $html ); $this -> _browser -> _page = $this -> _browser -> _parse ( $this -> _mockResponse ); } ?> Now, in my tests, I can say great things like this: <?php $html = drupal_get_form ( $form_id ); $this -> assertNull ( form_get_errors () ); $this -> setSource ( $html ); $this -> assertText ( 'bar' ); $this -> assertField ( 'setme' , 'one' ); $this -> assertNull ( $this -> _browser -> getField ( 'added' ) ); ?> tao's blog Add new comment

SimpleTest tips Submitted by tao on Thu, 2007-03-29 12:05 Drupal testing

Drupal

testing

Enough ranting about testing, here are some useful tips for enjoying SimpleTest.

Getting Rolling If this is your first time, start by downloading the SimpleTest module, and the Simple Test framework. Then create a directory in your module called tests, and add a file your_module.test. Here is a template:

Then add this to your module:

There are other ways to do it, but this will get you up and running.

Custom Path I like functionality of the SimpleTest but I don't care for the interface. So I just create a custom drupal path to run my tests directly:

function

Validating HTML Fragments Saving the best for last. I am currently working on code that generates html fragments. Simple Test out-of-the-box does a great job helping you test entire http pages, but only it if goes and gets them itself. I could not find any easy way to load in a local page/fragment. So I did it the hard way:

Now, in my tests, I can say great things like this:

Add new comment

Recent Drupal Posts Working at GoingOn Networks BADCamp 2010 Notes from BADCamp Eclipse Installfest Welcome to BADCamp 2009 Example from Intro to Module Development Presentation Quick WYSIWYG with Image uploading recipe more Recent comments Hi Tao! Can you post 1 year 48 weeks ago Mayk Brito on: Working at GoingOn Networks No way 1 year 48 weeks ago tao on: Mysqldump and Unicode This only works for the first item in a node reference results.. 1 year 51 weeks ago Anonymous on: Reverse Node References On drupal.org? 2 years 10 hours ago Simon on: Wikis in Drupal 6 - Module R... $form_state in Drupal 6 2 years 3 weeks ago vagkavan on: The best way to show results... more Tweets by @tstarbow Sad that the @typesafe repo is down. 5 weeks 4 days ago Feeling very at home with the Dead Can Dance crowd. 8 weeks 6 days ago @netsolcares Can you confirm/deny DDOS attack? 9 weeks 2 days ago @boss99er Geez, that is not good. 9 weeks 2 days ago 1 of 60 ›› Follow me on Twitter Today's popular content Effort to fix Install Profiles in Drupal 7 (2) PDT Milestone 1 (2) Whee! (2) Berkeley Drupal Group - THURSDAY September 27th Meeting (2) Global Search and Replace (1) more User login Username: * Password: * Request new password

Working at GoingOn Networks

BADCamp 2010

Notes from BADCamp Eclipse Installfest

Welcome to BADCamp 2009

Example from Intro to Module Development Presentation

Quick WYSIWYG with Image uploading recipe

Hi Tao! Can you post 1 year 48 weeks ago Mayk Brito on: Working at GoingOn Networks

No way 1 year 48 weeks ago tao on: Mysqldump and Unicode

This only works for the first item in a node reference results.. 1 year 51 weeks ago Anonymous on: Reverse Node References

On drupal.org? 2 years 10 hours ago Simon on: Wikis in Drupal 6 - Module R...

$form_state in Drupal 6 2 years 3 weeks ago vagkavan on: The best way to show results...

Sad that the @typesafe repo is down. 5 weeks 4 days ago

Feeling very at home with the Dead Can Dance crowd. 8 weeks 6 days ago

@netsolcares Can you confirm/deny DDOS attack? 9 weeks 2 days ago

@boss99er Geez, that is not good. 9 weeks 2 days ago

1 of 60

Effort to fix Install Profiles in Drupal 7 (2)

PDT Milestone 1 (2)

Whee! (2)

Berkeley Drupal Group - THURSDAY September 27th Meeting (2)

Global Search and Replace (1)

Request new password