Simpletest - A beginners guide


Published on

A brief look at the simpletest module in Drupal and how it can be applied.

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Simpletest - A beginners guide

  1. 1. SimpletestTesting in Drupal - A beginners guide
  2. 2. What is Simpletest?Contributed module available for Drupal 6 (requirespatch), Pressflow & Drupal 7Enables developers to test small pieces of PHP code(Unit testing)Enables developers to test web-oriented functionality(Functional testing)
  3. 3. Why test code?Testing ensures your code works when you’vefinished working on itTests can be re-run in the future when code startsinteracting with your codeWe can run tests continually when we commit newcodeWriting tests makes you think about your codeTesting improves overall code quality.
  4. 4. Unit & Functional tests in Simpletest Simpletest provides two classes DrupalWebTestCase – For functional testing DrupalUnitTestCase - For unit testing Both contain methods that save us time when writing tests We’ll look at the two cases in more detail in this presentation
  5. 5. Functional TestingSimpletest create a complete Drupal installationIt runs tests on a virtual web browser acting like you wouldby handEach test runs in a new site instanceNo modules, users, nodes, content types exist unless youspecify.
  6. 6. Simpletest Example preparation – step 1Begin by creating a file called simpletest_example.test and putit in the module directory.Create a class that extends DrupalWebTestCaseDeclare a user that we’ll use to interact with the browser
  7. 7. Preparation – step 2We need to make information available to theSimpletest testing interface – use getInfo
  8. 8. Preparation – step 3The setUp function is used to customise the environment you will be workingin.Enable modules by calling parent::setUp(‘MODULENAME’)Create a user and set permissions using drupalCreateUser methodIn this example we enable a module that defines a simple node and apermission set to create and edit that node
  9. 9. A basic testKey part of tests are the API functions and assertions.Below we use “drupalPost” to post through values to the nodecreation pageWe then assert that a success message should be returned usingthe “assertText” assertion
  10. 10. A more detailed test Create a node using DrupalCreateNode Print out any debug information using “verbose” method Navigate to the node edit page using “drupalGet” Make sure we can access the page ok using “assertResponse” Check the node title is on the page using “assertText”
  11. 11. Unit Testing in SimpletestSimpletest offers very powerful functionality throughits DrupalWebTestCase, but there may be times whenyou want to write a traditional, simple unit test.Test only isolated pieces of functionalityMake sure to test a variety of conditionsUse “DrupalUnitTestCase”
  12. 12. What to test?This is a good example of a function to testIt has no dependencies and is only a few lines of codePass in different values for $date_string and check thereturned Boolean value
  13. 13. Example test Preparation – step 1Create a .test file and add the path to your fileSetup the test case in a similar way to the functionaltests but using “DrupalUnitTestCase”
  14. 14. Preparation – step 2Just like our DrupalWebTestCase we need toimplement the getInfo method
  15. 15. And finally a test… Call the function with various different values Create a message string to be displayed in case of failure Make basic assertions on the returned value These tests should be quick and simple to write
  16. 16. Reading