phpunit¹¦ÄܵãÕûÀí
3988 µã»÷¡¤0 »ØÌû
![]() | ![]() | |
![]() | ÉèÖó¡¾° ´´½¨Êý×éFixtures [php] protected function setUp() { // ´´½¨Êý×éfixture¡£ $this->fixture = array(); } ¡°Ì×¼þ¼¶×°ÅäÆ÷¡± ¹²Ïífixture¼´sharedFixture PHPUnit_Framework_TestSuite¶ÔÏóµÄ$sharedFixtureÊôÐÔÔÚPHPUnit_Framework_TestSuite¶ÔÏ󼯺ϼ°PHPUnit_Framework_TestCase¶ÔÏóÖж¼¿ÉÓᣠ[php] protected function setUp() { $this->sharedFixture = new PDO( 'mysql:host=wopr;dbname=test', 'root', '' ); } providerÊý¾ÝÌṩÕß Ê¹ÓÃÊý¾ÝÌṩÕß ×éÖ¯²âÊÔÌ×¼þ PHPUnit¿ò¼ÜµÄPHPUnit_Framework_TestSuiteÀàÔÊÐíÎÒÃǽ«Ò»Ð©²âÊÔ×éÖ¯ÔÚÈô¸É²âÊÔÌ×¼þ¹¹³ÉµÄÒ»¸ö²ã´Î½á¹¹ÖС£ÈÃÎÒÃÇͨ¹ýÒ»¸öÀý×Ó¿´¿´PHPUnitÌØÓеIJâÊÔÌ×¼þ¡£ ·¶Àý 7.1ÏÔʾһ¸öɾ½Ú°æ±¾µÄTests/AllTests.php£¬·¶Àý 7.2ÏÔʾһ¸öɾ½Ú°æ±¾µÄTests/Framework/AllTests.php¡£ µÚÒ»¼¶£º [php] <?php if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'AllTests::main'); } require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'Framework/AllTests.php'; // ... class AllTests { public static function main() { PHPUnit_TextUI_TestRunner::run(self::suite()); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('PHPUnit'); $suite->addTest(Framework_AllTests::suite()); // ... return $suite; } } if (PHPUnit_MAIN_METHOD == 'AllTests::main') { AllTests::main(); } ?> µÚ¶þ¼¶£º [php] <?php if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'Framework_AllTests::main'); } require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'Framework/AssertTest.php'; // ... class Framework_AllTests { public static function main() { PHPUnit_TextUI_TestRunner::run(self::suite()); } public static function suite() { $suite = new PHPUnit_Framework_TestSuite('PHPUnit Framework'); $suite->addTestSuite('Framework_AssertTest'); // ... return $suite; } } if (PHPUnit_MAIN_METHOD == 'Framework_AllTests::main') { Framework_AllTests::main(); } ?> µÚÈý¼¶£º [php] <?php /** * PHPunit²âÊÔÌ×¼þ * /tests/Framework/Framework/AssertTest.php * @anthor Chen Wei Han * @copyright 2011-7-6ÏÂÎç02:10:29 * @package phpunit * @todo */ //require_once 'PHPUnit/Framework.php'; class Framework_Framework_AssertTest extends PHPUnit_Framework_TestCase{ public function testNewArrayIsEmpty() { // ´´½¨Êý×éfixture¡£ $fixture = array(); // ¶ÏÑÔÊý×éfixtureµÄ³ß´çÊÇ0¡£ $this->assertEquals(0, sizeof($fixture)); } public function testArrayContainsAnElement() { // ´´½¨Êý×éfixture¡£ $fixture = array(); // ÏòÊý×éfixtureÔö¼ÓÒ»¸öÔªËØ¡£ $fixture[] = 'Element'; //¶ÏÑÔÊý×éfixtureµÄ³ß´çÊÇ1¡£ $this->assertEquals(1, sizeof($fixture)); } } ?> ÀàFramework_AssertTestÊǸöÀ©Õ¹ÁËPHPUnit_Framework_TestCaseµÄ±ê×¼²âÊÔÓÃÀý¡£ ÔËÐÐTests/AllTests.phpÔòʹÓÃTextUI²âÊÔÆô¶¯Æ÷ÔËÐÐÈ«²¿²âÊÔ£¬È»¶øÔËÐÐTests/Framework/AllTests.phpÔòÖ»ÔËÐÐÀàPHPUnit_Framework_*µÄ²âÊÔ¡£ Ì×¼þ¼¶×°ÅäÆ÷ ÀàPHPUnit_Framework_TestSuiteÌṩÁ½¸öÄ£°å·½·¨£¬setUp()ºÍtearDown()£¬ËüÃÇ·Ö±ðÔÚ²âÊÔÌ×¼þµÄÊ׸ö²âÊÔǰºÍ×îºó²âÊÔºó±»µ÷Óᣠ[php] <?php require_once 'MyTest.php'; class MySuite extends PHPUnit_Framework_TestSuite { public static function suite() { return new MySuite('MyTest'); } protected function setUp() { print "\nMySuite::setUp()"; } protected function tearDown() { print "\nMySuite::tearDown()"; } } ?> δÍê³ÉºÍÌø¹ýµÄ²âÊÔ public function testSomething() { } Èç¹ûÎÒÃǷֱ𽫳ɹ¦µÄ²âÊÔºÍʧ°ÜµÄ±Ø×öÂÌµÆºÍºìµÆ£¬ÎÒÃÇ»¹ÐèÒª»ÆµÆ±ê¼ÇδÍê³É»òδʵÏֵIJâÊÔ¡£PHPUnit_Framework_IncompleteTestÊǸö±ê¼Ç½Ó¿Ú£¬ÓÃÓÚ±ê¼Çµ±²âÊÔ½á¹ûΪδÍê³É»òµ±Ç°Î´ÊµÏÖʱÒý·¢µÄÒì³£¡£ [php] <?php require_once 'PHPUnit/Framework.php'; class SampleTest extends PHPUnit_Framework_TestCase { public function testSomething() { //¿ÉÑ¡£ºËæ±ã²âÊÔʲô¶¼¿ÉÒÔ¡£ $this->assertTrue(TRUE, 'This should already work.'); // ÔÚÕâ¶ùͣס²¢½«²âÊÔ±ê¼ÇΪδÍê³É¡£ $this->markTestIncomplete( 'This test has not been implemented yet.' ); } } ?> Ìø¹ýµÄ²âÊÔ ÌØ¶¨µÄ»·¾³Öв¢·ÇËùÓеIJâÊÔ¶¼ÄÜÔËÐС£¿¼ÂǸöÀý×Ó£¬Ò»¸ö¾ßÓжà¸öÇý¶¯ÒÔÖ§³Ö²»Í¬Êý¾Ý¿âϵͳµÄÊý¾Ý¿âÌáÈ¡²ã¡£MySQLÇý¶¯µÄ²âÊÔµ±È»Ö»ÄÜÔÚMySQL·þÎñÆ÷ÉÏÔËÐС£ $this->markTestSkipped [php] <?php require_once 'PHPUnit/Framework.php'; class DatabaseTest extends PHPUnit_Framework_TestCase { protected function setUp() { if (!extension_loaded('mysqli')) { $this->markTestSkipped( 'The MySQLi extension is not available.' ); } } public function testConnection() { // ... } } ?> PHPUnit_Framework_TestResult µ±ÄãÔÚÔËÐÐËùÓÐÕâЩ²âÊÔʱ£¬ÄãÐèÒªÔÚij´¦´æ´¢ËùÓнá¹û£ºÔËÐÐÁ˶àÉÙ²âÊÔ£¬Äĸöʧ°ÜÁË£¬ÒÔ¼°ËûÃǺÄʱ¶à¾Ã¡£ PHPUnit×Ô´øÁ½¸ö¾ßÌåµÄ²âÊÔ×°ÊÎÕߣºPHPUnit_Extensions_RepeatedTestºÍPHPUnit_Extensions_TestSetup¡£Ç°Ò»¸öÓÃÓÚÖØ¸´ÔËÐÐÒ»¸ö²âÊÔ£¬²¢ÇÒÖ»µ±ËùÓеü´ú¶¼³É¹¦Ê±²ÅËã³É¹¦¡£ºóÃæÒ»¸öÔÚµÚ 6 ÕÂÖÐÌÖÂÛ¹ý¡£ Òª¶¨ÖÆPHPUnit_Framework_TestResult£¬Ã»±ØÒª±àдËüµÄÕû¸ö×ÓÀà¡£´ó¶àʱºò£¬ÊµÏÖÒ»¸öÐÂPHPUnit_Framework_TestListener£¨¼û±í 22.14£©²¢ÔÚÔËÐвâÊÔǰ¸½ÔÚPHPUnit_Framework_TestResult¶ÔÏóÉϾ͹»ÁË¡£ ·¶Àý 23.4: ÔËÐк͹۲â²âÊÔÌ×¼þ [php] www.atcpu.com <?php require_once 'PHPUnit/Framework.php'; require_once 'ArrayTest.php'; require_once 'SimpleTestListener.php'; // ´´½¨Ò»¸ö°üÀ¨²âÊÔÌ×¼þ£¬À´×ÔÀàArrayTestµÄ²âÊÔ¡£ $suite = new PHPUnit_Framework_TestSuite('ArrayTest'); // ´´½¨Ò»¸ö²âÊÔ½á¹û£¬²¢¸½ÉÏÒ»¸öSimpleTestListener¶ÔÏó×÷Ϊ¶ÔËüµÄ¹Û²âÕß¡£ $result = new PHPUnit_Framework_TestResult; $result->addListener(new SimpleTestListener); // ÔËÐвâÊÔ¡£ $suite->run($result); ?> | |
![]() | ![]() |