Jump to content

Writing tests: Difference between revisions

From NusaATK
Highlighting, cleanup, adherence to coding standards.
No edit summary
Line 1: Line 1:
(This howto is a work in progress)
{{Howto|easy|Sandy Pleyte <sandy@achievo.org>}}
To create a test you need to take the following steps:
To create a test you need to take the following steps:


Line 8: Line 8:
   class.test_[testname].inc
   class.test_[testname].inc


A test cases needs the following structure:
A basic test case has the following structure:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 20: Line 20:
   {
   {
     /**
     /**
     * Default constructor.
     * This (optional) function will be called before each
    */
    * test_* function. Use it to initialise your test.
    public function __construct()
    {
      parent::__construct(); //Not necessary now, but maybe in the future.
      $this->addFixture('module.table'); //Fixtures need to be loaded in the constructor.
    }
 
    /**
    * This function will be called before all other test_* functions. Use it to initialise
    * your testclass.
     */
     */
     function setUp()
     function setUp()
     {
     {
      //Don't forget to call the setUp of the parent.
       parent::setUp();
       parent::setUp();
     }
     }


     /**
     /**
     * This function will be called after all other test_* functions.  
     * This (optional) function will be called after  
     * Useful to clean stuff up.
    * each test_* function.  
     * Useful to do clean-up.
     */
     */
     function tearDown()
     function tearDown()
     {
     {
      //Don't forget to call the teadDown of the parent.
       parent::tearDown();
       parent::tearDown();
     }
     }
Line 53: Line 43:
     function test_doSomething()
     function test_doSomething()
     {
     {
       //Do some stuff
       // Do the actual test here.
       $class = new exampleclass();
       $class = new exampleclass();
       $class->doSomething();
       $class->doSomething();
Line 62: Line 52:
</syntaxhighlight>
</syntaxhighlight>


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


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 80: Line 70:
</syntaxhighlight>
</syntaxhighlight>


More information:
== Fixtures ==


[http://www.lastcraft.com/simple_test.php Simpletest]
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 ==


[[Using a test database]]
The following howto explains how to run a suite of testcases:


[[Run tests]]
* [[Run tests]]


=== Overloaders ===
=== Overloaders ===
Line 107: Line 102:
   }
   }
</syntaxhighlight>
</syntaxhighlight>
== More information ==
[http://www.lastcraft.com/simple_test.php Simpletest]

Revision as of 05:23, 16 May 2007

ATK Howto: Writing tests

Complexity: easy
Author: Sandy Pleyte <sandy@achievo.org>

List of other Howto's

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)

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