SimpleTest conversion: Difference between revisions
From MoodleDocs
mNo edit summary |
|||
(19 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{Template:obsolete}} | |||
{{Moodle 2.3}} | |||
=Overview= | =Overview= | ||
Moodle 2.3 switched to PHPUnit as the recommended unit testing framework. SimpleTest support will be completely removed from Moodle 2.4. | |||
The migration is pretty straightforward: | The migration is pretty straightforward: | ||
# create new test file in `xxx/tests/yyy_test.php` | # create new test file in `xxx/tests/yyy_test.php` | ||
# copy contents of the old test file | # copy contents of the old test file | ||
# replace < | # replace <syntaxhighlight lang="php">extends UnitTestCase</syntaxhighlight> with <syntaxhighlight lang="php">extends basic_testcase</syntaxhighlight> and <syntaxhighlight lang="php">extends UnitTestCaseUsingDatabase</syntaxhighlight> with <syntaxhighlight lang="php">extends advanced_testcase</syntaxhighlight> | ||
# move constructor code to setUp | # move constructor code to setUp() | ||
# fix setUp() | # fix setUp() and tearDown() to be protected | ||
# fix assert syntax | # fix assert syntax | ||
# add $this->resetAfterTest() in advanced test cases that modify database | # add <syntaxhighlight lang="php">$this->resetAfterTest();</syntaxhighlight> in advanced test cases that modify database | ||
# add missing global $CFG for includes outside of testcase classes | # add missing <syntaxhighlight lang="php">global $CFG;</syntaxhighlight> for includes outside of testcase classes | ||
# Usages of <syntaxhighlight lang="php">$UNITTEST->running</syntaxhighlight> must be replaced with <syntaxhighlight lang="php">PHPUNIT_TEST</syntaxhighlight> | |||
Usually the tests using database can be significantly simplified, the performance in PHPUnit is also a lot better. | |||
The old SimpleTest execution page is hidden in 2.3, if you want to execute the old tests go to http://www.example.com/admin/tool/unittest/index.php page on your server. | |||
=Assert differences= | =Assert differences= | ||
{| class=" | {| class="wikitable" | ||
|- | |- | ||
! SimpleTest | ! SimpleTest | ||
Line 19: | Line 28: | ||
! Notes | ! Notes | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertEqual($expected, $actual)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertEquals($expected, $actual)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertNotEqual($expected, $actual)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertNotEquals($expected, $actual)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertWithinMargin($expected, $actual, $margin)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertEquals($expected, $actual, '', $margin)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertIdentical($expected, $actual)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertSame($expected, $actual)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertNotIdentical($expected, $actual)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertNotSame($expected, $actual)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertTrue($actual)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertTrue((bool)$actual)</syntaxhighlight><br /><syntaxhighlight lang="php">$this->assertNotEmpty($actual)</syntaxhighlight> | ||
| SimpleTest converts parameter to bool | | SimpleTest converts parameter to bool | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertFalse($actual)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertFalse((bool)$actual)</syntaxhighlight><br /><syntaxhighlight lang="php">$this->assertEmpty($actual)</syntaxhighlight> | ||
| SimpleTest converts parameter to bool | | SimpleTest converts parameter to bool | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertIsA($actual, 'classname')</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertInstanceOf('classname', $actual)</syntaxhighlight> | ||
| only | | objects instances only | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertIsA($actual, 'array')</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertEquals('array', gettype($actual))</syntaxhighlight> | ||
| arrays only | | arrays only | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertPattern($pattern, $string)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertRegExp($pattern, $string)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->assertNoPattern($pattern, $string)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->assertNotRegExp($pattern, $string)</syntaxhighlight> | ||
| | | | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->expectException(true)</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->setExpectedException('exception_classname')</syntaxhighlight> | ||
| alternatively use phpdocs: @expectedException exception_classname | | alternatively use phpdocs: @expectedException exception_classname | ||
|- | |- | ||
| < | | <syntaxhighlight lang="php">$this->expectError()</syntaxhighlight> | ||
| < | | <syntaxhighlight lang="php">$this->setExpectedException('PHPUnit_Framework_Error')</syntaxhighlight> | ||
| alternatively use phpdocs: @expectedException PHPUnit_Framework_Error | | alternatively use phpdocs: @expectedException PHPUnit_Framework_Error | ||
|- | |- | ||
| various HTML expectations | | various HTML expectations | ||
| < | | <syntaxhighlight lang="php">$this->assertTag()</syntaxhighlight> | ||
| | | | ||
|} | |} | ||
=SimpleTest emulation= | =SimpleTest emulation= | ||
Some original SimpleTests can be executed directly via PHPUnit with minimal modifications (try adding global $CFG for includes outside of test case class). | |||
[[Category:Unit testing]] | [[Category:Unit testing]] |
Latest revision as of 16:28, 30 April 2024
Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable. |
Moodle 2.3
Overview
Moodle 2.3 switched to PHPUnit as the recommended unit testing framework. SimpleTest support will be completely removed from Moodle 2.4.
The migration is pretty straightforward:
- create new test file in `xxx/tests/yyy_test.php`
- copy contents of the old test file
- replace with
extends UnitTestCase
andextends basic_testcase
withextends UnitTestCaseUsingDatabase
extends advanced_testcase
- move constructor code to setUp()
- fix setUp() and tearDown() to be protected
- fix assert syntax
- add in advanced test cases that modify database
$this->resetAfterTest();
- add missing for includes outside of testcase classes
global $CFG;
- Usages of must be replaced with
$UNITTEST->running
PHPUNIT_TEST
Usually the tests using database can be significantly simplified, the performance in PHPUnit is also a lot better.
The old SimpleTest execution page is hidden in 2.3, if you want to execute the old tests go to http://www.example.com/admin/tool/unittest/index.php page on your server.
Assert differences
SimpleTest | PHPUnit | Notes |
---|---|---|
$this->assertEqual($expected, $actual)
|
$this->assertEquals($expected, $actual)
|
|
$this->assertNotEqual($expected, $actual)
|
$this->assertNotEquals($expected, $actual)
|
|
$this->assertWithinMargin($expected, $actual, $margin)
|
$this->assertEquals($expected, $actual, '', $margin)
|
|
$this->assertIdentical($expected, $actual)
|
$this->assertSame($expected, $actual)
|
|
$this->assertNotIdentical($expected, $actual)
|
$this->assertNotSame($expected, $actual)
|
|
$this->assertTrue($actual)
|
$this->assertTrue((bool)$actual)
$this->assertNotEmpty($actual)
|
SimpleTest converts parameter to bool |
$this->assertFalse($actual)
|
$this->assertFalse((bool)$actual)
$this->assertEmpty($actual)
|
SimpleTest converts parameter to bool |
$this->assertIsA($actual, 'classname')
|
$this->assertInstanceOf('classname', $actual)
|
objects instances only |
$this->assertIsA($actual, 'array')
|
$this->assertEquals('array', gettype($actual))
|
arrays only |
$this->assertPattern($pattern, $string)
|
$this->assertRegExp($pattern, $string)
|
|
$this->assertNoPattern($pattern, $string)
|
$this->assertNotRegExp($pattern, $string)
|
|
$this->expectException(true)
|
$this->setExpectedException('exception_classname')
|
alternatively use phpdocs: @expectedException exception_classname |
$this->expectError()
|
$this->setExpectedException('PHPUnit_Framework_Error')
|
alternatively use phpdocs: @expectedException PHPUnit_Framework_Error |
various HTML expectations | $this->assertTag()
|
SimpleTest emulation
Some original SimpleTests can be executed directly via PHPUnit with minimal modifications (try adding global $CFG for includes outside of test case class).