2008/02 20
目的:
支持各级别的代码自动测试
使用:
phpunit AllTests #全部测试
phpunit V2007_AllTests #V2007项目测试
phpunit V2007_Example_AllTests #V2007项目中Example模块测试
实现:
Tests
----| AllTests (All Test With test class: AllTests)
----| V2007 (A Project folder)
--------| AllTests.php (Project Testsuit, With test class: V2007_AllTests)
--------| Example.php (Module API Testcase, With test class: V2007_ExampleTest)
--------| Example (A Module folder)
------------| AllTests.php (Model Testsuit, With test class: V2007_Example_AllTests)
------------| ArrayTest.php (Function Testcase, With test class 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
 */
if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'AllTests::main');
}

//List your projects HERE
require_once 'V2007/AllTests.php';

class V2007_AllTests
{
    public static function main()
    {
        PHPUnit_TextUI_TestRunner::run(self::suite());
    }

    public static function suite()
    {
        $suite = new PHPUnit_Framework_TestSuite('V2007 luochunhui.com');

        //Add your project tests HERE 
        $suite->addTest(V2007_AllTests::suite());

        return $suite;
    }
}
if (PHPUnit_MAIN_METHOD == 'AllTests::main') {
    AllTests::main();
}


/**
 * V2007/AllTests.php
 */

if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'V2007_AllTests::main');
}

//List your modules HERE
require_once 'V2007/Example.php'; // For APIs
require_once 'V2007/Example/AllTests.php';

class V2007_AllTests
{
    public static function main()
    {
        PHPUnit_TextUI_TestRunner::run(self::suite());
    }

    public static function suite()
    {
        $suite = new PHPUnit_Framework_TestSuite('V2007 System - Rollenc.com');
        $suite->addTestSuite('V2007_ExampleTest');
        $suite->addTest(V2007_Example_AllTests::suite());
        return $suite;
    }

    public static function suite()
    {
        $suite = new PHPUnit_Framework_TestSuite('V2007 System - luochunhui.com');
        //Add your Module tests HERE
        $suite->addTest(V2007_Example_AllTests::suite());
        return $suite;
    }

}
if (PHPUnit_MAIN_METHOD == 'V2007_AllTests::main') {
    V2007_AllTests::main();
}


/**
 * V2007/Example/AllTest.php
 */
if (!defined('PHPUnit_MAIN_METHOD')) {
	define('PHPUnit_MAIN_METHOD', 'V2007_Example_AllTests::main');
}
//List your Function HERE
require_once 'V2007/Example/ArrayTest.php';
require_once 'V2007/Example/StringTest.php';
require_once 'V2007/Example/FunctionTest.php';

class V2007_Example_AllTests {
	public static function main() {
		PHPUnit_TextUI_TestRunner :: run(self :: suite());
	}

	public static function suite() {
		$suite = new PHPUnit_Framework_TestSuite('V2007 System - V2007_Example');

		$suite->addTestSuite('V2007_Example_ArrayTest');
		$suite->addTestSuite('V2007_Example_StringTest');
		$suite->addTestSuite('V2007_Example_FunctionTest');
		return $suite;
	}
}

if (PHPUnit_MAIN_METHOD == 'V2007_Example_AllTests::main') {
    V2007_Example_AllTests::main();
}


/**
 * V2007/Example/ArrayTest.php
 */
class V2007_Example_ArrayTest extends PHPUnit_Framework_TestCase
{
	public function setUp() {
		$this->a = array(1,2,3,4,5,6,7,8,9);
	}
	
	public function testA() {
		array_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 = array();
	}
}


参考:
ZendFramework

Posted by rollenc

Last modified on 2008-02-21 08:29