SimpleTest conversion: Difference between revisions
From MoodleDocs
Line 54: | Line 54: | ||
| <code php>$this->assertEquals('array', gettype($actual)</code> | | <code php>$this->assertEquals('array', gettype($actual)</code> | ||
| Other data types. | | Other data types. | ||
|- | |||
| <code php>$this->assertPattern($pattern, $string)</code> | |||
| <code php>$this->assertRegExp($pattern, $string)</code> | |||
| | |||
|- | |||
| <code php>$this->assertNoPattern($pattern, $string)</code> | |||
| <code php>$this->assertNotRegExp($pattern, $string)</code> | |||
| | |||
|- | |- | ||
| <code php>$this->expectException(true)</code> | | <code php>$this->expectException(true)</code> | ||
Line 61: | Line 69: | ||
| <code php>$this->expectError()</code> | | <code php>$this->expectError()</code> | ||
| <code php>$this->setExpectedException('PHPUnit_Framework_Error')</code> | | <code php>$this->setExpectedException('PHPUnit_Framework_Error')</code> | ||
| | |||
|- | |||
| various HTML expectations | |||
| <code php>$this->setAssertTag()</code> | |||
| | | | ||
|} | |} |
Revision as of 20:00, 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 UnitTestCase
withextends basic_testcase
andextends UnitTestCaseUsingDatabase
withextends 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)$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->assertPattern($pattern, $string)
|
$this->assertRegExp($pattern, $string)
|
|
$this->assertNoPattern($pattern, $string)
|
$this->assertNotRegExp($pattern, $string)
|
|
$this->expectException(true)
|
$this->setExpectedException('exception_name')
|
|
$this->expectError()
|
$this->setExpectedException('PHPUnit_Framework_Error')
|
|
various HTML expectations | $this->setAssertTag()
|