• Save
Simpletest - A beginners guide
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Simpletest - A beginners guide

on

  • 1,350 views

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

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

Statistics

Views

Total Views
1,350
Views on SlideShare
1,322
Embed Views
28

Actions

Likes
3
Downloads
0
Comments
0

2 Embeds 28

http://wiki.wonga.com 24
http://wiki 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Simpletest - A beginners guide Presentation Transcript

  • 1. SimpletestTesting in Drupal - A beginners guide
  • 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. 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. 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. 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. 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. Preparation – step 2We need to make information available to theSimpletest testing interface – use getInfo
  • 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. 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. 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. 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. 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. 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”
  • 14. Preparation – step 2Just like our DrupalWebTestCase we need toimplement the getInfo method
  • 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. Readinghttp://drupal.org/simpletest-tutorialhttp://drupal.org/node/811254http://www.lullabot.com/articles/introduction-unit-testing