2013考研初试时间 PHPUnit初试
程序员文章站
2022-06-11 23:28:04
...
先测试了一下加减,检查一下环境,又调用函数测试了服务器名。
源代码:
1class 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*/14public function plus($a, $b) 15 { 16return $a + $b; 17 } 1819/** 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*/28public function subtract($a, $b) 29 { 30return $a - $b; 31 } 3233public function connectToServer($serverName = null) 34 { 35if ($serverName == null) { 36thrownew Exception("这不是一个服务器名"); 37 } 38 $fp = fsockopen($serverName, 8080); 39return ($fp) ? true : false; 40 } 414243 }
生成测试文件:
1class DemoControllerTest extends \PHPUnit_Framework_TestCase 2{ 3 4/** 5 * @var DemoController 6*/ 7protected $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*/ 13protected 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*/ 22protected function tearDown() 23 { 24 25 } 26 27/** 28 * Generated from @assert (5, 8) == 13. 29 * 30 * @covers Home\Controller\DemoController::plus 31*/ 32public function testPlus() 33 { 34 $this->assertEquals( 3513, $this->object->plus(5, 8) 36 ); 37 } 38 39/** 40 * Generated from @assert (16, 76) == 92. 41 * 42 * @covers Home\Controller\DemoController::plus 43*/ 44public function testPlus2() 45 { 46 $this->assertEquals( 4792, $this->object->plus(16, 76) 48 ); 49 } 50 51/** 52 * Generated from @assert (6, 16) == 32. 53 * 54 * @covers Home\Controller\DemoController::plus 55*/ 56public function testPlus3() 57 { 58 $this->assertEquals( 5932, $this->object->plus(6, 16) 60 ); 61 } 62 63/** 64 * Generated from @assert (6, 4) == 0. 65 * 66 * @covers Home\Controller\DemoController::plus 67*/ 68public function testPlus4() 69 { 70 $this->assertEquals( 710, $this->object->plus(6, 4) 72 ); 73 } 74 75/** 76 * Generated from @assert ('abc', 1) == 0. 77 * 78 * @covers Home\Controller\DemoController::plus 79*/ 80public function testPlus5() 81 { 82 $this->assertEquals( 832, $this->object->plus('abc', 1) 84 ); 85 } 86 87/** 88 * Generated from @assert (14, 8) == 6. 89 * 90 * @covers Home\Controller\DemoController::subtract 91*/ 92public function testSubtract() 93 { 94 $this->assertEquals( 956, $this->object->subtract(14, 8) 96 ); 97 } 98 99/** 100 * Generated from @assert (16, 6) == 10. 101 * 102 * @covers Home\Controller\DemoController::subtract 103*/104public function testSubtract2() 105 { 106 $this->assertEquals( 10710, $this->object->subtract(16, 6) 108 ); 109 } 110111/** 112 * Generated from @assert (6, 4) == 0. 113 * 114 * @covers Home\Controller\DemoController::subtract 115*/116public function testSubtract3() 117 { 118 $this->assertEquals( 1190, $this->object->subtract(6, 4) 120 ); 121 } 122123/** 124 * Generated from @assert ('abc', 1) == 0. 125 * 126 * @covers Home\Controller\DemoController::subtract 127*/128public function testSubtract4() 129 { 130 $this->assertEquals( 13144, $this->object->subtract('45', 1) 132 ); 133 } 134135/** 136 * @covers Home\Controller\DemoController::connectToServer 137 * @todo Implement testConnectToServer(). 138*/139public 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 } 148public 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 71) Home\Controller\DemoControllerTest::testPlus3 8 Failed asserting that 22 matches expected 32. 910 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:6711122) Home\Controller\DemoControllerTest::testPlus4 13 Failed asserting that 10 matches expected 0. 1415 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:7916173) Home\Controller\DemoControllerTest::testPlus5 18 Failed asserting that 1 matches expected 2. 1920 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:9121224) Home\Controller\DemoControllerTest::testSubtract3 23 Failed asserting that 2 matches expected 0. 2425 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:12726275) Home\Controller\DemoControllerTest::testConnectToServer2 28 Failed asserting that falseistrue. 2930 D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:1583132 FAILURES! 33 Tests: 11, Assertions: 11, Failures: 5. 34 完成。
以上就介绍了2013考研初试时间 PHPUnit初试,包括了2013考研初试时间方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
上一篇: Python中多线程及程序锁浅析
下一篇: PHP断点续传的原理与实现