13
Testing

Testing And Drupal

Embed Size (px)

DESCRIPTION

Testing in Drupal

Citation preview

Page 1: Testing And Drupal

Testing

Page 2: Testing And Drupal

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)

Page 3: Testing And Drupal

Testing types

Unit Test

PHPUnit (PHP)SimpleTest (PHP)jUnit (Java)NUnit (C#)...

Functional Test

Selenium    IDE (html)    Grid (???)    Remote Control    CoreCastle

Page 4: Testing And Drupal

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

Page 5: Testing And Drupal

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

Page 6: Testing And Drupal

Browser element properties

Locators    id=___    name=___    dom=___ (javascript object)    xpath=___    link=___    css=__

Matchings    glob:___    refexp:___    exact:___ 

Page 7: Testing And Drupal

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);    }}

Page 8: Testing And Drupal

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');    }}

Page 9: Testing And Drupal

What you can test?

Unittest:    Levels:        Function testing        Feature testing        System testing

Functional test:    Page elements    Ajax jobs    Javascript

Page 10: Testing And Drupal

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

Page 11: Testing And Drupal

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)

Page 12: Testing And Drupal

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

Page 13: Testing And Drupal

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