Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

PHPUnit integration: Difference between revisions

From MoodleDocs
No edit summary
Line 3: Line 3:
Most of the documentation at http://www.phpunit.de/manual/3.6/en/index.html is relevant to Moodle PHPUnit integration, exceptions and additions are described bellow.
Most of the documentation at http://www.phpunit.de/manual/3.6/en/index.html is relevant to Moodle PHPUnit integration, exceptions and additions are described bellow.


==Definitions==
=Definitions=
These definitions may differ in each testing framework or programming language. The definitions here should be valid for PHPUnit framework with our Moodle tweaks.
These definitions may differ in each testing framework or programming language. The definitions here should be valid for PHPUnit framework with our Moodle tweaks.


Line 16: Line 16:
;Assertion: is the actual comparison of expected and actual behaviour of the tested code.
;Assertion: is the actual comparison of expected and actual behaviour of the tested code.


==Organisation of test files==
=Organisation of test files=
All testing related files are stored in <code>/tests/</code> subdirectories:
All testing related files are stored in <code>/tests/</code> subdirectories:


Line 24: Line 24:
* <code>.../tests/other/</code> is used for everything else that does not fit
* <code>.../tests/other/</code> is used for everything else that does not fit


== basic_testcase==
=basic_testcase=
''basic_testcase'' is useful for simple tests that do not modify database or global variables. If something accidentally changes global state the test fails. This test case class is nearly identical to PHPUnit_Framework_TestCase used in PHPUnit documentation.
''basic_testcase'' is useful for simple tests that do not modify database or global variables. If something accidentally changes global state the test fails. This test case class is nearly identical to PHPUnit_Framework_TestCase used in PHPUnit documentation.


Line 57: Line 57:
</code>
</code>


==advanced_testcase==
=advanced_testcase=








==Extra features==
=Extra features=
* detection of global state changes
* detection of global state changes
* global state reset
* global state reset

Revision as of 17:08, 20 April 2012

Moodle PHPUnit integration was created to simplify using of PHPUnit framework in Moodle. It consists of specialised bootstrap script, utility scripts that initialise testing environment and highly optimised custom test case classes that handle automatic global state resetting after test that includes global variables, database rollback and purging of dataroot. We also try to bridge the gap between the design and coding style of Moodle and PHPUnit.

Most of the documentation at http://www.phpunit.de/manual/3.6/en/index.html is relevant to Moodle PHPUnit integration, exceptions and additions are described bellow.

Definitions

These definitions may differ in each testing framework or programming language. The definitions here should be valid for PHPUnit framework with our Moodle tweaks.

Test suite
is a collection of test cases or other suites usually related to one area of the product. It is defined in PHPUnit configuration files (phpunit.xml by default).
Test file
is a file with with *_test.php name which contains test cases. (*_Test.php in PHPUnit)
Test case
is a class with test methods. Moodle tests extend basic_testcase or advanced_testcase. (PHPUnit_Framework_TestCase in PHPUnit)
Test
is a public method starting with "test_" prefix defined in test case class. It is the implementation of the test procedure.
Assertion
is the actual comparison of expected and actual behaviour of the tested code.

Organisation of test files

All testing related files are stored in /tests/ subdirectories:

  • .../tests/*_test.php are PHPUnit test files with classes that extend basic_testcase or advanced_testcase
  • .../tests/fixtures/ contains auxiliary files used in tests
  • .../tests/performance/ is reserved for performance testing scripts
  • .../tests/other/ is used for everything else that does not fit

basic_testcase

basic_testcase is useful for simple tests that do not modify database or global variables. If something accidentally changes global state the test fails. This test case class is nearly identical to PHPUnit_Framework_TestCase used in PHPUnit documentation.

Sample class for testing of HTML Purifier integration:

class core_htmlpurifier_testcase extends basic_testcase {

   /**
    * Verify our nolink tag accepted
    * @return void
    */
   public function test_nolink() {

$text = '<nolink>

no filters

</nolink>';

       $result = purify_html($text, array());
       $this->assertSame($text, $result);

$text = '<nolink>xxxxx

xxx

</nolink>';

       $result = purify_html($text, array());
       $this->assertSame($text, $result);
   }
   /**
    * Verify our tex tag accepted
    * @return void
    */
   public function test_tex() {
       $text = '<tex>a+b=c</tex>';
       $result = purify_html($text, array());
       $this->assertSame($text, $result);
   }

}

advanced_testcase

Extra features

  • detection of global state changes
  • global state reset
  • transaction rollbacks
  • dataset loading
  • database driver testing class
  • SimpleTest emulation class