您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHPUnit初试,phpunit_PHP教程

2025/8/3 21:04:45发布16次查看
phpunit初试,phpunit先测试了一下加减,检查一下环境,又调用函数测试了服务器名。
源代码:
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(这不是一个服务器名);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::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 }
这里的服务器测试用例是手动加上去的!
执行结果:
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:6711 12 2) home\controller\democontrollertest::testplus413 failed asserting that 10 matches expected 0.14 15 d:\wamp\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\wxportal\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 完成。
http://www.bkjia.com/phpjc/1134779.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1134779.htmltecharticlephpunit初试,phpunit 先测试了一下加减,检查一下环境,又调用函数测试了服务器名。 源代码: 1 class democontroller extends \think\controller 2 { 3...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product