Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Different Types Of Assertions in SimpleTest

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:03
    Different Types of Assertions in SimpleTest
  • 0:05
    with Joe Shindelar
  • 0:07
    Assertions make up the basis of all of our tests.
  • 0:11
    When a teacher grades a test, they usually have an answer sheet
  • 0:15
    that they can compare your answer to,
  • 0:16
    as way of quickly determining correct and incorrect answers.
  • 0:21
    Assertions are the part in our code where we ask a question,
  • 0:24
    and compare the resulting answer to our answer sheet,
  • 0:27
    to make sure the code passes the test.
  • 0:30
    So understanding how they work, and the various types of assertions

Different Types Of Assertions in SimpleTest

Loading...

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
Downloads: 
Log in or sign up to download companion files.