SimpleTest conversion
From MoodleDocs
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 <syntaxhighlight lang="php">extends UnitTestCase with <syntaxhighlight lang="php">extends basic_testcase and <syntaxhighlight lang="php">extends UnitTestCaseUsingDatabase with <syntaxhighlight lang="php">extends advanced_testcase
- move constructor code to setUp()
- fix setUp() and tearDown() to be protected
- fix assert syntax
- add <syntaxhighlight lang="php">$this->resetAfterTest(); in advanced test cases that modify database
- add missing <syntaxhighlight lang="php">global $CFG; for includes outside of testcase classes
- Usages of <syntaxhighlight lang="php">$UNITTEST->running must be replaced with <syntaxhighlight lang="php">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 |
|---|---|---|
| <syntaxhighlight lang="php">$this->assertEqual($expected, $actual) | <syntaxhighlight lang="php">$this->assertEquals($expected, $actual) | |
| <syntaxhighlight lang="php">$this->assertNotEqual($expected, $actual) | <syntaxhighlight lang="php">$this->assertNotEquals($expected, $actual) | |
| <syntaxhighlight lang="php">$this->assertWithinMargin($expected, $actual, $margin) | <syntaxhighlight lang="php">$this->assertEquals($expected, $actual, , $margin) | |
| <syntaxhighlight lang="php">$this->assertIdentical($expected, $actual) | <syntaxhighlight lang="php">$this->assertSame($expected, $actual) | |
| <syntaxhighlight lang="php">$this->assertNotIdentical($expected, $actual) | <syntaxhighlight lang="php">$this->assertNotSame($expected, $actual) | |
| <syntaxhighlight lang="php">$this->assertTrue($actual) | <syntaxhighlight lang="php">$this->assertTrue((bool)$actual) <syntaxhighlight lang="php">$this->assertNotEmpty($actual) |
SimpleTest converts parameter to bool |
| <syntaxhighlight lang="php">$this->assertFalse($actual) | <syntaxhighlight lang="php">$this->assertFalse((bool)$actual) <syntaxhighlight lang="php">$this->assertEmpty($actual) |
SimpleTest converts parameter to bool |
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'classname') | <syntaxhighlight lang="php">$this->assertInstanceOf('classname', $actual) | objects instances only |
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'array') | <syntaxhighlight lang="php">$this->assertEquals('array', gettype($actual)) | arrays only |
| <syntaxhighlight lang="php">$this->assertPattern($pattern, $string) | <syntaxhighlight lang="php">$this->assertRegExp($pattern, $string) | |
| <syntaxhighlight lang="php">$this->assertNoPattern($pattern, $string) | <syntaxhighlight lang="php">$this->assertNotRegExp($pattern, $string) | |
| <syntaxhighlight lang="php">$this->expectException(true) | <syntaxhighlight lang="php">$this->setExpectedException('exception_classname') | alternatively use phpdocs: @expectedException exception_classname |
| <syntaxhighlight lang="php">$this->expectError() | <syntaxhighlight lang="php">$this->setExpectedException('PHPUnit_Framework_Error') | alternatively use phpdocs: @expectedException PHPUnit_Framework_Error |
| various HTML expectations | <syntaxhighlight lang="php">$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).