Jump to content

Writing tests: Difference between revisions

From NusaATK
No edit summary
Highlighting, cleanup, adherence to coding standards.
Line 2: Line 2:
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 test cases needs the following structure:
A test cases needs the following structure:


   require_once(moduleDir("[modulename]")."class.[class you want to test].inc");
<syntaxhighlight lang="php">
  //Take for example the class 'class.example.inc'
   atkimport("[modulename].[class you want to test]");
 
   /**
   /**
   * Unittests for the example class
   * Unittests for the example class
Line 18: Line 19:
   class test_testcase_name extends atkTestCase  
   class test_testcase_name extends atkTestCase  
   {
   {
     //Warning: This is PHP5.
     /**
    * Default constructor.
    */
     public function __construct()
     public function __construct()
     {
     {
Line 24: Line 27:
       $this->addFixture('module.table'); //Fixtures need to be loaded in the constructor.
       $this->addFixture('module.table'); //Fixtures need to be loaded in the constructor.
     }
     }
     //This function will be called before all other test_* functions.
 
     /**
    * This function will be called before all other test_* functions. Use it to initialise
    * your testclass.
    */
     function setUp()
     function setUp()
     {
     {
Line 30: Line 37:
       parent::setUp();
       parent::setUp();
     }
     }
     //This function will be called after all other test_* functions.
 
     /**
    * This function will be called after all other test_* functions.  
    * Useful to clean stuff up.
    */
     function tearDown()
     function tearDown()
     {
     {
Line 36: Line 47:
       parent::tearDown();
       parent::tearDown();
     }
     }
     //Simpele test function
 
     /**
    * Simple test function
    */
     function test_doSomething()
     function test_doSomething()
     {
     {
Line 42: Line 56:
       $class = new exampleclass();
       $class = new exampleclass();
       $class->doSomething();
       $class->doSomething();
       $result = $class->getSomeValue;
       $result = $class->getSomeValue();
       $this->assertEqual($result,'expected_value','text in the test.');
       $this->assertEqual($result,'expected_value','text in the test.');
     }
     }
   }
   }
</syntaxhighlight>


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


<syntaxhighlight lang="php">
   assertNull($value, $message = "%s")
   assertNull($value, $message = "%s")
   assertIsA($object, $type, $message = "%s")
   assertIsA($object, $type, $message = "%s")
Line 62: Line 78:
   assertTrue($result, $message = false)
   assertTrue($result, $message = false)
   assertFalse($result, $message = false)
   assertFalse($result, $message = false)
 
</syntaxhighlight>
These can be found in the 'atk/test' directory.
 


More information:
More information:
Line 84: Line 98:
An example:
An example:


<syntaxhighlight lang="php">
   function test_aclass()
   function test_aclass()
   {
   {
Line 91: Line 106:
     $this->assertEqual($result, "blabla", "result should be blabla");
     $this->assertEqual($result, "blabla", "result should be blabla");
   }
   }
</syntaxhighlight>

Revision as of 17:54, 14 May 2007

(This howto is a work in progress) 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 test cases needs the following structure:

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

  /**
   * Unittests for the example class
   *
   */
  class test_testcase_name extends atkTestCase 
  {
    /**
     * Default constructor.
     */
    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()
    {
      //Don't forget to call the setUp of the parent.
      parent::setUp();
    }

    /**
     * This function will be called after all other test_* functions. 
     * Useful to clean stuff up.
     */
    function tearDown()
    {
      //Don't forget to call the teadDown of the parent.
      parent::tearDown();
    }

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

The actual testing is done by the assert* functions. Here is a small list of the most used 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)

More information:

Simpletest

Using a test database

Run tests

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