What are we testing?

Oct 25, 2008

After multiple iterations, the Intro to SimpleTest session has gotten pretty good at teaching people how to write a test, but one of the questions that were often asked during the testing sprint that followed the session was: what do I need to test? I submitted this presentation again for DrupalCon DC as there are still a lot of developers out there who want to get familiar with automated testing, and this time we need to be able to make a better job at responding to the question "what are we testing?"

When I presented this session for the last time at BADCamp, I added a section to talk about what it is that we're doing when writing a test, in which I subdivided a test into four components:

  • Context
  • User
  • Action
  • Outcome

The context is the circumstances in which the test is run. In Drupal, setting up the context is mostly taken care of by the SimpleTest module which creates a clean copy of Drupal. Enabling required modules, setting variables and anything you might do in the setUp() method is also setting up the context.

Creating a user might be considered part of the context, but I made it a separate component given its importance for browser-based testing which covers a large amount of the existing tests. The test user is a virtual person who executes actions.

Actions are actual code getting run. For browser-based testing, the action is triggered by a helper function like $this->clickLink() which simulates a user clicking on a link. When testing PHP functions, we call them directly in the test function.

After an action has been executed, we write assertions to check that the desired outcome is fulfilled. This is done using one of the many $this->assert*() functions provided by the DrupalWebTestCase class.

So what are we testing? The actions. However, we don't want to put print "it works!" inside of our code, so we check for the consequences of these actions by asserting the desired outcome.

Want to get involved and co-present? Let me know!