Assertions

Last modified: May 14, 2009 - 14:59

Assertions make up the core of any testing framework, and SimpleTest is no exception. In your test cases, you have the option of using any of a wide range of assertion types, each of which either directly or indirectly call either $this->_assert() in order to save the assertion to the database.

Here is a table of all API assertions available in the DrupalWebTestCase class. Note that all assertions accept the parameters $message and $group; these represent respectively the message to display upon passing/failing and the group to which this assertion belongs.

Method Description Example usage
<?php
$this
->assertTrue($result, $message = FALSE, $group = 'Other')
?>
Asserts that the variable $result resolves to TRUE.
<?php
$valid
= is_valid('foo');
$this->assertTrue($valid, t('Make sure "foo" is a valid variable.'));
?>
<?php
$this
->assertFalse($result, $message = '%s', $group = 'Other')
?>
Asserts that the variable $result resolves to FALSE.
<?php
$valid
= is_valid('bar');
$this->assertFalse($valid, t('Make sure "bar" is not a valid variable.'));
?>
<?php
$this
->assertNull($value, $message= '%s', $group= 'Other')
?>
Asserts that the variable $value resolves to NULL.
<?php
$result
= load_my_object(-1);
$this->assertNull($result, t('Make sure we get NULL when trying to load an invalid object.'));
?>
<?php
$this
->assertNotNull($value, $message= '%s', $group= 'Other')
?>
Asserts that the variable $value does NOT resolves to NULL.
<?php
$result
= load_my_object(1);
$this->assertNotNull($result, t('Make sure we get a result when trying to load a valid object.'));
?>
<?php
$this
->assertEqual($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is roughly equivalent (==) to $second.
<?php
menu_set_active_item
('node/3');
$arg = arg(1);
$this->assertEqual($arg, 3, t('Make sure arg(1) on the page node/3 resolves to 3.'));
?>
<?php
$this
->assertNotEqual($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is not roughly equivalent (!=) to $second.
<?php
menu_set_active_item
('node/3');
$arg = arg(0);
$this->assertNotEqual($arg, 3, t('Make sure arg(0) on the page node/3 does not resolve to 3.'));
?>
<?php
$this
->assertIdentical($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is absolutely identical (===) to $second.
<?php
$awesome
= get_awesome();
$this->assertIdentical($awesome, t('Drupal'), t('Make sure Drupal is awesome.'));
?>
<?php
$this
->assertNotIdentical($first, $second, $message= '%s', $group= 'Other')
?>
Asserts that the variable $first is not absolutely identical (!==) to $second.
<?php
$awesome
= get_awesome();
$this->assertNotIdentical($awesome, t('Drupal\'s evil twin'), t('Make sure Drupal\'s evil twin is NOT awesome.'));
?>
<?php
$this
->assertPattern($pattern, $message= '%s', $group= 'Other')
?>
Asserts that the raw html content of the current page matches the regular expression $pattern.
<?php
$this
->assertPattern('|[Dd]rupal|', t('Make sure Drupal/drupal is on current page.'));
?>
<?php
$this
->assertNoPattern($pattern, $message= '%s', $group= 'Other')
?>
Asserts that the raw html content of the current page does not match the regular expression $pattern.
<?php
$this
->assertNoPattern('|[Ee]vil|', t('Make sure Evil/evil is not on current page.'));
?>
<?php
$this
->assertRaw($raw, $message="%s", $group= 'Other')
?>
Asserts that the html $raw appears in the raw html content of the current page in the SimpleTest browser.
<?php
$this
->drupalGet('user/register');
$this->assertRaw('<a href="'. url('user/password') .'">', t('Make sure a link appears to "Request new password" on the user register page.'));
?>
<?php
$this
->assertNoRaw($raw, $message="%s", $group= 'Other')
?>
Asserts that the html $raw does NOT appear in the raw html content of the current page in the SimpleTest browser.
<?php
$this
->drupalGet('node/1');
$this->assertNoRaw('<a href="'. url('user/password') .'">', t('Make sure a link appears to "Request new password" does not appear on the page node/1.'));
?>
<?php
$this
->assertText($text, $message="%s", $group= 'Other')
?>
Asserts that the text in $text appears in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('user/register');
$this->assertText(t('Request new password'), t('Make sure the text "Request new password" appears on the user register page.'));
?>
<?php
$this
->assertNoText($text, $message="%s", $group= 'Other')
?>
Asserts that the text in $text does NOT appear in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('node/1');
$this->assertNoText(t('Request new password'), t('Make sure the text "Request new password" does NOT appear on the page node/1.'));
?>
<?php
$this
->assertTitle($title, $message, $group= 'Other')
?>
Asserts that the title given in $title is the title of the current page in the SimpleTest browser.
<?php
$this
->drupalGet('tracker');
$this->assertTitle(t('Recent posts'), t('Make sure the title on the tracker page is "Recent posts".'));
?>
<?php
$this
->assertUniqueText($text, $message='', $group= 'Other')
?>
Asserts that the text in $text appears exactly once in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('user/register');
$this->assertUniqueText(t('Request new password'), t('Make sure the text "Request new password" appears exactly once on the user register page.'));
?>
<?php
$this
->assertNoUniqueText($text, $message='', $group= 'Other')
?>
Asserts that the text in $text appears more than once in the content of the current page in the SimpleTest browser (html-stripped).
<?php
$this
->drupalGet('user/register');
$this->assertNoUniqueText(t('password'), t('Make sure the text "password" appears more than once on the user register page.'));
?>
<?php
$this
->assertResponse($code, $message= '')
?>
Asserts that the http response code for the current page in the simpletest browser matches $code.
<?php
$this
->drupalGet('admin');
$this->assertResponse(403, t('Make sure access is denied on the administration page.'));
?>
 
 

Drupal is a registered trademark of Dries Buytaert.