首页  >  文章  >  php教程  >  phpunit功能点整理

phpunit功能点整理

WBOY
WBOY原创
2016-06-13 10:53:411135浏览

 

 

设置场景

创建数组Fixtures

 

[php]  

protected function setUp()  

{  

// 创建数组fixture。  

$this->fixture = array();  

}  

 

 

 

 

“套件级装配器” 共享fixture即sharedFixture

PHPUnit_Framework_TestSuite对象的$sharedFixture属性在PHPUnit_Framework_TestSuite对象集合及PHPUnit_Framework_TestCase对象中都可用。

[php] 

protected function setUp()  

{  

$this->sharedFixture = new PDO(  

  'mysql:host=wopr;dbname=test',  

  'root',  

  ''  

);  

}  

 

 

 

 

provider数据提供者

 

 

使用数据提供者

 

组织测试套件

 

 

 

PHPUnit框架的PHPUnit_Framework_TestSuite类允许我们将一些测试组织在若干测试套件构成的一个层次结构中。让我们通过一个例子看看PHPUnit特有的测试套件。

 

 

范例 7.1显示一个删节版本的Tests/AllTests.php,范例 7.2显示一个删节版本的Tests/Framework/AllTests.php。 

 

第一级:

[php] 

if (!defined('PHPUnit_MAIN_METHOD')) {  

    define('PHPUnit_MAIN_METHOD', 'AllTests::main');  

}  

   

require_once 'PHPUnit/Framework.php';  

require_once 'PHPUnit/TextUI/TestRunner.php';  

   

require_once 'Framework/AllTests.php';  

// ...  

   

class AllTests  

{  

    public static function main()  

    {  

        PHPUnit_TextUI_TestRunner::run(self::suite());  

    }  

   

    public static function suite()  

    {  

        $suite = new PHPUnit_Framework_TestSuite('PHPUnit');  

   

        $suite->addTest(Framework_AllTests::suite());  

        // ...  

   

        return $suite;  

    }  

}  

   

if (PHPUnit_MAIN_METHOD == 'AllTests::main') {  

    AllTests::main();  

}  

?>  

 

 

 

第二级:

 

[php]  

if (!defined('PHPUnit_MAIN_METHOD')) {  

    define('PHPUnit_MAIN_METHOD', 'Framework_AllTests::main');  

}  

   

require_once 'PHPUnit/Framework.php';  

require_once 'PHPUnit/TextUI/TestRunner.php';  

   

require_once 'Framework/AssertTest.php';  

// ...  

   

class Framework_AllTests  

{  

    public static function main()  

    {  

        PHPUnit_TextUI_TestRunner::run(self::suite());  

    }  

   

    public static function suite()  

    {  

        $suite = new PHPUnit_Framework_TestSuite('PHPUnit Framework');  

   

        $suite->addTestSuite('Framework_AssertTest');  

        // ...  

   

        return $suite;  

    }  

}  

   

if (PHPUnit_MAIN_METHOD == 'Framework_AllTests::main') {  

    Framework_AllTests::main();  

}  

?>  

 

 

 

第三级:

[php]  

/** 

 * PHPunit测试套件 

 * /tests/Framework/Framework/AssertTest.php 

 * @anthor Chen Wei Han 

 * @copyright  2011-7-6下午02:10:29 

 * @package phpunit 

 * @todo 

 */  

//require_once 'PHPUnit/Framework.php';  

class Framework_Framework_AssertTest extends PHPUnit_Framework_TestCase{     

 public function testNewArrayIsEmpty()     

 {    

  // 创建数组fixture。         

  $fixture = array();          

  // 断言数组fixture的尺寸是0。         

  $this->assertEquals(0, sizeof($fixture));     

 }  

      

 public function testArrayContainsAnElement()     

 {    

  // 创建数组fixture。         

  $fixture = array();          

  // 向数组fixture增加一个元素。         

  $fixture[] = 'Element';          

  //断言数组fixture的尺寸是1。         

  $this->assertEquals(1, sizeof($fixture));     

 }  

}  

?>  

 

 

 

类Framework_AssertTest是个扩展了PHPUnit_Framework_TestCase的标准测试用例。

 

运行Tests/AllTests.php则使用TextUI测试启动器运行全部测试,然而运行Tests/Framework/AllTests.php则只运行类PHPUnit_Framework_*的测试。

 

 

 

套件级装配器

 

类PHPUnit_Framework_TestSuite提供两个模板方法,setUp()和tearDown(),它们分别在测试套件的首个测试前和最后测试后被调用。

[php] 

require_once 'MyTest.php';  

   

class MySuite extends PHPUnit_Framework_TestSuite  

{  

    public static function suite()  

    {  

        return new MySuite('MyTest');  

    }  

   

    protected function setUp()  

    {  

        print "\nMySuite::setUp()";  

    }  

   

    protected function tearDown()  

    {  

        print "\nMySuite::tearDown()";  

    }  

}  

?>  

 

 

 

未完成和跳过的测试

 

public function testSomething()

{

}

 

如果我们分别将成功的测试和失败的必做绿灯和红灯,我们还需要黄灯标记未完成或未实现的测试。PHPUnit_Framework_IncompleteTest是个标记接口,用于标记当测试结果为未完成或当前未实现时引发的异常。

 

[php]  

require_once 'PHPUnit/Framework.php';  

   

class SampleTest extends PHPUnit_Framework_TestCase  

{  

    public function testSomething()  

    {  

        //可选:随便测试什么都可以。  

        $this->assertTrue(TRUE, 'This should already work.');  

   

        // 在这儿停住并将测试标记为未完成。  

        $this->markTestIncomplete(  

          'This test has not been implemented yet.'  

        );  

    }  

}  

?>  

 

 

 

跳过的测试

 

特定的环境中并非所有的测试都能运行。考虑个例子,一个具有多个驱动以支持不同数据库系统的数据库提取层。MySQL驱动的测试当然只能在MySQL服务器上运行。 $this->markTestSkipped

 

[php] 

require_once 'PHPUnit/Framework.php';  

   

class DatabaseTest extends PHPUnit_Framework_TestCase  

{  

    protected function setUp()  

    {  

        if (!extension_loaded('mysqli')) {  

            $this->markTestSkipped(  

              'The MySQLi extension is not available.'  

            );  

        }  

    }  

   

    public function testConnection()  

    {  

        // ...  

    }  

}  

?>  

 

 

 

 

PHPUnit_Framework_TestResult

 

当你在运行所有这些测试时,你需要在某处存储所有结果:运行了多少测试,哪个失败了,以及他们耗时多久。

 

PHPUnit自带两个具体的测试装饰者:PHPUnit_Extensions_RepeatedTest和PHPUnit_Extensions_TestSetup。前一个用于重复运行一个测试,并且只当所有迭代都成功时才算成功。后面一个在第 6 章中讨论过。

 

 要定制PHPUnit_Framework_TestResult,没必要编写它的整个子类。大多时候,实现一个新PHPUnit_Framework_TestListener(见表 22.14)并在运行测试前附在PHPUnit_Framework_TestResult对象上就够了。

 

范例 23.4: 运行和观测测试套件

[php]  www.2cto.com

require_once 'PHPUnit/Framework.php';  

   

require_once 'ArrayTest.php';  

require_once 'SimpleTestListener.php';  

   

// 创建一个包括测试套件,来自类ArrayTest的测试。  

$suite = new PHPUnit_Framework_TestSuite('ArrayTest');  

   

// 创建一个测试结果,并附上一个SimpleTestListener对象作为对它的观测者。  

$result = new PHPUnit_Framework_TestResult;  

$result->addListener(new SimpleTestListener);  

   

// 运行测试。  

$suite->run($result);  

?>  

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn