SimpletestTesting in Drupal - A beginners guide
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)
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.
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
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.
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
Preparation – step 2We need to make information available to theSimpletest testing interface – use getInfo
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
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
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”
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”
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
Example test Preparation – step 1Create a .test file and add the path to your modules.info fileSetup the test case in a similar way to the functionaltests but using “DrupalUnitTestCase”
Preparation – step 2Just like our DrupalWebTestCase we need toimplement the getInfo method
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