Module Development

Different Types Of Assertions in SimpleTest for Drupal 7

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Sprout Video

Assertions are like the answer sheet a teacher uses when grading a multiple choice test. When we write tests our primary objective is to check, or assert, that the state of some system, or the value of some variable, in the specified context matches our expectations. Like saying, "I expect that when I break this cookie in half I'll see chocolate pieces.". If you break the cookie and they are not there you might then decide that this particular cookie does not pass the chocolate chip cookie test. And flag it for review.

There are a lot of different types of assertions that we can make when using SimpleTest and this lesson attempts to demystify what each of the different types of assertions does. There is some really good documentation about the various types of assertions and some examples of using them in the Assertions documentation page on Drupal.org.

Not all of the assertions in SimpleTest are listed there though. For that, your best bet is to look at the API documentation for DrupalWebTestCase, and use the filter field at the top of the table to limit the list to only methods that start with the keyword "assert". This will give you a complete, and up-to-date list of all the assertions.

Basic assertion types reference:

Checking the value of something

  • assertTrue
  • assertFalse
  • assertNull
  • assertNotNull
  • assertEqual
  • assertNotEqual
  • assertIdentical
  • assertNotIdentical

Checking for the presence of something

  • assertPattern
  • assertNoPattern
  • assertRaw
  • assertNoRaw
  • assertText
  • assertNoText
  • assertTitle
  • assertNoTitle
  • assertUniqueText
  • assertNoUniqueText
  • assertLink
  • assertNoLink
  • assertResponse

Checking form elements

  • assertFieldById
  • assertNoFieldById
  • assertFieldByName
  • assertNoFieldByName
  • assertFieldChecked
  • assertNoFieldChecked
  • assertOptionSelected
  • assertNoOptionSelected

Additional resources

Drupal.org Assertion Examples / Documentation

Legacy Module Development