专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » phpunit:PHPUnit测试框架应用类及代码结构 »正文

phpunit:PHPUnit测试框架应用类及代码结构

来源: 发布时间:星期一, 2009年1月12日 浏览:26次 评论:0
  目:

  支持各级别代码自动测试

  使用:

  phpunit AllTests #全部测试

  phpunit V2007_AllTests #V2007项目测试

  phpunit V2007_Example_AllTests #V2007项目中Example模块测试

  实现:

  Tests

  ----| AllTests (All Test With test : AllTests)

  ----| V2007 (A Project folder)

  --------| AllTests.php (Project Testsuit, With test : V2007_AllTests)

  --------| Example.php (Module API Test, With test : V2007_ExampleTest)

  --------| Example (A Module folder)

  ------------| AllTests.php (Model Testsuit, With test : V2007_Example_AllTests)

  ------------| ArrayTest.php (Function Test, With test V2007_Example_ArrayTest)

  文件及类命名方式规范标准:

  1. TestCase 以"Test"结尾

  2. TestSuit 以"Tests"结尾

  3. 目录结构和类名根据逻辑测试结构而定如需V2007项目Example模块Array类/测试用例则文件置于V2007/Example/ArrayTest.php 其类名为V2007_Examlpe_ArrayTest

  4. 每次测试以TestSuit进行不支持单独运行TestCase测试

  代码举例:

/**
* AllTests.php
*/
(!d('PHPUnit_MAIN_METHOD')) {
  ('PHPUnit_MAIN_METHOD', 'AllTests::');
}
//List your projects HERE
require_once 'V2007/AllTests.php';
V2007_AllTests
{
  public function
  {
    PHPUnit_TextUI_TestRunner::run(self::suite);
  }
  public function suite
  {
    $suite = PHPUnit_Framework_TestSuite('V2007 rollenc.com');
    //Add your project tests HERE
    $suite->addTest(V2007_AllTests::suite);
     $suite;
  }
}
(PHPUnit_MAIN_METHOD 'AllTests::') {
  AllTests::;
}
  
/**
* V2007/AllTests.php
*/
(!d('PHPUnit_MAIN_METHOD')) {
  ('PHPUnit_MAIN_METHOD', 'V2007_AllTests::');
}
//List your modules HERE
require_once 'V2007/Example.php'; // For APIs
require_once 'V2007/Example/AllTests.php';
V2007_AllTests
{
  public function
  {
    PHPUnit_TextUI_TestRunner::run(self::suite);
  }
  public function suite
  {
    $suite = PHPUnit_Framework_TestSuite('V2007 - Rollenc.com');
    $suite->addTestSuite('V2007_ExampleTest');
    $suite->addTest(V2007_Example_AllTests::suite);
     $suite;
  }
  public function suite
  {
    $suite = PHPUnit_Framework_TestSuite('V2007 - rollenc.com');
    //Add your Module tests HERE
    $suite->addTest(V2007_Example_AllTests::suite);
     $suite;
  }
}
(PHPUnit_MAIN_METHOD 'V2007_AllTests::') {
  V2007_AllTests::;
}
  
/**
* V2007/Example/AllTest.php
*/
(!d('PHPUnit_MAIN_METHOD')) {
    ('PHPUnit_MAIN_METHOD', 'V2007_Example_AllTests::');
}
//List your Function HERE
require_once 'V2007/Example/ArrayTest.php';
require_once 'V2007/Example/StringTest.php';
require_once 'V2007/Example/FunctionTest.php';
V2007_Example_AllTests {
    public function {
        PHPUnit_TextUI_TestRunner :: run(self :: suite);
    }
    public function suite {
        $suite = PHPUnit_Framework_TestSuite('V2007 - V2007_Example');
        $suite->addTestSuite('V2007_Example_ArrayTest');
        $suite->addTestSuite('V2007_Example_StringTest');
        $suite->addTestSuite('V2007_Example_FunctionTest');
         $suite;
    }
}
(PHPUnit_MAIN_METHOD 'V2007_Example_AllTests::') {
  V2007_Example_AllTests::;
}
  
/**
* V2007/Example/ArrayTest.php
*/
V2007_Example_ArrayTest extends PHPUnit_Framework_TestCase
{
    public function Up {
        $this->a = .gif' />(1,2,3,4,5,6,7,8,9);
    }
    
    public function testA {
        .gif' />_push($this->a, 0);
        $this->assertEquals(count($this->a), 10);
        $this->assertEquals(1, 1);
    }
    public function testB {
        $this->assertEquals(count($this->a), 9);
    }
    
    public function tearDown {
        $this->a = .gif' />;
    }
}


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: