an introduction to unit testing in drupal · we're an interactive strategy, design, and...

22
We're an interactive strategy , design , and development company. We create delightful experiences using Drupal and open source technologies. Menu Home Blog Articles An Introduction to Unit Testing in Drupal by Robert Douglass (/who-we-are/robert-douglass) on November 26, 2007 // Short URL (http://lbt.me/Wzt) An Introduction to Unit Testing in Drupal 15 Comments (/blog/articles/introduction-unit-testing-drupal#comments) // 0 Tweets (https://twitter.com/#!/search/http%3A%2F%2Fwww.lullabot.com%2Fblog%2Farticles%2Fintro unit-testing-drupal/#) // Share This article applies to Drupal 5.3 (http://drupal.org/drupal-5.3) , the Simpletest module version 5.x-1.0 (http://drupal.org/node/176392) , and the Simpletest project (http://simpletest.org/) from Sourceforge.net, version simpletest_1.0.1beta2 (http://sourceforge.net/project/downloading.php? groupname=simpletest&filename=simpletest_1.0.1beta2.tar.gz&use_mirror=internap) . What is unit testing Unit testing (http://en.wikipedia.org/wiki/Unit_testing) is the art and practice of taking a small portion of code, a unit, and subjecting it to programmatic tests to prove its correctness. The smallest units in PHP code are usually functions, and the unit tests will run the functions with sets of controlled parameters, and then make assertions about the return values of the function, or about the state of the application before and after the function has run. For example, if the function is

Upload: others

Post on 05-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Were an interactive strategy design and development company We create delightfulexperiences using Drupal and open source technologies

Menu

Home Blog Articles An Introduction to Unit Testing in Drupal

by Robert Douglass (who-we-arerobert-douglass) on November 26 2007 Short URL

(httplbtmeWzt)

An Introduction to Unit Testing in Drupal15 Comments (blogarticlesintroduction-unit-testing-drupalcomments)

0 Tweets

(httpstwittercomsearchhttp3A2F2Fwwwlullabotcom2Fblog2Farticles2Fintroduction-

unit-testing-drupal)

Share

This article applies to Drupal 53 (httpdrupalorgdrupal-53) the Simpletest module

version 5x-10 (httpdrupalorgnode176392) and the Simpletest project

(httpsimpletestorg) from Sourceforgenet version simpletest_101beta2

(httpsourceforgenetprojectdownloadingphp

groupname=simpletestampfilename=simpletest_101beta2targzampuse_mirror=internap)

What is unit testing

Unit testing (httpenwikipediaorgwikiUnit_testing) is the art and practice of taking

a small portion of code a unit and subjecting it to programmatic tests to prove its

correctness The smallest units in PHP code are usually functions and the unit

tests will run the functions with sets of controlled parameters and then make

assertions about the return values of the function or about the state of the

application before and after the function has run For example if the function is

designed to validate email addresses the unit tests would pass in known valid email

addresses and assert that they validate correctly The tests would also pass in a

number of invalid email addresses as well and assert that they do not validate

Why is unit testing useful

Writing unit tests helps produce higher quality code on many levels

The availability of tests helps detect the introduction of bugs whenever the

programmer adds new features or refactors the code This is called regression

testing

Tests also serve as a source of documentation about what code is really

expected to do

The act of writing the tests challenges the programmer to consider possible

edge cases and their consequences

Last but not least the act of writing tests encourages the programmer to write

code in small chunks that can be tested independently

The last point is worth diving into If we are writing code that needs to do three

manipulations to an incoming string the temptation is to write a function where

the string comes in as an argument the three manipulations are executed and the

resulting string is returned When this function isnt working right it is hard to

determine which of the three manipulations are failing Your test case can only

send a string in and compare the result with the expected result How can you know

which of the manipulations isnt working correctly Breaking the function into

smaller pieces one for each manipulation results in code that is easier to test Test

cases can then be written for each manipulation better isolating the source of

failure

Unit tests can be often be scripted to run automatically making them an ideal part

of your development build and deployment process You can run tests before

committing new code to the repository You can run the tests after adding new

modules and thus ensure that previous functionality hasnt been compromised

You can run tests after deploying changes from a development environment to a

staging environment thus cutting down on the dependency of trial and error to

detect problems

What testing tools does Drupal offer

The principal tool for unit testing in drupal is the Simpletest module

(httpdrupalorgprojectsimpletest) This module is a Drupal extension to the

Simpletest project (httpsimpletestorg) for general PHP unit testing The Drupal

module adds a wealth of tools and convenience for Drupal specific unit testing For

example it has the ability to create new users and nodes set configuration

variables and submit Drupal forms

As an addition to the Simpletest module there is the Simpletest automation project

(httpdrupalorgprojectsimpletestauto) which provides a system for running test

suites automatically and reporting the results The Simpletest automation is also

capable of applying patches to Drupal code and is thus an essential tool for vetting

patches in the issue queue

The similarly named but very different Simpletest automator

(httpdrupalorgprojectsimpletest_automator) extends the convenience of writing

tests even further by allowing you to click through your site as it records your

actions as a macro This macro can then be used as the basis for a unit test that you

can run automatically at a later time

How to set up the Simpletest module

To begin running unit tests on your Drupal installation download the latest release

of the Simpletest module

(httpwwwlullabotcom3Chttp3Adrupalorgprojectsimpletest3E) and install it as

you would any other module Please note that you should not install this module on a

production site It is only designed for development and staging purposes and

running the tests will alter the state of your database Running the simpletest unit

tests on a production site could lead to lost data or unpredictable behavior

The Drupal module depends on the Simpletest library which you can download

from Sourceforgenet The latest release as of this writing is simpletest_101beta2

(httpsourceforgenetprojectdownloadingphp

groupname=simpletestampfilename=simpletest_101beta2targzampuse_mirror=internap) The

download from Sourceforge comes as a tarball which you need to extract into the

simpletest module directory The resulting directory structure looks like this

That concludes the installation of the Simpletest module and you are now ready to

run the existing unit tests

How to run the included tests

The existing unit tests can be found under AdministershygtSite buildingshy

gtSimpletest unit testing (adminbuildsimpletest) This page is a listing

of all the test suites that are installed The bulk of them come from the Simpletest

module itself and are used to test Drupal core functionality Since it is possible for

any module to provide test suites it is entirely possible that some of the contributed

modules you have installed will also have test suites Some modules that include

test cases are the coder (httpdrupalorgprojectcoder) organic groups

(httpdrupalorgprojectog) and timeline (httpdrupalorgprojecttimeline) modules

The Simpletest unit testing page allows you to select all of the tests in any group or

if you expand the Tests fieldset in any group the single tests individually Select the

Run selected tests option at the bottom and click Begin and Simpletest will do its

work Depending on how many tests youve chosen the running time may be

anywhere from a couple of seconds to minutes Here I am about to run all of the

tests in the Node tests group

Here is the output generated by running all of the tests in the Node tests group

What to do if you get errors

The beauty of having unit tests available is that it makes error reporting much

easier If you run the test suites for Drupal core or a contributed module and get

Fails you have a great opportunity to use the Drupalorg issue queue

(httpdrupalorgprojectissues) to file a bug report (httpdrupalorgnodeaddproject-

issue) Paste the output from Simpletest into the issue and the module maintainer

will know exactly what it is that went wrong Be sure to include the ordinary

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 2: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

designed to validate email addresses the unit tests would pass in known valid email

addresses and assert that they validate correctly The tests would also pass in a

number of invalid email addresses as well and assert that they do not validate

Why is unit testing useful

Writing unit tests helps produce higher quality code on many levels

The availability of tests helps detect the introduction of bugs whenever the

programmer adds new features or refactors the code This is called regression

testing

Tests also serve as a source of documentation about what code is really

expected to do

The act of writing the tests challenges the programmer to consider possible

edge cases and their consequences

Last but not least the act of writing tests encourages the programmer to write

code in small chunks that can be tested independently

The last point is worth diving into If we are writing code that needs to do three

manipulations to an incoming string the temptation is to write a function where

the string comes in as an argument the three manipulations are executed and the

resulting string is returned When this function isnt working right it is hard to

determine which of the three manipulations are failing Your test case can only

send a string in and compare the result with the expected result How can you know

which of the manipulations isnt working correctly Breaking the function into

smaller pieces one for each manipulation results in code that is easier to test Test

cases can then be written for each manipulation better isolating the source of

failure

Unit tests can be often be scripted to run automatically making them an ideal part

of your development build and deployment process You can run tests before

committing new code to the repository You can run the tests after adding new

modules and thus ensure that previous functionality hasnt been compromised

You can run tests after deploying changes from a development environment to a

staging environment thus cutting down on the dependency of trial and error to

detect problems

What testing tools does Drupal offer

The principal tool for unit testing in drupal is the Simpletest module

(httpdrupalorgprojectsimpletest) This module is a Drupal extension to the

Simpletest project (httpsimpletestorg) for general PHP unit testing The Drupal

module adds a wealth of tools and convenience for Drupal specific unit testing For

example it has the ability to create new users and nodes set configuration

variables and submit Drupal forms

As an addition to the Simpletest module there is the Simpletest automation project

(httpdrupalorgprojectsimpletestauto) which provides a system for running test

suites automatically and reporting the results The Simpletest automation is also

capable of applying patches to Drupal code and is thus an essential tool for vetting

patches in the issue queue

The similarly named but very different Simpletest automator

(httpdrupalorgprojectsimpletest_automator) extends the convenience of writing

tests even further by allowing you to click through your site as it records your

actions as a macro This macro can then be used as the basis for a unit test that you

can run automatically at a later time

How to set up the Simpletest module

To begin running unit tests on your Drupal installation download the latest release

of the Simpletest module

(httpwwwlullabotcom3Chttp3Adrupalorgprojectsimpletest3E) and install it as

you would any other module Please note that you should not install this module on a

production site It is only designed for development and staging purposes and

running the tests will alter the state of your database Running the simpletest unit

tests on a production site could lead to lost data or unpredictable behavior

The Drupal module depends on the Simpletest library which you can download

from Sourceforgenet The latest release as of this writing is simpletest_101beta2

(httpsourceforgenetprojectdownloadingphp

groupname=simpletestampfilename=simpletest_101beta2targzampuse_mirror=internap) The

download from Sourceforge comes as a tarball which you need to extract into the

simpletest module directory The resulting directory structure looks like this

That concludes the installation of the Simpletest module and you are now ready to

run the existing unit tests

How to run the included tests

The existing unit tests can be found under AdministershygtSite buildingshy

gtSimpletest unit testing (adminbuildsimpletest) This page is a listing

of all the test suites that are installed The bulk of them come from the Simpletest

module itself and are used to test Drupal core functionality Since it is possible for

any module to provide test suites it is entirely possible that some of the contributed

modules you have installed will also have test suites Some modules that include

test cases are the coder (httpdrupalorgprojectcoder) organic groups

(httpdrupalorgprojectog) and timeline (httpdrupalorgprojecttimeline) modules

The Simpletest unit testing page allows you to select all of the tests in any group or

if you expand the Tests fieldset in any group the single tests individually Select the

Run selected tests option at the bottom and click Begin and Simpletest will do its

work Depending on how many tests youve chosen the running time may be

anywhere from a couple of seconds to minutes Here I am about to run all of the

tests in the Node tests group

Here is the output generated by running all of the tests in the Node tests group

What to do if you get errors

The beauty of having unit tests available is that it makes error reporting much

easier If you run the test suites for Drupal core or a contributed module and get

Fails you have a great opportunity to use the Drupalorg issue queue

(httpdrupalorgprojectissues) to file a bug report (httpdrupalorgnodeaddproject-

issue) Paste the output from Simpletest into the issue and the module maintainer

will know exactly what it is that went wrong Be sure to include the ordinary

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 3: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

of your development build and deployment process You can run tests before

committing new code to the repository You can run the tests after adding new

modules and thus ensure that previous functionality hasnt been compromised

You can run tests after deploying changes from a development environment to a

staging environment thus cutting down on the dependency of trial and error to

detect problems

What testing tools does Drupal offer

The principal tool for unit testing in drupal is the Simpletest module

(httpdrupalorgprojectsimpletest) This module is a Drupal extension to the

Simpletest project (httpsimpletestorg) for general PHP unit testing The Drupal

module adds a wealth of tools and convenience for Drupal specific unit testing For

example it has the ability to create new users and nodes set configuration

variables and submit Drupal forms

As an addition to the Simpletest module there is the Simpletest automation project

(httpdrupalorgprojectsimpletestauto) which provides a system for running test

suites automatically and reporting the results The Simpletest automation is also

capable of applying patches to Drupal code and is thus an essential tool for vetting

patches in the issue queue

The similarly named but very different Simpletest automator

(httpdrupalorgprojectsimpletest_automator) extends the convenience of writing

tests even further by allowing you to click through your site as it records your

actions as a macro This macro can then be used as the basis for a unit test that you

can run automatically at a later time

How to set up the Simpletest module

To begin running unit tests on your Drupal installation download the latest release

of the Simpletest module

(httpwwwlullabotcom3Chttp3Adrupalorgprojectsimpletest3E) and install it as

you would any other module Please note that you should not install this module on a

production site It is only designed for development and staging purposes and

running the tests will alter the state of your database Running the simpletest unit

tests on a production site could lead to lost data or unpredictable behavior

The Drupal module depends on the Simpletest library which you can download

from Sourceforgenet The latest release as of this writing is simpletest_101beta2

(httpsourceforgenetprojectdownloadingphp

groupname=simpletestampfilename=simpletest_101beta2targzampuse_mirror=internap) The

download from Sourceforge comes as a tarball which you need to extract into the

simpletest module directory The resulting directory structure looks like this

That concludes the installation of the Simpletest module and you are now ready to

run the existing unit tests

How to run the included tests

The existing unit tests can be found under AdministershygtSite buildingshy

gtSimpletest unit testing (adminbuildsimpletest) This page is a listing

of all the test suites that are installed The bulk of them come from the Simpletest

module itself and are used to test Drupal core functionality Since it is possible for

any module to provide test suites it is entirely possible that some of the contributed

modules you have installed will also have test suites Some modules that include

test cases are the coder (httpdrupalorgprojectcoder) organic groups

(httpdrupalorgprojectog) and timeline (httpdrupalorgprojecttimeline) modules

The Simpletest unit testing page allows you to select all of the tests in any group or

if you expand the Tests fieldset in any group the single tests individually Select the

Run selected tests option at the bottom and click Begin and Simpletest will do its

work Depending on how many tests youve chosen the running time may be

anywhere from a couple of seconds to minutes Here I am about to run all of the

tests in the Node tests group

Here is the output generated by running all of the tests in the Node tests group

What to do if you get errors

The beauty of having unit tests available is that it makes error reporting much

easier If you run the test suites for Drupal core or a contributed module and get

Fails you have a great opportunity to use the Drupalorg issue queue

(httpdrupalorgprojectissues) to file a bug report (httpdrupalorgnodeaddproject-

issue) Paste the output from Simpletest into the issue and the module maintainer

will know exactly what it is that went wrong Be sure to include the ordinary

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 4: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

of the Simpletest module

(httpwwwlullabotcom3Chttp3Adrupalorgprojectsimpletest3E) and install it as

you would any other module Please note that you should not install this module on a

production site It is only designed for development and staging purposes and

running the tests will alter the state of your database Running the simpletest unit

tests on a production site could lead to lost data or unpredictable behavior

The Drupal module depends on the Simpletest library which you can download

from Sourceforgenet The latest release as of this writing is simpletest_101beta2

(httpsourceforgenetprojectdownloadingphp

groupname=simpletestampfilename=simpletest_101beta2targzampuse_mirror=internap) The

download from Sourceforge comes as a tarball which you need to extract into the

simpletest module directory The resulting directory structure looks like this

That concludes the installation of the Simpletest module and you are now ready to

run the existing unit tests

How to run the included tests

The existing unit tests can be found under AdministershygtSite buildingshy

gtSimpletest unit testing (adminbuildsimpletest) This page is a listing

of all the test suites that are installed The bulk of them come from the Simpletest

module itself and are used to test Drupal core functionality Since it is possible for

any module to provide test suites it is entirely possible that some of the contributed

modules you have installed will also have test suites Some modules that include

test cases are the coder (httpdrupalorgprojectcoder) organic groups

(httpdrupalorgprojectog) and timeline (httpdrupalorgprojecttimeline) modules

The Simpletest unit testing page allows you to select all of the tests in any group or

if you expand the Tests fieldset in any group the single tests individually Select the

Run selected tests option at the bottom and click Begin and Simpletest will do its

work Depending on how many tests youve chosen the running time may be

anywhere from a couple of seconds to minutes Here I am about to run all of the

tests in the Node tests group

Here is the output generated by running all of the tests in the Node tests group

What to do if you get errors

The beauty of having unit tests available is that it makes error reporting much

easier If you run the test suites for Drupal core or a contributed module and get

Fails you have a great opportunity to use the Drupalorg issue queue

(httpdrupalorgprojectissues) to file a bug report (httpdrupalorgnodeaddproject-

issue) Paste the output from Simpletest into the issue and the module maintainer

will know exactly what it is that went wrong Be sure to include the ordinary

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 5: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

The existing unit tests can be found under AdministershygtSite buildingshy

gtSimpletest unit testing (adminbuildsimpletest) This page is a listing

of all the test suites that are installed The bulk of them come from the Simpletest

module itself and are used to test Drupal core functionality Since it is possible for

any module to provide test suites it is entirely possible that some of the contributed

modules you have installed will also have test suites Some modules that include

test cases are the coder (httpdrupalorgprojectcoder) organic groups

(httpdrupalorgprojectog) and timeline (httpdrupalorgprojecttimeline) modules

The Simpletest unit testing page allows you to select all of the tests in any group or

if you expand the Tests fieldset in any group the single tests individually Select the

Run selected tests option at the bottom and click Begin and Simpletest will do its

work Depending on how many tests youve chosen the running time may be

anywhere from a couple of seconds to minutes Here I am about to run all of the

tests in the Node tests group

Here is the output generated by running all of the tests in the Node tests group

What to do if you get errors

The beauty of having unit tests available is that it makes error reporting much

easier If you run the test suites for Drupal core or a contributed module and get

Fails you have a great opportunity to use the Drupalorg issue queue

(httpdrupalorgprojectissues) to file a bug report (httpdrupalorgnodeaddproject-

issue) Paste the output from Simpletest into the issue and the module maintainer

will know exactly what it is that went wrong Be sure to include the ordinary

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 6: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

What to do if you get errors

The beauty of having unit tests available is that it makes error reporting much

easier If you run the test suites for Drupal core or a contributed module and get

Fails you have a great opportunity to use the Drupalorg issue queue

(httpdrupalorgprojectissues) to file a bug report (httpdrupalorgnodeaddproject-

issue) Paste the output from Simpletest into the issue and the module maintainer

will know exactly what it is that went wrong Be sure to include the ordinary

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 7: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

information about your Drupal installation including what release you are running

and what modules you have installed

How to write a basic unit test

Unit testing is a great productivity enhancer for programmers and I highly

recommend using it as a core technique whenever you write code Your code will

come together quicker and will be higher quality for the effort Adding unit test

support to your Drupal module is easy Simpletest defines hook_simpletest

which your module implements

ltphp

Implementation of hook_simpletest()

function hook_simpletest()

$module_name = mymodule Change this to your module name

$dir = drupal_get_path(module $module_name) tests

$tests = file_scan_directory($dir test$)

return array_keys($tests)

gt

The hook returns a list of files that contain test cases The convention is to make a

tests directory in your module and put the test cases in there If you follow the

convention then you only need to copy the code above and change the function

name and update the $module_name variable to be the name of your module

Now you can start to create test cases These files should have the ending test

and reside in the tests directory in your module The stub code for a test case

looks like this

ltphp

class PageViewTest extends DrupalTestCase

function

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 8: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

get_info()

return array(

name =gt t(Page node creation)

desc =gt t(Create a page node and verify its consistency

in the database)

group =gt t(Node Tests)

)

function

testSomething()

function

testSomethingElse()

gt

A test case is a class that extends the DrupalTestCase class You must

implement the get_info() method which returns a keyed array with name

desc and group strings which are used for display on the Simpletest unit

test page Beyond that any function that starts with test will be executed whenever

the test case is run

The Simpletest framework and the DrupalTestCase offer a lot of handy tools for

executing your tests Well explore a couple of these by looking at some non-trivial

examples taken from the test cases found in existing modules The first example

comes from the user_validationtest suite from the Simpletest module

ltphp

username validation

function testMinLengthName()

$name =

$result = user_validate_name($name)

$thisshygtassertNotNull($result Excessively short username)

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 9: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

gt

This test creates a $name which is unacceptable as a user name because it is an

empty string It passes the $name into Drupals user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) function and then uses the

assertNotNull() method to check whether the test passes or fails The

assertNotNull() method must be called using the $this object which is an

implicit self reference in any PHP class object The assertNotNull() method

will check whether $result is NULL If it is NULL the test fails If it is not NULL

it passes This makes sense because the function is asserting that the object is not

NULL which is another way of saying I expect this to have a value (the error

message saying that the validation fails) please fail if not NULL The

assertNotNull() method also takes a message parameter This message gets

passed on to the Simpletest framework and is displayed on the test results page

Here is the outcome of the test listed above

As you can see the test passed which means user_validate_name() did the

expected and returned a non NULL value when the $name variable was

unacceptable If you refer to the API documentation for user_validate_name

(httpapidrupalorgapifunctionuser_validate_name5) you will see that it returns

string values whenever validation fails

The next example comes from the tests for the finduser module

(httpdrupalorgprojectfinduser) The goal of the module is to search for users In

order to test this the site needs to have users and the test suite has to know about

them otherwise it wouldnt know what to search for or what to expect Fortunately

the DrupalTestCase has many Drupal-specific convenience methods that let us

handle this situation Heres the code

ltphp

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 10: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

function testSearchByEmail()

Temporarily set the finduser_email variable to TRUE It will

return

to whatever its normal state is when the tearDown() method

is called

$thisshygtdrupalVariableSet(finduser_email TRUE)

Prepare a user that we can search for

$web_user = $thisshygtdrupalCreateUserRolePerm()

Search by email

$results = finduser_do_search(email $web_usershygtmail)

Assert that only one result is found

$thisshygtassertEqual(count($results) 1)

Assert that it is the user we created

$thisshygtassertEqual($web_usershygtuid $results[0]shygtid)

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

The first convenience method we see here is drupalVariableSet() Once

again this is a method of the test case object itself and thus must be called from

the $this object What the method does is inspect the current value for a Drupal

variable (finduser_email in this case) take note of it and then set it to a new

value (TRUE) After the tests have run the variable will be set to its original value

all in the background without you needing to worry about it In this way you can

temporarily change the configuration parameters of your site and not have to

worry about cleaning up - the Simpletest module handles it for you

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 11: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

The next convenience method is drupalCreateUserRolePerm() This

method takes an array of permissions and uses them to create a new user account

The user account will have a generated name and email address and will have a

special user role with the permissions in the array You can then use this user

account to test the functionality of your site Just like with

drupalVariableSet() the user and the roles will be deleted when the tests

are finished running so you neednt worry about filling up your database with extra

test users nor do you need to go and create these users manually

Once the setup steps are all done the test goes on to invoke the actual function that

is being tested the finduser_do_search() function The function is told to

search for an email that is equal to the $web_users email To determine whether

the function behaves as expected $thisshygtassertEqual() is called

assertEqual() takes the first two parameters and asserts that they are equal

The results of assertEqual() will be saved for reporting on the Simpletest unit

tests page The test continues however and $thisshygtassertEqual() is called

again this time asserting that the $web_users name is equal to the username

that was found by doing the search

It is important to test both success cases and failure cases equally The first two

assertions tested that a user was found when expected The third assertion in the

code above asserts that no user is found when searching for a bogus non-existent

user

ltphp

Search for a bogus nonshyexistent user

$bogus_results = finduser_do_search(email xxx $web_usershy

gtmail)

Assert that zero results are found

$thisshygtassertEqual(count($bogus_results) 0)

gt

There are many more ways to do assertions as well as many more Drupal helper

functions The Simpletest handbook pages (httpdrupalorgsimpletest) on

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 12: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Drupalorg are a good reference for the various tools available

Functionality testing - a special case

The examples we have looked at so far are true unit tests because they inspect one

function at a time throwing various arguments at it and asserting that the results

are correct The Simpletest framework supports an entirely different method of

testing that simulates actions done in a web browser and inspects the output that is

sent to the browser making assertions based on the output Here is an example

that creates a new user logs into the site using the new user submits the form to

create a new Page node asserts that the output that gets sent to the browser has the

Your post has been created message and asserts that the node exists in the

database Keep in mind how many clicks it would take you to do all of those steps

manually

ltphp

function testPageCreation()

Prepare settings

$thisshygtdrupalVariableSet(node_options_page array(status

promote))

Prepare a user to do the stuff

$web_user = $thisshygtdrupalCreateUserRolePerm(array(edit own

page content create page content))

$thisshygtdrupalLoginUser($web_user)

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 13: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

The first new method in this code is $thisshygtdrupalLoginUser() Use this

function to log in using any $user object such as those returned by

user_load() This code logs in using the $web_user which was created with

the edit own page content and create page content permissions

This code submits a form using Http POST Note that $edit contains only the

information that the user would be required to enter on the web form The title and

body fields were generated using the $thisshygtrandomName() method Submit

is the name of the button that gets clicked in order to submit the form The $this

object stores the HTML output so that you can make assertions against it later

ltphp

$edit = array()

$edit[title] = SimpleTest test node $thisshy

gtrandomName(10)

$edit[body] = SimpleTest test body $thisshy

gtrandomName(32) $thisshygtrandomName(32)

$thisshygtdrupalPostRequest(nodeaddpage $edit Submit)

gt

This code takes the HTML output that is returned after submitting the form and

looks for a specific string within it The method asserts that the string exists

ltphp

$thisshygtassertWantedRaw(t(Your post has been created array

(post =gt Page)) Page created)

gt

Finally this test asserts that the node is found in the database as well

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 14: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Robert Douglass (who-we-arerobert-douglass)

ltphp

$node = node_load(array(title =gt $edit[title]))

$thisshygtassertNotNull($node Node found in database s)

gt

More information

For more information on the Simpletest module (httpdrupalorgprojectsimpletest)

please refer to the handbook pages on Drupalorg (httpdrupalorgsimpletest) The

source code for the drupal_unit_testsphp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_unit_testsphp

revision=17ampview=markup) and drupal_test_casephp

(httpcvsdrupalorgviewvcpydrupalcontributionsmodulessimpletestdrupal_test_casephp

revision=134ampview=markup) is also informative as the reference for the available

assert and helper methods The Simpletest API documentation

(httpsimpletestorgapi) is useful for learning about the underlying framework

Any of the methods available in the Simpletest base classes are also available to

your test cases through class inheritance The center of testing activity for

Drupalorg is testingdrupalorg (httptestingdrupalorg) There are two groups on

groupsdrupalorg that deal with testing the Unit testing group

(httpgroupsdrupalorgunit-testing) and the Quality assurance group

(httpgroupsdrupalorgquality-assurance)

TOPICS

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 15: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Drupal Site Building (taxonomyterm834)

TAGSdevelopment (taxonomyterm184) drupal (taxonomyterm212) unit testing

(taxonomyterm743) testing (taxonomyterm711) simpletest (taxonomyterm649)

quality assurance (taxonomyterm599) drupal 53 (taxonomyterm1007) what is unit

testing (taxonomyterm1515) simpletest module setup (taxonomyterm1406)

CommentsAdd Your Comment

gman (httpwwwtech-wanderingscom) Nov 26 2007

Andre Molnar (httpbecirclecom) Nov 26 2007

Excellent Post

I have needing to help push my developer group to embrace unit testing I

have been meaning to look at the Simpletest module for a while and now

with this article I can bring it up again in our next developers meeting

Reply (commentreply2962905)

function testMinLengthName()

Nice write up

You may want to touch up the testMinLengthName() example to match the

description you give - or - the other way around You say that

testMinLengthName() asserts Null but the example code is actually

asserting NOT Null

Just to add to the article this is actually a good example of things testers

should keep in mind When writing tests make sure that the test is testing

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 16: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Bevan (httpdrupalgeeknz) Nov 26 2007

robert (who-we-arerobert-douglass) Nov 26 2007

Rolf Nov 27 2007

what you think it is testing Get used to thinking in double negatives and

doing other logical jumping jacks A PASS is sometimes a FALSE and a FAIL

is sometimes a TRUE

If you can answer the question Didnt you not never go nowhere when you

were a kid AND explain why your answer is TRUE or FALSE - you too can

write a unit test

andre

Reply (commentreply2962906)

Thanks Robert

Thanks Robert This is a great article

Reply (commentreply2962907)

Thanks good catch

All updated now

Reply (commentreply2962908)

JavascriptjQuery unit test

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 17: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Anonymous Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Hi

Great article From my point of view unit test is fundamental in modern

SW development and Simple test it perfect for that purpose Please bear in

mind that programmers have always made unit tests However most

programmers keep these unit tests in their head instead of a framework

such as Simple test

During the last couple of months I have done some programming in

Javascript for Drupal 5x and since jQuery is included in Drupal 5x it has

been fun However I havnt been able to find a unit test frame work for

JavascriptjQuery in Drupal Can Simple test be extended to activate

javascript unit tests Or can you recommend at third party tool for that

purpose

Cheers

Rolf

Reply (commentreply2962909)

How many unit tests does the Drupal core have

and are all checkins required to show that they dont break tests

IOW now that Drupal has a testing module is it being used

Reply (commentreply2962910)

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 18: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Island Usurper (httpwwwubercartorg) Nov 27 2007

robert (who-we-arerobert-douglass) Nov 27 2007

Drupal core coverage is less thancomplete

We need people who are evil minded testers to come forward and write

more tests for Drupal core The proper place for these tests is as a part

of the Simpletest module itself

There is an automated test server that is being tested on the Drupalorg

infrastructure It takes patches from the issue queue runs tests on

them and reports back to the issue queue with the results

These initiatives need a lot of support and energy before they see the

light of day so if youre interested jump on board and lend a hand

Reply (commentreply2962911)

Not quite done

Theres a line that says did the expected and returned a NULL value

which is wrong for an assertNotNull()

Reply (commentreply2962912)

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 19: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Gabriel Nov 28 2007

David Luhman (httpluhmanorg) Nov 30 2007

Also corrected

Thank you

Reply (commentreply2962913)

Here are some links

Check out Selenium (

httpwwwopenqaorgselenium (httpwwwopenqaorgselenium))

and JsUnit (httpwwwjsunitnet (httpwwwjsunitnet)) Selenium is

probably what you look for

Reply (commentreply2962914)

Interested in helping with unit testsfor Drupal core

Hey Robert - Enjoyed the article and your contribs on the podcasts

Id be interested to help with the Drupal core unit testing Is there a

URL or group you can point us to to pitch in

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 20: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

robert (who-we-arerobert-douglass) Nov 30 2007

chx Jan 08 2008

David

Reply (commentreply2962915)

The project page for the module

The project page for the module is where you can submit patches

(including new tests)

httpdrupalorgprojectsimpletest (httpdrupalorgprojectsimpletest)

The Unit testing group

httpgroupsdrupalorgunit-testing (httpgroupsdrupalorgunit-

testing)

And the Quality assurance group

httpgroupsdrupalorgquality-assurance

(httpgroupsdrupalorgquality-assurance)

See the groups to talk to people about specific tests or testing

techniques

Reply (commentreply2962916)

core tests differ a little

for now they go into the tests directory of the the simpletest module itself

Reply (commentreply2962917)

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 21: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Mike Cantelon (httpmikecanteloncom) Jul 06 2008

Anonymous Mar 13 2009

Add Your Comment

Your name

E-mail

The content of this field is kept private and will not be shown publicly

Homepage

Subject

hook_simpletest no longer used

Great article You might want to add a note that as per

httpdrupalorgnode208144 (httpdrupalorgnode208144) you no

longer have to implement hook_simpletest in your module to provide unit

tests

Reply (commentreply2962918)

Link Simpletest module in

Link Simpletest module in How to set up the Simpletest module is

broken

Reply (commentreply2962919)

Subscribe to our mailing list

Comment

PREVIEWPREVIEW

Page 22: An Introduction to Unit Testing in Drupal · We're an interactive strategy, design, and development company. We create delightful experiences using Drupal and open source technologies

Subscribe to our mailing list

Comment

PREVIEWPREVIEW