Jump to content

Writing tests: Difference between revisions

From NusaATK
No edit summary
 
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
(This howto is a work in progress)
{{Howto|easy|Sandy Pleyte <sandy@achievo.org>, Boy Baukema <boy@achievo.org>}}
 
== Basic testcase ==
To create a test you need to take the following steps:
To create a test you need to take the following steps:


Create a directory 'testcases' in your module directory.
Create a directory 'testcases' in the directory where the class is located that you want to write a testcase for.


In the directory you need to place one or more files with the name:
In the directory you need to place one or more files with the name:
    
    
   class.test_[testnaam].inc
   class.test_[testname].inc
 
A basic test case has the following structure:


A test cases needs the following structure:
<syntaxhighlight lang="php">
  atkimport("[modulename].[class you want to test]");


  require_once(moduleDir("[modulename]")."class.[class you want to test].inc");
  //Take for example the class 'class.example.inc'
   /**
   /**
   * Unittests for the example class
   * Unittests for the example class
Line 18: Line 21:
   class test_testcase_name extends atkTestCase  
   class test_testcase_name extends atkTestCase  
   {
   {
     //Warning: This is PHP5.
     /**
    public function __construct()
    * This (optional) function will be called before each
    {
    * test_* function. Use it to initialise your test.
      parent::__construct(); //Not necessary now, but maybe in the future.
    */
      $this->addFixture('module.table'); //Fixtures need to be loaded in the constructor.
    }
    //Deze functie wordt steeds voor alle andere test_* functies aangeroepen.
     function setUp()
     function setUp()
     {
     {
      //Niet vergeten ook de setUp van de parent te doen.
       parent::setUp();
       parent::setUp();
     }
     }
     //Deze functie wordt steeds na alle andere test_* functies aangeroepen.
 
     /**
    * This (optional) function will be called after
    * each test_* function.  
    * Useful to do clean-up.
    */
     function tearDown()
     function tearDown()
     {
     {
      //Niet vergeten ook de tearDown van de parent te doen.
       parent::tearDown();
       parent::tearDown();
     }
     }
     //Simpele test functie
 
     /**
    * Simple test function
    */
     function test_doSomething()
     function test_doSomething()
     {
     {
       //Doe dingen
       // Do the actual test here.
       $class = new exampleclass();
       $class = new exampleclass();
       $class->doSomething();
       $class->doSomething();
       $result = $class->getSomeValue;
       $result = $class->getSomeValue();
       $this->assertEqual($result,'waarde_die_ik_had_dit_verwacht','tekst die in de tests wordt getoond.');
       $this->assertEqual($result,'expected_value','text in the test.');
     }
     }
   }
   }
</syntaxhighlight>


Het eigenlijke testen wordt door de assert* functies gedaan. Er zijn er verschillende, en de naam geeft
Reporting test results is done by the assert* functions. Here is a small list of the most common ones:
goed aan wat de eigenlijke functie is:


<syntaxhighlight lang="php">
   assertNull($value, $message = "%s")
   assertNull($value, $message = "%s")
   assertIsA($object, $type, $message = "%s")
   assertIsA($object, $type, $message = "%s")
Line 63: Line 70:
   assertTrue($result, $message = false)
   assertTrue($result, $message = false)
   assertFalse($result, $message = false)
   assertFalse($result, $message = false)
</syntaxhighlight>
== PHPUnit testcases ==
As of 20-05-2008 the nightly of ATK also supports PHPUnit testcases, to add a phpunit testcase simple follow the above instructions for creating a testcase only name it as follows:
  class.test_[testname].phpunit.inc
A basic PHPUnit testcase will have the following structure:
<syntaxhighlight lang="php">
  class test_foo extends PHPUnit_Framework_TestCase
  {
    public function testBar()
    {
      $this->assertEquals('foo','bar','This test will fail');
    }
  }
</syntaxhighlight>
Most assert function of SimpleTest are also in phpUnit, for more information, please search online for 'writing phpunit testcases'.
To run all phpunit testcases, make sure the atkappsuite.php file from atk/skel is in your application root, the atkroot and appplication_dir configuration values in the atkappsuite.php file are properly set and you have phpunit installed on your server, then run:
  phpunit atkappsuite


En vele anderen. Zie
And PHPUnit will make ATK look up all the phpunit testcases in your application and run them, resulting in output like the following:
  platform/atk/test/
voor meer mogelijkheden.


  PHPUnit 3.2.9 by Sebastian Bergmann.
 
  F.
 
  Time: 0 seconds
 
  There was 1 failure:
 
  1) testBar(test_foo)
  Failed asserting that two strings are equal.
  expected string <foo>
  difference      <xxx>
  got string      <bar>
  /var/www/html/atkapp/modules/foomodule/testcases/class.test_foo.phpunit.inc:7
 
  FAILURES!
  Tests: 1, Failures: 1.


Meer informatie op
== Fixtures ==
[http://www.achievo.org/wiki/Using_a_test_database http://www.achievo.org/wiki/Using_a_test_database]
 
In testcases you will sometimes want to use simulated database data. You can use [[Fixtures]] for this.
See the following howto for using fixtures:
 
* [[Using a test database]]
 
== Running the testcases ==
 
The following howto explains how to run a suite of testcases:
 
* [[Run tests]]


=== Overloaders ===
=== Overloaders ===


Soms gebruikt een functie een klasse waarvoor je veel extra setup moet doen, dan kan het handig zijn
Sometimes a function uses a class for which you need to do some extra setup, at that point it can be handy
een 'mock' klasse te gebruiken die standaard resultaten oplevert. Dan kun je die klasse overloaden door
to use a 'mock' class which will return some default results. Then you can overload this class by using an
middel van een overloader:
overloader:


Dit werkt overigens alleen wanneer de klasse in kwestie geladen wordt door atkNew().
This only works when a class is loaded with atkNew().


Een voorbeeld:
An example:


   function test_eenclass()
<syntaxhighlight lang="php">
   function test_aclass()
   {
   {
     // ik weet dat deze funktie atkSoap gebruikt maar dat wil ik effe niet voor deze test.
     // I know this function uses atkSoap, but we don't want to use it for this test
     atkClassLoader::addOverloader("atk.utils.atksoap", "mycustomsoapstub");
     atkClassLoader::addOverloader("atk.utils.atksoap", "mycustomsoapstub");
     $result = eenfunktie();
     $result = afunction();
     $this->assertEqual($result, "blabla", "resultaat moet blabla zijn");
     $this->assertEqual($result, "blabla", "result should be blabla");
   }
   }
</syntaxhighlight>
== More information ==
[http://www.lastcraft.com/simple_test.php Simpletest]

Latest revision as of 14:37, 20 May 2008

ATK Howto: Writing tests

Complexity: easy
Author: Sandy Pleyte <sandy@achievo.org>, Boy Baukema <boy@achievo.org>

List of other Howto's

Basic testcase

To create a test you need to take the following steps:

Create a directory 'testcases' in the directory where the class is located that you want to write a testcase for.

In the directory you need to place one or more files with the name:

 class.test_[testname].inc

A basic test case has the following structure:

  atkimport("[modulename].[class you want to test]");

  /**
   * Unittests for the example class
   *
   */
  class test_testcase_name extends atkTestCase 
  {
    /**
     * This (optional) function will be called before each 
     * test_* function. Use it to initialise your test.
     */
    function setUp()
    {
      parent::setUp();
    }

    /**
     * This (optional) function will be called after 
     * each test_* function. 
     * Useful to do clean-up.
     */
    function tearDown()
    {
      parent::tearDown();
    }

    /**
     * Simple test function
     */
    function test_doSomething()
    {
      // Do the actual test here.
      $class = new exampleclass();
      $class->doSomething();
      $result = $class->getSomeValue();
      $this->assertEqual($result,'expected_value','text in the test.');
    }
  }

Reporting test results is done by the assert* functions. Here is a small list of the most common ones:

  assertNull($value, $message = "%s")
  assertIsA($object, $type, $message = "%s")
  assertEqual($first, $second, $message = "%s")
  assertIdentical($first, $second, $message = "%s")
  assertReference(&$first, &$second, $message = "%s")
  assertCopy(&$first, &$second, $message = "%s")
  assertWantedPattern($pattern, $subject, $message = "%s")
  assertNoUnwantedPattern($pattern, $subject, $message = "%s")
  assertNoErrors($message = "%s")
  assertError($expected = false, $message = "%s")
  assertErrorPattern($pattern, $message = "%s")
  assertTrue($result, $message = false)
  assertFalse($result, $message = false)

PHPUnit testcases

As of 20-05-2008 the nightly of ATK also supports PHPUnit testcases, to add a phpunit testcase simple follow the above instructions for creating a testcase only name it as follows:

 class.test_[testname].phpunit.inc

A basic PHPUnit testcase will have the following structure:

  class test_foo extends PHPUnit_Framework_TestCase
  {
    public function testBar()
    {
      $this->assertEquals('foo','bar','This test will fail');
    }
  }

Most assert function of SimpleTest are also in phpUnit, for more information, please search online for 'writing phpunit testcases'.

To run all phpunit testcases, make sure the atkappsuite.php file from atk/skel is in your application root, the atkroot and appplication_dir configuration values in the atkappsuite.php file are properly set and you have phpunit installed on your server, then run:

 phpunit atkappsuite

And PHPUnit will make ATK look up all the phpunit testcases in your application and run them, resulting in output like the following:

 PHPUnit 3.2.9 by Sebastian Bergmann.
 
 F.
 
 Time: 0 seconds
 
 There was 1 failure:
 
 1) testBar(test_foo)
 Failed asserting that two strings are equal.
 expected string <foo>
 difference      <xxx>
 got string      <bar>
 /var/www/html/atkapp/modules/foomodule/testcases/class.test_foo.phpunit.inc:7
 
 FAILURES!
 Tests: 1, Failures: 1.

Fixtures

In testcases you will sometimes want to use simulated database data. You can use Fixtures for this. See the following howto for using fixtures:

Running the testcases

The following howto explains how to run a suite of testcases:

Overloaders

Sometimes a function uses a class for which you need to do some extra setup, at that point it can be handy to use a 'mock' class which will return some default results. Then you can overload this class by using an overloader:

This only works when a class is loaded with atkNew().

An example:

  function test_aclass()
  {
    // I know this function uses atkSoap, but we don't want to use it for this test
    atkClassLoader::addOverloader("atk.utils.atksoap", "mycustomsoapstub");
    $result = afunction();
    $this->assertEqual($result, "blabla", "result should be blabla");
  }

More information

Simpletest