<?php class MyTestClass extends PHPUnit_Framework_TestCase { /** * Testing the answer to “do you love unit tests?” */ publicfunction testDoYouLoveUnitTests() { $love=true; $this->assertTrue($love); } } ?>
可以看到,其实一段以/** **/为标记的文字,就可以认为是一种Annotations,但Annotations其实不单单是简单的注释,它是与一个程序元素相关联信息或者元数据的标注,它不影响程序的运行,但相关的软件工具或框架能够将其转换成特殊的元数据标记,以方便开发者以更少的代码去提高效率(比如通过。如果你熟悉Java,则会发现在Java SE 5中及象Spring等框架中,都大量使用了Annotations。
<?php require_once'/MyMathClass.php'; class MyMathClassTest extends PHPUnit_Framework_TestCase { /** * @var MyMathClass */ protected$object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protectedfunction setUp() { $this->object=new MyMathClass; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protectedfunction tearDown() { } /** * @todo Implement testAddValues(). */ publicfunction testAddValues() { // Remove the following lines when you implement this test. $this->markTestIncomplete( 'This test has not been implemented yet.' ); } } ?>
<?php class MyMathClass { /** * Add two given values together and return sum * @assert (1,2) == 3 */ publicfunction addValues($a,$b) { return$a+$b; } } ?>
phpunit中的Annotations如果有其他编程语言(如java)经验的开发者,应该对Annotations(注解)不陌生,其实在phpunit中,一个简单的如下面的一段注释也可以认为是Annotations:&lt;?phpclass MyTestClass extends PHPUnit_Framework_TestCase{/*** Testing the answer to “do ...