
phpunit –skeleton-test MyMathClass
这个时候会为我们生成如下的单元测试代码:

<?php

/**

* Generated from @assert (1,2) == 3.

*/

publicfunction testAddValues()

{

$this->assertEquals(

3,

$this->object->addValues(1,2)

);

}

?>

看到了么?我们在原有的类中加入了注解@assert(1,2)==3,则phpunit自动为我们生成了正确的单元测试代码。当然,可以参考phpunit手册,学习到更多的关于@assert注解使用的规则。
下面再举一个例子来讲解annotations。假设我们的程序中的一个方法,只是仅需要数据的输入,并且不依赖XML或者数据库提供数据源,则为了测试这个方法,我们可能想到的一个方法是在程序中设置一个测试数据集去测试,但这里介绍一个比较简单的方法,就是使用注解@dataProvider,修改MyMathClass.php如下:

<?php

/**

* Data provider for test methods below

*/

publicstaticfunction provider()

{

returnarray(

array(1,2,3),

array(4,2,6),

array(1,5,7)

);

}

/**

* Testing addValues returns sum of two values

* @dataProvider provider

*/

publicfunction testAddValues($a,$b,$sum)

{

$this->assertEquals(

$sum,

$this->object->addValues($a,$b)

);

}

?>
