Writing SimpleTests for Commerce modules

Drupal Commerce has an automated testing suite based on the SimpleTest module in Drupal Core.

SimpleTest resources:

All tests should extend the class CommerceBaseTestCase which adds helper functions to DrupalWebTestCase. The Commerce base test class lets you quickly create products and set up different store environments for other tests.

Take a look at the CommerceBaseTestCase to see which functions are available when writing other Commerce tests. (Need a link once the code is committed. Could also use it's own API page eventually).

Best Practices for Test Development

Here are some best practices that came about while the base class was being developed.

  • Follow the Drupal Commerce Developer Guide
  • Ease your test development with:
  • Save user creation for the test unless it is used more than once (user creation leads to many page loads!)
  • Capitalize module names in the comments
  • Test the default state of Drupal Commerce before considering edge cases
    • Example: Provide test coverage and consideration to the default Product entity before considering custom product entities
  • Functional tests vs. Unit tests
    • Functional tests (UI tests) - These test the UI using form submissions and other UI interaction.
      • Test coverage should begin with functional tests since it tests the UI and the API simultaneously. If a test fails when there are only functional tests available, it is not always clear if the error resides in the UI or the API module. Because of this, test coverage should quickly expand to include unit tests.
    • Unit tests (API tests) - These test the API using API functions and database queries.
      • Note: Unit tests do not have to extend DrupalUnitTestCase. That class is only for tests that do not require the Drupal database. Unit tests (in gneral) can extend DrupalWebTestCase to have access to the Drupal database and then use API functions and databse queries to check if the functions are behaving as expected.
      • Drupal Commerce unit tests sould extend CommerceBaseTestCase just like anything else would.

Separation of Tests & Files

  • commerce_base.test - contains CommerceBaseTestCase class that all Commerce test classes should extend.
  • commerce_full.test - contains tests that are cross-system
  • [module].test - contains tests specific to code inside [module].module. These are unit tests (see description above).
  • [module]_ui.test - contains tests specific to code inside [module]_ui.module. These are usually functional tests (see description above).