PHPµ¥Ôª²âÊÔÀûÆ÷ PHPUNITÉîÈëÓ÷¨(Èý)µÚ1/2Ò³

5年以前  |  阅读数:389 次  |  编程语言:PHP 

ÔÚ±¾ÎÄÖУ¬±ÊÕß½«Îª´o¼Ò½eÉÜphpunitÖеÄÁ½¸o¸ß¼¶¸ÅÄiºÍÓ裬¾¡¹ÜËu²»Ò»¶¨ÔÚÄaµÄÈÕ³£µ¥Ôª²aÊÔÖж¼Óõ½£¬µ«Ài½aºÍѧ»aËuÃǵÄÓè¶Ôѧϰphpunit»¹ÊÇÊ®*ÖÖØÒªµÄ¡£

¡¡¡¡PhpunitÖеÄAnnotations

¡¡¡¡Èç¹uÓÐÆaËu±a³ÌÓiÑÔ¾­ÑeµÄ¿ª*¢Õߣ¬Ó¦¸Ã¶ÔAnnotations(×¢½a)²»Ä°Éu£¬ÆaʵÔÚphpunitÖУ¬Ò»¸o¼oµ¥µÄÈçÏÂÃaeµÄÒ»¶Î×¢ÊÍÒ²¿ÉÒÔÈÏΪÊÇAnnotations£º

<?php
class MyTestClass extends PHPUnit_Framework_TestCase
{
/*
Testing the answer to ¡°do you love unit tests?¡±
*/
public function testDoYouLoveUnitTests()
{
$love = true;
$this->assertTrue($love);
}
}
?>

¡¡¡¡¿ÉÒÔ¿´µ½£¬Æaʵһ¶ÎÒÔ/ /Ϊ±e¼ÇµÄÎÄ×Ö£¬¾Í¿ÉÒÔÈÏΪÊÇÒ»ÖÖAnnotations£¬µ«AnnotationsÆaʵ²»µ¥µ¥ÊǼoµ¥µÄ×¢ÊÍ£¬ËuÊÇÓeÒ»¸o³ÌÐoÔªËØÏa¹ØÁªÐÅÏ¢»oÕßÔªÊý¾ÝµÄ±e×¢£¬Ëu²»Ó°Ïi³ÌÐoµÄÔËÐУ¬µ«Ïa¹ØµÄÈi¼þ¹¤¾ß»o¿o¼ÜÄܹ»½«Æaת»»³ÉÌØÊaµÄÔªÊý¾Ý±e¼Ç£¬ÒÔ½±a¿ª¢ÕßÒÔ¸uÉٵĴuÂeÈ¥Ìa¸ßЧÂÊ(±ÈÈçͨ¹ý¡£Èç¹uÄaÊiϤJava£¬Ôo»a*¢ÏÖÔÚJava SE 5Öм°ÏoSpringµÈ¿o¼ÜÖУ¬¶¼´oÁ¿Ê¹ÓÃÁËAnnotations¡£

¡¡¡¡È»¶ø£¬ÓÉÓÚphp²¢²»ÏoJavaÄÇÑuÊDZaÒeÐÔÓiÑÔ£¬Òo´Ë±¾Éiȱ*¦È¥½aÎoAnnotationsµÄ»uÖÆ£¬µ«ÐÒºÃphpunitÈ¥Ìa¹(C)ÁËÕaÑuµÄ¹¦ÄÜ£¬ÎÒÃÇÒÔÏÂÃaeµÄ´uÂeΪÀý£º

<?php
class MyMathClass
{
/*
Add two given values together and return sum
*/
public function addValues($a,$b)
{
return $a+$b;
}
}
?>

¡¡¡¡ÉÏÃaeµÄÖ»ÊÇÒ»¸o¼oµ¥µÄ¼Ó¨µÄÀý×Ó£¬Îª´Ë£¬ÎÒÃÇʹÓÃAnnotationsÈ¥±aдһ¸oµ¥Ôª²aÊÔ£¬ÔÚÉÏÁ½ÆªÎÄÕÂÖУ¬ÎÒÃDzÉÓõÄÊÇÊÖ¹¤±aдµ¥Ôª²aÊԵĽ¨£¬¶ø±¾ÎÄÖУ¬½«½eÉÜʹÓÃphpunitÃuÁiÐеĽ¨£¬×Ô¶¯Éu³Éµ¥Ôª²aÊԵĿo¼Ü£¬½*¨ÈçÏ£º

¡¡¡¡Ê×ÏÈ°ÑÉÏÃaeµÄÀa±£´aeΪMyMathClass.php,È»ºoÔÚÃuÁiÐÐÏÂÔËÐÐÈçÏÂÃuÁi£º

phpunit ¨Cskeleton-test MyMathClass

¡¡¡¡Õaʱphpunit»a×Ô¶¯Éu³ÉÈçϵĿo¼Üµ¥Ôª²aÊÔ´uÂe£º

<?php
require_once '/path/to/MyMathClass.php';
/
Test class for MyMathClass.
Generated by PHPUnit on 2011-02-07 at 12:22:07.
*/
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.
*/
protected function setUp()
{
$this->object = new MyMathClass;
}
/

Tears down the fixture, for example, closes a network connection.
This method is called after a test is executed.
*/
protected function tearDown()
{
}
/*
@todo Implement testAddValues().
*/
public function testAddValues()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
?>

¡¡¡¡¿ÉÒÔ¿´µ½£¬phpunitΪÎÒÃÇÉu³ÉµÄµ¥Ôª²aÊÔ´uÂe×Ô¶¯ÒýÈeÁËÔ­À´µÄMyMathClass.php,ͬʱҲÉu³ÉÁËsetUpºÍtearDown½¨£¬µ«Î¨Ò»µÄºËÐĵ¥Ôª²aÊÔ´uÂeÊÇÁo¸øÁËÎÒÃDZaд¡£Èç¹uÏeÔÚÕa¸o»u´¡Éϸu¿iËÙµÄÉu³ÉÎÒÃÇÏeÒªµÄµ¥Ôª²aÊÔ´uÂe£¬ÒªÈçºÎʵÏÖÄØ?û´i£¬¾ÍÊÇʹÓÃannotations!ÎÒÃÇ¿ÉÒÔÔÚÔ­À´µÄMyMathClass.phpÖмÓÈeÈçϵÄannotations¡£

<?php
class MyMathClass
{
/*
Add two given values together and return sum
@assert (1,2) == 3
/
public function addValues($a,$b)
{
return $a+$b;
}
}
?>

¡¡¡¡È»ºoÔÙÏoÉÏÊoÒ»ÑuÔÚÃuÁiÐÐÔËÐУº

phpunit ¨Cskeleton-test MyMathClass

¡¡¡¡Õa¸oʱºo»aΪÎÒÃÇÉu³ÉÈçϵĵ¥Ôª²aÊÔ´uÂe£º

<?php
/*
Generated from @assert (1,2) == 3.
*/
public function testAddValues()
{
$this->assertEquals(
3,
$this->object->addValues(1,2)
);
}
?>

¡¡¡¡¿´µ½ÁËô?ÎÒÃÇÔÚÔ­ÓеÄÀaÖмÓÈeÁË×¢½a@assert(1,2)==3,Ôophpunit×Ô¶¯ÎªÎÒÃÇÉu³ÉÁËÕýÈ*µÄµ¥Ôª²aÊÔ´uÂe¡£µ±È»£¬¿ÉÒԲο¼phpunitÊÖ²a£¬Ñ§Ï°µ½¸u¶aµÄ¹ØÓÚ@assert×¢½aʹÓõĹaeÔo¡£

¡¡¡¡ÏÂÃaeÔÙ¾ÙÒ»¸oÀý×ÓÀ´½²½aannotations¡£¼ÙÉeÎÒÃǵijÌÐoÖеÄÒ»¸o½¨£¬Ö»ÊǽoÐeÒªÊý¾ÝµÄÊaÈe£¬²¢ÇÒ²»ÒÀÀµXML»oÕßÊý¾Ý¿aÌa¹(C)Êý¾ÝÔ´£¬ÔoΪÁ˲aÊÔÕa¸o½¨£¬ÎÒÃÇ¿ÉÄÜÏeµ½µÄÒ»¸o½¨ÊÇÔÚ³ÌÐoÖÐÉeÖÃÒ»¸o²aÊÔÊý¾Ý¼¯È¥²aÊÔ£¬µ«ÕaÀi½eÉÜÒ»¸o±È½Ï¼oµ¥µÄ½¨£¬¾ÍÊÇʹÓÃ×¢½a@dataProvider£¬ÐÞ¸ÄMyMathClass.phpÈçÏ£º

<?php
/
Data provider for test methods below
/
public static function provider()
{
return array(
array(1,2,3),
array(4,2,6),
array(1,5,7)
);
}
/

Testing addValues returns sum of two values
@dataProvider provider
*/
public function testAddValues($a,$b,$sum)
{
$this->assertEquals(
$sum,
$this->object->addValues($a,$b)
);
}
?>

¡¡¡¡¿ÉÒÔ¿´µ½£¬ÕaÀiʹÓÃÁË×¢½a@dataProvider,Ö¸Ã÷Á˲aÊÔÓÃÀýµÄÊý¾ÝÌa¹(C)ÕßÊÇÓÉprovider½¨*µ»ØµÄÒ»¸oÊý×e¡£ËuÒÔÔÚµ¥Ôª²aÊÔʱ£¬Êý×eÖеĵÚ0¸oÔªËØÔo»a¸³Öµ¸ø$a,µÚ1¸oÔªËØÔo»a¸³Öµ¸øb£¬µÚ3¸oÔªËØÔo»a¸³Öµ¸øsum£¬¿ÉÒÔ¿´µ½£¬ÉÏÃaeµÄµÚ3¸oÊý×eÌa¹(C)µÄÊý¾ÝÊDz»ÄÜͨ¹ýµ¥Ôª²aÊԵģ¬ÒoΪ1+5²»µÈÓÚ7¡£

¡¡¡¡´ËÍa£¬ÕaÀi»¹¼oµ¥½eÉÜÁ½¸o³£ÓõÄannotations,±ÈÈç@expectedException×¢½a¿ÉÒÔ²aÊÔ´uÂeÖÐÊÇñÕýÈÅ׳oÁËÒi³££¬±ÈÈ磺

<?phprequire_once 'PHPUnit/Framework.php';
class ExceptionTest extends PHPUnit_Framework_TestCase{
/*
@expectedException InvalidArgumentException */
public function testException() {
}
}

?>

¡¡¡¡ÕaÀi¾ÍÓÃ×¢½aµÄ½¨±iʾtestExceptionÖбØÐeÅ׳oµÄÒi³£ÀaÐÍΪInvalidArgumentException¡£

¡¡¡¡ÁiÍaÒ»¸oÊÇ@cover×¢½a¡£ËuµÄ×÷ÓÃÊDZeʶphpunitֻΪÀaÖеÄÄÄÐ(C)½¨»o×÷ÓÃÓoÉu³É²aÊÔ´uÂe£¬±ÈÈ磺

/*
@covers SampleClass::publicMethod
@covers SampleClass::<!public>
@covers HelperClass
*/
public function testMethod()
{
$result = SampleClass::method();
}

¡¡¡¡ÔophpunitֻΪSampleClassÀaÖеÄpublicMethod½¨¡¢SampleClassÀaÖеÄËuÓÐÇpublicÉuÃ÷µÄ½*¨ºÍHelperClassÀa»oÕßËuµÄÆaÖÐÒ»¸o¸¸Àa²uÉuµ¥Ôª²aÊÔ´uÂe¡£

µ±Ç°1/2Ò³ 12ÏÂÒ»Ò³ÔĶÁÈ«ÎÄ

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例