SimpleTest conversion: Difference between revisions
From MoodleDocs
| Line 47: | Line 47: | ||
| SimpleTest converts parameter to bool | | SimpleTest converts parameter to bool | ||
|- | |- | ||
| <code>$this->assertIsA($actual, | | <code>$this->assertIsA($actual, 'classname')</code> | ||
| <code>$this->assertInstanceOf( | | <code>$this->assertInstanceOf('classname', $actual)</code> | ||
| only objects | | only objects | ||
|- | |- | ||
Revision as of 20:11, 20 April 2012
Overview
The migration is pretty straightforward:
- create new test file in `xxx/tests/yyy_test.php`
- copy contents of the old test file
- replace
extends UnitTestCasewithextends basic_testcaseandextends UnitTestCaseUsingDatabasewithextends advanced_testcase - move constructor code to setUp
- fix setUp(), tearDown()
- fix assert syntax
- add $this->resetAfterTest() in advanced test cases that modify database
- 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)$actual)
|
SimpleTest converts parameter to bool |
$this->assertFalse($actual)
|
$this->assertFalse((bool)$actual)
|
SimpleTest converts parameter to bool |
$this->assertIsA($actual, 'classname')
|
$this->assertInstanceOf('classname', $actual)
|
only objects |
$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()
|