testing and drupal
DESCRIPTION
Testing in DrupalTRANSCRIPT
Testing
Why we need to test our code (site)?
Ensure securityCheck semantic and syntaxIn a small team you can track changesIt can reveal bugs in a very early stage of developmentEnvironment validationFastPermanent (not like us:( )TDD - (later)
Testing types
Unit Test
PHPUnit (PHP)SimpleTest (PHP)jUnit (Java)NUnit (C#)...
Functional Test
Selenium IDE (html) Grid (???) Remote Control CoreCastle
Testing in common use
Grouping test suites -> test tree Optional start nodeSet-Up <-> Tear-DownEvery single test could be a separate workflowArbitrary extension capability (SimpleTest -> SimpleBrowserTest)Skip tests
Testing functions
check if on value is... a specific value true false not null null
check if an element / object exists not exists equal to ...
check if an error (exception) occurecheck if an event triggered
Browser element properties
Locators id=___ name=___ dom=___ (javascript object) xpath=___ link=___ css=__
Matchings glob:___ refexp:___ exact:___
UnitTest sample
public class UserTest extends UnitTest { private User user;
void setUp() { this.user = User.GetSampleUser(); }
void testUserSetName() { this.user.setName('John Doe'); this.assertTrue(this.user.name.length > 0); this.assertFalse(this.user.name == 'anonym'); this.assertEquals('John Doe', this.user.getName); }}
Functional test sample
public class SearchTest extends FunctionalTest { function testFindResult() { this.openBrowser('firefox'); this.open('http://www.example.com', 30000); this.type("//input[@id='search-field']", "Harrr"); this.click("//fieldset/input[@type=submit]"); this.waitForPageToLoad(30000); this.assertTestPresent("regexp:\\d+ results found.*"); this.assertCookieExists('MySessionID'); }}
What you can test?
Unittest: Levels: Function testing Feature testing System testing
Functional test: Page elements Ajax jobs Javascript
Shortcomings in testing
Testing GUI appearanceComplicated system actions (e.g.: in specific cases)There is no setup and teardown on test suit levelIt's easily became slowBrowser (un)capabilities frame / window handling security issues (file upload) best in FF (of course)If your code needs some conceptual change, likely your tests tooSelenium sometimes collide with other JS frameworks (MooTools)UnitTest modify access to global variables, so sometimes those are unavailable
Why TDD is cool?
You can fix the topmost (maybe all the) requirementsHelp to build a more stable software requirementContinuous code checkingPrevent from writing test for code (the best: write code for the test)
Drupal and testing
Drupal uses SimpleTest frameworkBunch of helper to ease preparing environment for test for: create user login user post nodeEvery core module has it's own module.test fileEvery core.inc has it's own test in simpletest module
Unfortunately Drupal's Selenium module is unsupported now
Tips and tricks
Try to cover all your code with testsDon't write test depend on other tests If one fails, it can ruin other tests (even good ones) Without dependency, every test could be called separatelyNever run tests on live site (neither live database dump)You can write a script to svn that runs the test suit before commit - prevent from commiting false code