时间:2021-07-01 10:21:17 帮助过:8人阅读
先测试了一下加减,检查一下环境,又调用函数测试了服务器名。
源代码:
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) == 2 10 * @param int $a 11 * @param int $b 12 * @return int 13 */ 14 public function plus($a, $b) 15 { 16 return $a + $b; 17 } 18 19 /** 20 * @assert (14, 8) == 6 21 * @assert (16, 6) == 10 22 * @assert (6, 4) == 0 23 * @assert ('45', 1) == 44 24 * @param int $a 25 * @param int $b 26 * @return int 27 */ 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("这不是一个服务器名"); 37 } 38 $fp = fsockopen($serverName, 8080); 39 return ($fp) ? true : false; 40 } 41 42 43 }
生成测试文件:
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::subtract 103 */ 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::subtract 115 */ 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::subtract 127 */ 128 public function testSubtract4() 129 { 130 $this->assertEquals( 131 44, $this->object->subtract('45', 1) 132 ); 133 } 134 135 /** 136 * @covers Home\Controller\DemoController::connectToServer 137 * @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 }
这里的服务器测试用例是手动加上去的!
执行结果:
1 ..FFF..F..F 11 / 11 (100%) 2 3 Time: 44.42 seconds, Memory: 8.75Mb 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:67 11 12 2) Home\Controller\DemoControllerTest::testPlus4 13 Failed asserting that 10 matches expected 0. 14 15 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:79 16 17 3) Home\Controller\DemoControllerTest::testPlus5 18 Failed asserting that 1 matches expected 2. 19 20 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:91 21 22 4) Home\Controller\DemoControllerTest::testSubtract3 23 Failed asserting that 2 matches expected 0. 24 25 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:127 26 27 5) Home\Controller\DemoControllerTest::testConnectToServer2 28 Failed asserting that false is true. 29 30 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:158 31 32 FAILURES! 33 Tests: 11, Assertions: 11, Failures: 5. 34 完成。
http://www.bkjia.com/PHPjc/1134779.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1134779.htmlTechArticlePHPUnit初试,phpunit 先测试了一下加减,检查一下环境,又调用函数测试了服务器名。 源代码: 1 class DemoController extends \Think\Controller 2 { 3...