PHPUnit初试

先测试了一下加减,检查一下环境,又调用函数测试了服务器名。

源代码:

 class DemoController extends \Think\Controller
{ /**
* @assert (5, 8) == 13
* @assert (16, 76) == 92
* @assert (6, 16) == 32
* @assert (6, 4) == 0
* @assert ('abc', 1) == 2
* @param int $a
* @param int $b
* @return int
*/
public function plus($a, $b)
{
return $a + $b;
} /**
* @assert (14, 8) == 6
* @assert (16, 6) == 10
* @assert (6, 4) == 0
* @assert ('45', 1) == 44
* @param int $a
* @param int $b
* @return int
*/
public function subtract($a, $b)
{
return $a - $b;
} public function connectToServer($serverName = null)
{
if ($serverName == null) {
throw new Exception("这不是一个服务器名");
}
$fp = fsockopen($serverName, );
return ($fp) ? true : false;
} }

生成测试文件:

 class DemoControllerTest extends \PHPUnit_Framework_TestCase
{ /**
* @var DemoController
*/
protected $object; /**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new DemoController;
} /**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{ } /**
* Generated from @assert (5, 8) == 13.
*
* @covers Home\Controller\DemoController::plus
*/
public function testPlus()
{
$this->assertEquals(
, $this->object->plus(, )
);
} /**
* Generated from @assert (16, 76) == 92.
*
* @covers Home\Controller\DemoController::plus
*/
public function testPlus2()
{
$this->assertEquals(
, $this->object->plus(, )
);
} /**
* Generated from @assert (6, 16) == 32.
*
* @covers Home\Controller\DemoController::plus
*/
public function testPlus3()
{
$this->assertEquals(
, $this->object->plus(, )
);
} /**
* Generated from @assert (6, 4) == 0.
*
* @covers Home\Controller\DemoController::plus
*/
public function testPlus4()
{
$this->assertEquals(
, $this->object->plus(, )
);
} /**
* Generated from @assert ('abc', 1) == 0.
*
* @covers Home\Controller\DemoController::plus
*/
public function testPlus5()
{
$this->assertEquals(
, $this->object->plus('abc', )
);
} /**
* Generated from @assert (14, 8) == 6.
*
* @covers Home\Controller\DemoController::subtract
*/
public function testSubtract()
{
$this->assertEquals(
, $this->object->subtract(, )
);
} /**
* Generated from @assert (16, 6) == 10.
*
* @covers Home\Controller\DemoController::subtract
*/
public function testSubtract2()
{
$this->assertEquals(
, $this->object->subtract(, )
);
} /**
* Generated from @assert (6, 4) == 0.
*
* @covers Home\Controller\DemoController::subtract
*/
public function testSubtract3()
{
$this->assertEquals(
, $this->object->subtract(, )
);
} /**
* Generated from @assert ('abc', 1) == 0.
*
* @covers Home\Controller\DemoController::subtract
*/
public function testSubtract4()
{
$this->assertEquals(
, $this->object->subtract('', )
);
} /**
* @covers Home\Controller\DemoController::connectToServer
* @todo Implement testConnectToServer().
*/
public function testConnectToServer()
{
// // Remove the following lines when you implement this test.
// $this->markTestIncomplete(
// 'This test has not been implemented yet.'
// );
$serverName = 'wwwcom';
$this->assertTrue($this->object->connectToServer($serverName) === false);
}
public function testConnectToServer2()
{
$serverName = 'www.baidu.com';
$this->assertTrue($this->object->connectToServer($serverName) !== false);
}

这里的服务器测试用例是手动加上去的!

执行结果:

 ..FFF..F..F                                                        /  (%)

 Time: 44.42 seconds, Memory: .75Mb

 There were  failures:

 ) Home\Controller\DemoControllerTest::testPlus3
Failed asserting that matches expected . D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: ) Home\Controller\DemoControllerTest::testPlus4
Failed asserting that matches expected . D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: ) Home\Controller\DemoControllerTest::testPlus5
Failed asserting that matches expected . D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: ) Home\Controller\DemoControllerTest::testSubtract3
Failed asserting that matches expected . D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: ) Home\Controller\DemoControllerTest::testConnectToServer2
Failed asserting that false is true. D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php: FAILURES!
Tests: , Assertions: , Failures: .
完成。
上一篇:【Java】在Eclipse中使用JUnit4进行单元测试(初级篇)


下一篇:ASP.NET MVC路由