PHPUnit: phpunit. In the initial test of PHPUnit, phpunit first tests addition and subtraction, checks the environment, and calls a function to test the server name. Source code: 1classDemoControllerextendsThinkController2 {3 PHPUnit initial test, phpunit
First, test 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 4/*** 5 * @ assert (5, 8) = 13 6 * @ assert (16, 76) = 92 7 * @ assert (6, 16) = 32 8 * @ assert (6, 4) = 0 9 * @ assert ('ABC', 1) = 210 * @ param int $ a11 * @ param int $ b12 * @ return int13 */14 public function plus ($ a, $ B) 15 {16 return $ a + $ B; 17} 18 19/** 20 * @ assert (14, 8) = 621 * @ assert (16, 6) = 1022 * @ assert (6, 4) = 023 * @ assert ('45', 1) = 4424 * @ param int $ a25 * @ param int $ b26 * @ return int27 */28 public function subtract ($ a, $ B) 29 {30 return $ a-$ B; 31} 32 33 public function connectToServer ($ serverName = null) 34 {35 if ($ serverName = null) {36 throw new Exception ("This is not a server name"); 37} 38 $ fp = fsockopen ($ serverName, 8080); 39 return ($ fp )? True: false; 40} 41 42 43}
Generate a test file:
1 class DemoControllerTest extends \PHPUnit_Framework_TestCase 2 { 3 4 /** 5 * @var DemoController 6 */ 7 protected $object; 8 9 /** 10 * Sets up the fixture, for example, opens a network connection. 11 * This method is called before a test is executed. 12 */ 13 protected function setUp() 14 { 15 $this->object = new DemoController; 16 } 17 18 /** 19 * Tears down the fixture, for example, closes a network connection. 20 * This method is called after a test is executed. 21 */ 22 protected function tearDown() 23 { 24 25 } 26 27 /** 28 * Generated from @assert (5, 8) == 13. 29 * 30 * @covers Home\Controller\DemoController::plus 31 */ 32 public function testPlus() 33 { 34 $this->assertEquals( 35 13, $this->object->plus(5, 8) 36 ); 37 } 38 39 /** 40 * Generated from @assert (16, 76) == 92. 41 * 42 * @covers Home\Controller\DemoController::plus 43 */ 44 public function testPlus2() 45 { 46 $this->assertEquals( 47 92, $this->object->plus(16, 76) 48 ); 49 } 50 51 /** 52 * Generated from @assert (6, 16) == 32. 53 * 54 * @covers Home\Controller\DemoController::plus 55 */ 56 public function testPlus3() 57 { 58 $this->assertEquals( 59 32, $this->object->plus(6, 16) 60 ); 61 } 62 63 /** 64 * Generated from @assert (6, 4) == 0. 65 * 66 * @covers Home\Controller\DemoController::plus 67 */ 68 public function testPlus4() 69 { 70 $this->assertEquals( 71 0, $this->object->plus(6, 4) 72 ); 73 } 74 75 /** 76 * Generated from @assert ('abc', 1) == 0. 77 * 78 * @covers Home\Controller\DemoController::plus 79 */ 80 public function testPlus5() 81 { 82 $this->assertEquals( 83 2, $this->object->plus('abc', 1) 84 ); 85 } 86 87 /** 88 * Generated from @assert (14, 8) == 6. 89 * 90 * @covers Home\Controller\DemoController::subtract 91 */ 92 public function testSubtract() 93 { 94 $this->assertEquals( 95 6, $this->object->subtract(14, 8) 96 ); 97 } 98 99 /**100 * Generated from @assert (16, 6) == 10.101 *102 * @covers Home\Controller\DemoController::subtract103 */104 public function testSubtract2()105 {106 $this->assertEquals(107 10, $this->object->subtract(16, 6)108 );109 }110 111 /**112 * Generated from @assert (6, 4) == 0.113 *114 * @covers Home\Controller\DemoController::subtract115 */116 public function testSubtract3()117 {118 $this->assertEquals(119 0, $this->object->subtract(6, 4)120 );121 }122 123 /**124 * Generated from @assert ('abc', 1) == 0.125 *126 * @covers Home\Controller\DemoController::subtract127 */128 public function testSubtract4()129 {130 $this->assertEquals(131 44, $this->object->subtract('45', 1)132 );133 }134 135 /**136 * @covers Home\Controller\DemoController::connectToServer137 * @todo Implement testConnectToServer().138 */139 public function testConnectToServer()140 {141 // // Remove the following lines when you implement this test.142 // $this->markTestIncomplete(143 // 'This test has not been implemented yet.'144 // );145 $serverName = 'wwwcom';146 $this->assertTrue($this->object->connectToServer($serverName) === false);147 }148 public function testConnectToServer2()149 {150 $serverName = 'www.baidu.com';151 $this->assertTrue($this->object->connectToServer($serverName) !== false);152 }
The server test cases here are manually added!
Execution result:
1 .. FFF .. F .. F 11/11 (100%) 2 3 Time: 44.42 seconds, Memory: 8.75 Mb 4 5 There were 5 failures: 6 7 1) Home \ Controller \ DemoControllerTest :: testPlus3 8 Failed asserting that 22 matches expected 32. 9 10 D: \ wamp \ www \ wxportal \ tests \ Application \ Home \ Controller \ DemoController. classTest. php: 6711 12 2) Home \ Controller \ DemoControllerTest: testPlus413 Failed asserting that 10 matches expected 0.14 15 D: \ wam P \ www \ wxportal \ tests \ Application \ Home \ Controller \ DemoController. classTest. php: 7916 17 3) Home \ Controller \ DemoControllerTest: testPlus518 Failed asserting that 1 matches expected 2.19 20 D: \ wamp \ www \ wxportal \ tests \ Application \ Home \ Controller \ DemoController. classTest. php: 9121 22 4) Home \ Controller \ DemoControllerTest: testSubtract323 Failed asserting that 2 matches expected 0.24 25 D: \ wamp \ www \ wxport Al \ tests \ Application \ Home \ Controller \ DemoController. classTest. php: 12726 27 5) Home \ Controller \ DemoControllerTest: testConnectToServer228 Failed asserting that false is true.29 30 D: \ wamp \ www \ wxportal \ tests \ Application \ Home \ Controller \ DemoController. classTest. php: 15831 32 FAILURES! 33 Tests: 11, Assertions: 11, Failures: 5.34 completed.
Worker Tests addition and subtraction, checks the environment, and calls a function to test the server name. Source code: 1 class DemoController extends \ Think \ Controller 2 {3...