測試用例詳解(4)

發表于:2014-08-01來源:uml.org.cn作者:nonultimate點擊數: 標簽:測試用例
} } return $url; } ? PHP代碼 require_once functions.php; class functionsTest extends PHPUnit_Framework_TestCase { /** * 確認url()返回預期的字符串 */ public function testurl() { define(, in

  }

  }

  return $url;

  }

  ?>

  PHP代碼

  require_once 'functions.php';

  class functionsTest extends PHPUnit_Framework_TestCase

  {

  /**

  * 確認url()返回預期的字符串

  */

  public function testurl()

  {

  define('', 'index.php');

  C('VAR_MODULE', 'module');

  C('VAR_ACTION', 'action');

  C('VAR_ROUTER', 'route');

  // 測試通常模式URL

  $uri = url('Index', 'Home', '', APP_NAME, array('q' => 'test', 'msg' => 'OK'));

  $this->assertEquals('index.php?module=Home&action=Index&q=test&msg=OK', $uri);

  // 測試通常模式路由

  $uri = url('Index', 'Home', 'default', APP_NAME, array('q' => 'test', 'msg' => 'OK'));

  $this->assertEquals('index.php?route=default&q=test&msg=OK', $uri);

  C('DISPATCH_ON', true);

  C('URL_MODEL', 1);

  C('PATH_MODEL', 1);

  // 測試普通PATHINFO模式URL

  $uri = url('Index', 'Home', '', APP_NAME, array('q' => 'test', 'msg' => 'OK'));

  $this->assertEquals('index.php/module/Home/action/Index/q/test/msg/OK', $uri);

  // 測試普通PATHINFO模式路由

  $uri = url('Index', 'Home', 'default', APP_NAME, array('q' => 'test', 'msg' => 'OK'));

  $this->assertEquals('index.php/route/default/q/test/msg/OK', $uri);

  C('PATH_MODEL', 2);

  C('PATH_DEPR', '/');

  // 測試智能PATHINFO模式URL

  $uri = url('Index', 'Home', '', APP_NAME, array('q' => 'test', 'msg' => 'OK'));

  $this->assertEquals('index.php/Home/Index/q/test/msg/OK', $uri);

  // 測試智能PATHINFO模式路由

  $uri = url('Index', 'Home', 'default', APP_NAME, array('q' => 'test', 'msg' => 'OK'));

  $this->assertEquals('index.php/default/q/test/msg/OK', $uri);

  }

  }

  ?>

  異常測試

  有時程序執行了非法操作而拋出異常,我們需要模擬某個異常,然后捕捉它是否觸發了該異常。

  PHP代碼

  class UnitTest extends PHPUnit_Framework_TestCase

  {

  /**

  * 測試異常

  */

  public function testException()

  {

  // 期望Exception異常

  $this->setExpectedException('Exception');

  // 拋出Exception異常

  throw new Exception('TestException');

  }

  }

  ?>

  輸出測試

  有時某個方法并不返回而輸出某些內容,我們需要繼承PHPUnit_Extensions_OutputTestCase類來捕捉輸出內容。PHPUnit默認不載入擴展類,需要自己加載。

  PHP代碼

  // 載入輸出測試用例擴展

  require_once 'PHPUnit/Extensions/OutputTestCase.php';

  class UnitTest extends PHPUnit_Extensions_OutputTestCase

  {

  /**

  * 測試輸出

  */

  public function testOutput()

  {

  // 期望輸出的內容是字符串 'Hello'

  $this->expectOutputString('Hello');

  // 輸出 'Hello'

  echo 'Hello';

  }

  }

  ?>

  數據庫測試

  PHPUnit的數據庫測試并不完善,只提供了assertTablesEqual和assertDataSetsEqual兩個斷言與 createFlatXMLDataSet和createXMLDataSet創建XML數據集的方法。無法進行全面的數據操作測試,建議使用 DBUnit。

  附錄

  PHPUnit斷言參考

  assertArrayHasKey($key, array $array, $message = '')

  assertArrayNotHasKey($key, array $array, $message = '')

  assertAttributeContains($needle, $haystackAttributeName, $haystackClassOrObject, $message = '')

  assertAttributeContainsOnly($type, $haystackAttributeName, $haystackClassOrObject, $isNativeType = NULL, $message = '')

原文轉自:http://www.uml.org.cn/Test/200910169.asp

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97