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: 

Understanding SimpleTest's Terminology and Methodology

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
    Understanding SimpleTest's Terminology and Methodology
  • 0:05
    with Joe Shindelar
  • 0:08
    Before we jump in and start writing some tests,
  • 0:11
    I want to spend a few minutes talking about some
  • 0:13
    of the different terminology we'll be using throughout this series
  • 0:16
    just so that we're all on the same page.
  • 0:20
    And then I want to take a really high-level look at the components
  • 0:25
    that make up an individual test case in SimpleTest.
  • 0:29
    In this lesson, we'll talk about the origins of some of SimpleTest
  • 0:33
    and defined terms like "test case" and "assertion."

Understanding SimpleTest's Terminology and Methodology

Loading...

SimpleTest is the tool that Drupal 7 core uses for discovering, and running all of it's tests. SimpleTest is also the name of the system that is used to write tests that can be run by the SimpleTest module. Understanding how SimpleTest locates the code that makes up our tests, and then executes that code, is an important part of understanding how to write tests. In this lesson we'll be discussing the pieces that make up the SimpleTest toolkit, some related terminology, and some best practices for dealing with SimpleTests in Drupal.

Methodology

Some best practices to keep in mind when working with SimpleTest and writing tests.

  • Tests should always be run in a separate environment from your production site.
  • Each test case sets up a completely new Drupal env, as such your test case needs to prepare the new environment as appropriate for your tests. Things like enabling any necessary modules, creating dummy content, and users etc. For this reason, it's best to have tests that require a similar setup be part of the same test case for better performance.
  • After a test case is run the environment is torn down and started fresh for the next test case. This ensures there is no contamination between test cases and that each test can count on knowing exactly what to expect from the environment it's running in.
  • SimpleTest is not always ideal for doing functional testing of your existing site. Drupalize.Me for example, we want to test that our About page is visible. But that's content, and doesn't live in code anywhere so creating it at install time can be tricky: We'll cover this scenario in more depth later.
  • Always test both sides of the problem. If your site only allows authenticated users to make comments, and you write a test to make sure authenticated users can comment you'll also want to make sure you write a test that verifies that anon. users can not comment. If you're checking that a value exists under specific conditions, also verify that it doesn't exist when those conditions are not met

Terminology

SimpleTest: The original name of the module used for testing in Drupal. Though it's now called just "Testing", the terms are used interchangeably. Based on the PHP SimpleTest library.

Test Case: A set of conditions under which the tester will determine whether the system being tested satisfies requirements and works correctly. A Test case defines the environment in which a test or set of tests will run. Including preconditions such as what modules are enabled, what content exists, etc. It is assumed that these same preconditions are true for all individual tests within a test case.

In Drupal, all test cases are classes that extend the DrupalWebTestCase, or the DrupalUnitTestCase class.

Test (Test method): Each individual test checks the functionality of one discreet thing. Example, if our test case is "User cancellation" there are many variations of cancellation that we need to test. Cancel the account and keep their content, cancel the account and delete their content, don't allow cancellation of the account with UID 1, etc. Each of these individual tests is checking one piece of functionality with the test case.

Assertion: The smallest unit of a test. An assertion is used to check wether or not a given condition is true in the current context.

Downloads: 
Log in or sign up to download companion files.