PHPUnit Preliminary interview, PHPUnit
Test the Add and subtract first, check the environment, and call the function to test the server name.
Source:
1 classDemocontroller extends \think\controller2 {3 4 /**5 * @assert (5, 8) = =6 * @assert (+) = =7 * @assert (6, +) = =8 * @assert (6, 4) = = 09 * @assert (' abc ', 1) = = 2Ten * @param int $a One * @param int $b A * @return int - */ - Publicfunction Plus ($a, $b) the { - return$a +$b; - } - + /** - * @assert (8) = = 6 + * @assert (6) = = A * @assert (6, 4) = = 0 at * @assert (' 1 ') = = - * @param int $a - * @param int $b - * @return int - */ - Publicfunction Subtract ($a, $b) in { - return$a-$b; to } + - Publicfunction Connecttoserver ($serverName =NULL) the { * if($serverName = =NULL) { $ Throw NewException ("This is not a server name");Panax Notoginseng } -$fp = Fsockopen ($serverName,8080); the return($FP)?true:false; + } A the +}
To generate a test file:
1 classDemocontrollertest extends \phpunit_framework_testcase2 {3 4 /**5 * @var Democontroller6 */7 protected$Object;8 9 /**Ten * Sets up the fixture, for example, opens a network connection. One * This method was called before a test is executed. A */ - protectedfunction setUp () - { the$ This-Object=NewDemocontroller; - } - - /** + * Tears down the fixture, for example, closes a network connection. - * This method was called after a test is executed. + */ A protectedfunction TearDown () at { - - } - - /** - * Generated from @assert (5, 8) = =. in * - * @covers home\controller\democontroller::p LUs to */ + Publicfunction Testplus () - { the$ This-Assertequals ( * -, $ This-Object->plus (5,8) $ );Panax Notoginseng } - the /** + * Generated from @assert (+) = =. A * the * @covers home\controller\democontroller::p LUs + */ - Publicfunction TestPlus2 () $ { $$ This-Assertequals ( - the, $ This-Object->plus ( -, the) - ); the } - Wuyi /** the * Generated from @assert (6, +) = =. - * Wu * @covers home\controller\democontroller::p LUs - */ About Publicfunction TestPlus3 () $ { -$ This-Assertequals ( - +, $ This-Object->plus (6, -) - ); A } + the /** - * Generated from @assert (6, 4) = = 0. $ * the * @covers home\controller\democontroller::p LUs the */ the Publicfunction TestPlus4 () the { -$ This-Assertequals ( in 0, $ This-Object->plus (6,4) the ); the } About the /** the * Generated from @assert (' abc ', 1) = = 0. the * + * @covers home\controller\democontroller::p LUs - */ the Publicfunction TESTPLUS5 ()Bayi { the$ This-Assertequals ( the 2, $ This-Object->plus ('ABC',1) - ); - } the the /** the * Generated from @assert (8) = = 6. the * - * @covers home\controller\democontroller::subtract the */ the Publicfunction testsubtract () the {94$ This-Assertequals ( the 6, $ This-Object->subtract ( -,8) the ); the }98 About /** - * Generated from @assert (6) = = Ten.101 *102 * @covers home\controller\democontroller::subtract103 */104 Publicfunction TestSubtract2 () the {106$ This-Assertequals (107 Ten, $ This-Object->subtract ( -,6)108 );109 } the 111 /** the * Generated from @assert (6, 4) = = 0.113 * the * @covers home\controller\democontroller::subtract the */ the Publicfunction testSubtract3 ()117 {118$ This-Assertequals (119 0, $ This-Object->subtract (6,4) - );121 }122 123 /**124 * Generated from @assert (' abc ', 1) = = 0. the *126 * @covers home\controller\democontroller::subtract127 */ - Publicfunction TestSubtract4 ()129 { the$ This-Assertequals (131 -, $ This-Object->subtract (' $',1) the );133 }134 135 /**136 * @covers Home\controller\democontroller::connecttoserver137 * @todo Implement testconnecttoserver ().138 */139 Publicfunction Testconnecttoserver () $ {141 // //Remove The following lines when you implement this test.142 //$this->marktestincomplete (143 //' This test had not been implemented yet. '144 // );145$serverName ='wwwcom';146$ This->asserttrue ($ This-Object->connecttoserver ($serverName) = = =false);147 }148 Publicfunction TestConnectToServer2 ()149 { Max$serverName ='www.baidu.com';151$ This->asserttrue ($ This-Object->connecttoserver ($serverName)!==false); the}
The server test case here is manually added to the!
Execution Result:
1.. FFF. F.. F One/ One( -%)2 3Time:44.42Seconds, Memory:8. 75Mb4 5There were5Failures:6 7 1) Home\controller\democontrollertest::testplus38Failed asserting that AMatches expected +.9 TenD:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: the One A 2) HOME\CONTROLLER\DEMOCONTROLLERTEST::TESTPLUS4 -Failed asserting thatTenMatches expected0. - theD:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: - - - 3) HOME\CONTROLLER\DEMOCONTROLLERTEST::TESTPLUS5 -Failed asserting that1Matches expected2. + -D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: the + A 4) Home\controller\democontrollertest::testsubtract3 atFailed asserting that2Matches expected0. - -D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:127 - - 5) Home\controller\democontrollertest::testconnecttoserver2 -Failed asserting thatfalse is true. in -D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:158 to +failures! -Tests: One, assertions: One, Failures:5. theComplete.
http://www.bkjia.com/PHPjc/1134779.html www.bkjia.com true http://www.bkjia.com/PHPjc/1134779.html techarticle PHPUnit First, phpunit test the addition and subtraction, check the environment, and call the function to test the server name. Source code: 1 class Democontroller extends \think\controller 2 {3 ...