Jump to content

Writing tests: Difference between revisions

From NusaATK
No edit summary
 
No edit summary
Line 24: Line 24:
       $this->addFixture('module.table'); //Fixtures need to be loaded in the constructor.
       $this->addFixture('module.table'); //Fixtures need to be loaded in the constructor.
     }
     }
     //Deze functie wordt steeds voor alle andere test_* functies aangeroepen.
     //This function will be called before all other test_* functions.
     function setUp()
     function setUp()
     {
     {
       //Niet vergeten ook de setUp van de parent te doen.
       //Don't forget to call the setUp of the parent.
       parent::setUp();
       parent::setUp();
     }
     }
     //Deze functie wordt steeds na alle andere test_* functies aangeroepen.
     //This function will be called after all other test_* functions.
     function tearDown()
     function tearDown()
     {
     {
       //Niet vergeten ook de tearDown van de parent te doen.
       //Don't forget to call the teadDown of the parent.
       parent::tearDown();
       parent::tearDown();
     }
     }
     //Simpele test functie
     //Simpele test function
     function test_doSomething()
     function test_doSomething()
     {
     {
       //Doe dingen
       //Do some stuff
       $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.');
     }
     }
   }
   }


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


   assertNull($value, $message = "%s")
   assertNull($value, $message = "%s")
Line 64: Line 63:
   assertFalse($result, $message = false)
   assertFalse($result, $message = false)


En vele anderen. Zie
These can be found in the 'atk/test' directory.
  platform/atk/test/
voor meer mogelijkheden.




Meer informatie op
More information:
 
[http://www.lastcraft.com/simple_test.php Simpletest]
[http://www.achievo.org/wiki/Using_a_test_database http://www.achievo.org/wiki/Using_a_test_database]
[http://www.achievo.org/wiki/Using_a_test_database http://www.achievo.org/wiki/Using_a_test_database]


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

Revision as of 07:39, 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 your module directory.

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

 class.test_[testnaam].inc

A test cases needs the following structure:

 require_once(moduleDir("[modulename]")."class.[class you want to test].inc");
 //Take for example the class 'class.example.inc'
 /**
  * Unittests for the example class
  *
  */
 class test_testcase_name extends atkTestCase 
 {
   //Warning: This is PHP5.
   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.
   function setUp()
   {
     //Don't forget to call the setUp of the parent.
     parent::setUp();
   }
   //This function will be called after all other test_* functions.
   function tearDown()
   {
     //Don't forget to call the teadDown of the parent.
     parent::tearDown();
   }
   //Simpele 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)

These can be found in the 'atk/test' directory.


More information:

Simpletest http://www.achievo.org/wiki/Using_a_test_database

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