Note:

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

SimpleTest conversion

From MoodleDocs

Overview

The migration is pretty straightforward:

  1. create new test file in `xxx/tests/yyy_test.php`
  2. copy contents of the old test file
  3. replace extends UnitTestCase with extends basic_testcase and extends UnitTestCaseUsingDatabase with extends advanced_testcase
  4. move constructor code to setUp
  5. fix setUp(), tearDown()
  6. fix assert syntax
  7. add $this->resetAfterTest() in advanced test cases that modify database
  8. add missing global $CFG for includes outside of testcase classes

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)$a, $b) SimpleTest converts parameter to bool.
$this->assertFalse($actual) $this->assertFalse((bool)$a, $b) SimpleTest converts parameter to bool.
$this->assertIsA($actual, $classname) $this->assertInstanceOf($classname, $actual) Only objects.
$this->assertIsA($actual, 'array') $this->assertEquals('array', gettype($actual) Other data types.
$this->expectException(true) $this->setExpectedException('exception_name')
$this->expectError() $this->setExpectedException('PHPUnit_Framework_Error')

SimpleTest emulation