Beginning SimpleTest in Drupal6

18th Jun 2010 // By Alexander // Drupal Planet

I have been using Drupal for nearly 4 weeks now, and SimpleTest even less. I just wanted to share some things which I found, and that others learning SimpleTest should look out for.

Single test environment

The first snag I ran into whilst diving into SimpleTest (always did push buttons first, read instructions later) was when I tried to run sequential tests.

public function testAddBroadcast(){} public function testAddNodeToBroadcast(){}

When I tried to add the node to the broadcast I had created in the first function, I found that it didn't exist. This is because SImpleTest will call the setUp and tearDown methods on each one of the tests, like so:

  1. $this->setUp();
  2. $this->testAddBroadcast();
  3. $this->tearDown();
  4.  
  5. $this->setUp();
  6. $this->testAddBroadcast();
  7. $this->tearDown();

It's such a simple thing to overlook, but affected the performance of my tests dramatically.

Failing SimpleTest's fail()

Another one that had me stumped for a while was calling $this->fail() from within the setUp() method. I'm not sure about other browsers, but in Chrome (v5.0.375.70 Ubuntu 10.04 x64), calling this would actually result in the page crashing.
It seems that if you want to use this method, or any others for that matter (pass, fail, assertText, etc), you're going to have to set them to a class member, and check that value in each test.

drupalPost() won't set undeclared fields

drupalPost(), as wonderful as it is, requires the URL to point to the page in which the form is located, rather than the action page, because it will fetch the form page and check that the input fields exist before populating them and submitting them, rather just just simply send a POST request with the passed parameters.
This becomes a problem when the page in question is almost entirely driven by Javascript, which sends fields that are not necessarily hard-coded into the HTML, causing drupalPost() to fail when it can't find the input elements with those names.

Fatal Error: Call to undefined method assertLinkByHref

I don't care what it says in the documentation, unless I somehow got a dodgy download, $this->assertLinkByHref() isn't a native method; how about writing your own? This is my brief (and limited) implementation.

  1. /**
  2.  * SimpleTest Method Overwrite: assertLinkByHref
  3.  *
  4.  * For some reason, even though it is referenced in the API,
  5.  * assertLinkByHref is undefined; nor can I find it in the source. This
  6.  * is my implementation of what I believe it would have been.
  7.  * This is nowhere near thorough enough though, it does not support things
  8.  * inside the URL, such as query strings or fragments. Mainly because I
  9.  * don't know XPath.
  10.  *
  11.  * @access protected
  12.  * @param  string    $href
  13.  * @param  int       $index
  14.  * @param  string    $message
  15.  * @param  string    $group
  16.  * @return boolean
  17.  */
  18. protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') {
  19.   $xpath = '//a[@href="' . url($href) . '"]';
  20.   $links = $this->xpath($xpath);
  21.   // If no links were returned, it's probably because the page contains
  22.   // absolute URLs.
  23.   if (count($links) == 0) {
  24.     $xpath = '//a[@href="' . url($href, array('absolute' => true)) . '"]';
  25.     $links = $this->xpath($xpath);
  26.   }
  27.   $message = $message
  28.            ? $message
  29.            : t('Link with href "!href" found.', array('!href' => $href));
  30.   return $this->assert(isset($links[$index]), $message, $group);
  31. }

About The Author

Alexander's Profile Picture
0

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.

New job vacancies button

Categories

Search