Note:

This site is no longer used and is in read-only mode. Instead please go to our new Moodle Developer Resource site.

SimpleTest conversion: Difference between revisions

From MoodleDocs
m Text replacement - "class="nicetable"" to "class="wikitable""
m Text replacement - "</code>" to "</syntaxhighlight>"
Line 7: Line 7:
# 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 <syntaxhighlight lang="php">extends UnitTestCase</code> with <syntaxhighlight lang="php">extends basic_testcase</code> and  <syntaxhighlight lang="php">extends UnitTestCaseUsingDatabase</code> with <syntaxhighlight lang="php">extends advanced_testcase</code>
# 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() and tearDown() to be protected
# fix setUp() and tearDown() to be protected
# fix assert syntax
# fix assert syntax
# add <syntaxhighlight lang="php">$this->resetAfterTest();</code> in advanced test cases that modify database
# add <syntaxhighlight lang="php">$this->resetAfterTest();</syntaxhighlight> in advanced test cases that modify database
# add missing <syntaxhighlight lang="php">global $CFG;</code> 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</code> must be replaced with <syntaxhighlight lang="php">PHPUNIT_TEST</code>
# 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.
Usually the tests using database can be significantly simplified, the performance in PHPUnit is also a lot better.
Line 27: Line 27:
! Notes
! Notes
|-
|-
| <syntaxhighlight lang="php">$this->assertEqual($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertEqual($expected, $actual)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertEquals($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertEquals($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->assertNotEqual($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotEqual($expected, $actual)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertNotEquals($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotEquals($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->assertWithinMargin($expected, $actual, $margin)</code>
| <syntaxhighlight lang="php">$this->assertWithinMargin($expected, $actual, $margin)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertEquals($expected, $actual, '', $margin)</code>
| <syntaxhighlight lang="php">$this->assertEquals($expected, $actual, '', $margin)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->assertIdentical($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertIdentical($expected, $actual)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertSame($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertSame($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->assertNotIdentical($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotIdentical($expected, $actual)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertNotSame($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotSame($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->assertTrue($actual)</code>
| <syntaxhighlight lang="php">$this->assertTrue($actual)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertTrue((bool)$actual)</code><br /><syntaxhighlight lang="php">$this->assertNotEmpty($actual)</code>
| <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)</code>
| <syntaxhighlight lang="php">$this->assertFalse($actual)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertFalse((bool)$actual)</code><br /><syntaxhighlight lang="php">$this->assertEmpty($actual)</code>
| <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')</code>
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'classname')</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertInstanceOf('classname', $actual)</code>
| <syntaxhighlight lang="php">$this->assertInstanceOf('classname', $actual)</syntaxhighlight>
| objects instances only
| objects instances only
|-
|-
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'array')</code>
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'array')</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertEquals('array', gettype($actual))</code>
| <syntaxhighlight lang="php">$this->assertEquals('array', gettype($actual))</syntaxhighlight>
| arrays only
| arrays only
|-
|-
| <syntaxhighlight lang="php">$this->assertPattern($pattern, $string)</code>
| <syntaxhighlight lang="php">$this->assertPattern($pattern, $string)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertRegExp($pattern, $string)</code>
| <syntaxhighlight lang="php">$this->assertRegExp($pattern, $string)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->assertNoPattern($pattern, $string)</code>
| <syntaxhighlight lang="php">$this->assertNoPattern($pattern, $string)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->assertNotRegExp($pattern, $string)</code>
| <syntaxhighlight lang="php">$this->assertNotRegExp($pattern, $string)</syntaxhighlight>
|
|
|-
|-
| <syntaxhighlight lang="php">$this->expectException(true)</code>
| <syntaxhighlight lang="php">$this->expectException(true)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->setExpectedException('exception_classname')</code>
| <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()</code>
| <syntaxhighlight lang="php">$this->expectError()</syntaxhighlight>
| <syntaxhighlight lang="php">$this->setExpectedException('PHPUnit_Framework_Error')</code>
| <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()</code>
| <syntaxhighlight lang="php">$this->assertTag()</syntaxhighlight>
|
|
|}
|}

Revision as of 20:25, 14 July 2021

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:

  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() and tearDown() to be protected
  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
  9. Usages of
    $UNITTEST->running
    
    must be replaced with
    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).