Drupalcamp Simpletest

14
Testing in Drupal An Introduction

Transcript of Drupalcamp Simpletest

Page 1: Drupalcamp Simpletest

Testing in DrupalAn Introduction

Page 2: Drupalcamp Simpletest

Who Am I?

Simon RobertsIT Consultant,Taniwha SolutionsUsing PHP since ~1996Using Drupal since 2005http://drupal.org/user/33667

First used SimpleTest at Drupalcon Szeged Testing Sprint

Page 3: Drupalcamp Simpletest

Who Am I?

Before the Drupalcon, I’d only had the most casual of looks at Testing in DrupalHave used JUnit etc previouslyWas able to provide useful tests to Drupal Core inside a couple of hours!!You can too!

Page 4: Drupalcamp Simpletest

What was SimpleTest?

PHP Testing FrameworkIntegrated as a contributed module in D5 + D6Integrated in core for Drupal 7 (sortof)Implements Automated Testing

Page 5: Drupalcamp Simpletest

Why Automated Testing?

Define exactly what the code should do Easier development Easier to refactor code Less debugging Less mistakes More fun!!

Page 6: Drupalcamp Simpletest

Why Automated Testing?

Document Bugs$xss = '<script>alert("xss")</script>'; $edit = array(

'title' => $xss,

);

$node = $this->drupalCreateNode($edit);

$this->drupalGet('node/' . $node->nid . '/edit');

$this->assertNoRaw($xss, t('Harmful tags are escaped.'));

Page 7: Drupalcamp Simpletest

Why Automated Testing?

Document BugsPrevent regressionsQuality AssuranceBetter Software

Safety net!

Page 8: Drupalcamp Simpletest

Why Automated Testing?

Once a test is written, it will likely be run many many timesEvery time it is run, it verifies that the code is still workingIf it catches even one bug, it has “paid for itself” - High valueCan be used to help reproduce problems

Page 9: Drupalcamp Simpletest

Testing in Drupal Core

Dries (and Drupal!) has made Testing a high priority for Drupal 7http://buytaert.net/drupal-7-timeline

Effective Test coverage allows us to compress the “fix” phase of a Drupal release (code freeze)

The actual Drupal 6 Timeline

The Drupal 7 timeline assuming that we fully embrace testing.

Note: These dates are going to be delayed anyway - slower migration to Drupal 6 than expected (eg: CCK, Views, Panels. drupal.org)

Page 10: Drupalcamp Simpletest

Not just for Drupal Core!

Page 11: Drupalcamp Simpletest

so... how does it work?

.test files

Contain test classes

Test classes extend the SimpleTest class

well, not exactly

Use Helper methods to:

Simulate user actions

Check behavior

Page 12: Drupalcamp Simpletest

No, really... how does it work?

Let me show you...

Page 13: Drupalcamp Simpletest

What’s Left To Do?

Outstanding Testing Issueshttp://drupal.org/project/issues?projects=3060&versions=156281&components=simpletest.module,tests

“We currently have 38 tests taken out of 43 for a test coverage of 88%. This will change when the misnamed tests are merged.” http://groups.drupal.org/node/9408

Page 14: Drupalcamp Simpletest

References

Webchick’s presentation “Testing Part 1: Intro to Testing” (Szeged Drupalcon)

http://drupal.org/simpletest

http://groups.drupal.org/testing-qa

One SimpleTest template to rule them allhttp://groups.drupal.org/node/7731

Drupal testing patternshttp://groups.drupal.org/node/15190