2. What is Simpletest?
Contributed module available for Drupal 6 (requires
patch), Pressflow & Drupal 7
Enables 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’ve
finished working on it
Tests can be re-run in the future when code starts
interacting with your code
We can run tests continually when we commit new
code
Writing tests makes you think about your code
Testing 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 Testing
Simpletest create a complete Drupal installation
It runs tests on a virtual web browser acting like you would
by hand
Each test runs in a new site instance
No modules, users, nodes, content types exist unless you
specify.
6. Simpletest Example
preparation – step 1
Begin by creating a file called simpletest_example.test and put
it in the module directory.
Create a class that extends DrupalWebTestCase
Declare a user that we’ll use to interact with the browser
7. Preparation – step 2
We need to make information available to the
Simpletest testing interface – use getInfo
8. Preparation – step 3
The setUp function is used to customise the environment you will be working
in.
Enable modules by calling parent::setUp(‘MODULENAME’)
Create a user and set permissions using drupalCreateUser method
In this example we enable a module that defines a simple node and a
permission set to create and edit that node
9. A basic test
Key part of tests are the API functions and assertions.
Below we use “drupalPost” to post through values to the node
creation page
We then assert that a success message should be returned using
the “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 Simpletest
Simpletest offers very powerful functionality through
its DrupalWebTestCase, but there may be times when
you want to write a traditional, simple unit test.
Test only isolated pieces of functionality
Make sure to test a variety of conditions
Use “DrupalUnitTestCase”
12. What to test?
This is a good example of a function to test
It has no dependencies and is only a few lines of code
Pass in different values for $date_string and check the
returned Boolean value
13. Example test
Preparation – step 1
Create a .test file and add the path to your modules
.info file
Setup the test case in a similar way to the functional
tests but using “DrupalUnitTestCase”
14. Preparation – step 2
Just like our DrupalWebTestCase we need to
implement 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