Note:

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

SimpleTest conversion: Difference between revisions

From MoodleDocs
mNo edit summary
 
(24 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:obsolete}}
{{Moodle 2.3}}
=Overview=
=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:
The migration is pretty straightforward:
# 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 <code>extends UnitTestCase</code> with <code>extends basic_testcase</code> and  <code>extends UnitTestCaseUsingDatabase</code> with <code>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(), tearDown()
# fix setUp() and tearDown() to be protected
# fix assert syntax
# fix assert syntax
# add $this->resetAfterTest() in advanced test cases that modify database
# add <syntaxhighlight lang="php">$this->resetAfterTest();</syntaxhighlight> in advanced test cases that modify database
# add missing global $CFG 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</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.
 
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=
=Assert differences=


{| class="nicetable"
{| class="wikitable"
|-
|-
! SimpleTest
! SimpleTest
Line 19: Line 28:
! Notes
! Notes
|-
|-
| <code php>$this->assertEqual($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertEqual($expected, $actual)</syntaxhighlight>
| <code php>$this->assertEquals($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertEquals($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <code php>$this->assertNotEqual($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotEqual($expected, $actual)</syntaxhighlight>
| <code php>$this->assertNotEquals($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotEquals($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <code php>$this->assertWithinMargin($expected, $actual, $margin)</code>
| <syntaxhighlight lang="php">$this->assertWithinMargin($expected, $actual, $margin)</syntaxhighlight>
| <code php>$this->assertEquals($expected, $actual, '', $margin)</code>
| <syntaxhighlight lang="php">$this->assertEquals($expected, $actual, '', $margin)</syntaxhighlight>
|
|
|-
|-
| <code php>$this->assertIdentical($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertIdentical($expected, $actual)</syntaxhighlight>
| <code php>$this->assertSame($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertSame($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <code php>$this->assertNotIdentical($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotIdentical($expected, $actual)</syntaxhighlight>
| <code php>$this->assertNotSame($expected, $actual)</code>
| <syntaxhighlight lang="php">$this->assertNotSame($expected, $actual)</syntaxhighlight>
|
|
|-
|-
| <code php>$this->assertTrue($actual)</code>
| <syntaxhighlight lang="php">$this->assertTrue($actual)</syntaxhighlight>
| <code php>$this->assertTrue((bool)$a, $b)</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
|-
|-
| <code php>$this->assertFalse($actual)</code>
| <syntaxhighlight lang="php">$this->assertFalse($actual)</syntaxhighlight>
| <code php>$this->assertFalse((bool)$a, $b)</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
|-
|-
| <code php>$this->assertIsA($actual, $classname)</code>
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'classname')</syntaxhighlight>
| <code php>$this->assertInstanceOf($classname, $actual)</code>
| <syntaxhighlight lang="php">$this->assertInstanceOf('classname', $actual)</syntaxhighlight>
| Only objects.
| objects instances only
|-
|-
| <code php>$this->assertIsA($actual, 'array')</code>
| <syntaxhighlight lang="php">$this->assertIsA($actual, 'array')</syntaxhighlight>
| <code php>$this->assertEquals('array', gettype($actual)</code>
| <syntaxhighlight lang="php">$this->assertEquals('array', gettype($actual))</syntaxhighlight>
| Other data types.
| arrays only
|-
|-
| <code php>$this->expectException(true)</code>
| <syntaxhighlight lang="php">$this->assertPattern($pattern, $string)</syntaxhighlight>
| <code php>$this->setExpectedException('exception_name')</code>
| <syntaxhighlight lang="php">$this->assertRegExp($pattern, $string)</syntaxhighlight>
|
|
|-
|-
| <code php>$this->expectError()</code>
| <syntaxhighlight lang="php">$this->assertNoPattern($pattern, $string)</syntaxhighlight>
| <code php>$this->setExpectedException('PHPUnit_Framework_Error')</code>
| <syntaxhighlight lang="php">$this->assertNotRegExp($pattern, $string)</syntaxhighlight>
|
|-
| <syntaxhighlight lang="php">$this->expectException(true)</syntaxhighlight>
| <syntaxhighlight lang="php">$this->setExpectedException('exception_classname')</syntaxhighlight>
| alternatively use phpdocs: @expectedException exception_classname
|-
| <syntaxhighlight lang="php">$this->expectError()</syntaxhighlight>
| <syntaxhighlight lang="php">$this->setExpectedException('PHPUnit_Framework_Error')</syntaxhighlight>
| alternatively use phpdocs: @expectedException PHPUnit_Framework_Error
|-
| various HTML expectations
| <syntaxhighlight lang="php">$this->assertTag()</syntaxhighlight>
|
|
|}
|}


=SimpleTest emulation=
=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).




[[Category:Unit testing]]
[[Category:Unit testing]]

Latest revision as of 16:28, 30 April 2024

Warning: This page is no longer in use. The information contained on the page should NOT be seen as relevant or reliable.

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).