没想到啊

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

http://www.oschina.net/question/54100_33155

PHPUnit是什么?

它是一款轻量级的php测试框架

为什么要用PHPUnit?

1. facebook在用

2. 可以通过命令操控测试脚本

3. 可以测试性能

4. 可以测试代码覆盖率

5. 可以自动化的更新测试用例的参数数据

6. 各种格式的日志

6. 最最重要的是,功能如此炫,使用起来还特别简单

PHPUnit的安装

1 pear channel-discover pear.phpunit.de 
2 pear install phpunit/PHPUnit

快速入门

01 <?php
02 require_once 'PHPUnit/Framework.php';
03   
04 class ArrayTest extends PHPUnit_Framework_TestCase
05 {
06     public function testNewArrayIsEmpty()
07     {
08         // 创建数组fixture。
09         $fixture = array();
10   
11         // 断言数组fixture的尺寸是0。
12         $this->assertEquals(0, sizeof($fixture));
13     }
14 }
15 ?>

1. ArrayTest为测试类

2. ArrayTest 继承于PHPUnit_Framework_TestCase

3.测试方法testNewArrayIsEmpty(),测试方法必须为public权限,一般以test开头,或者你也可以选择给其加注释@test来表明该函数为测试函数

1 /**
2 * @test
3 */
4 public function testNewArrayIsEmpty()
5 {
6      $fixture = array();
7      $this->assertEquals(0, sizeof($fixture));
8 }

命令行启动测试

phpunit  测试文件名,此处为要测试ArrayTest.php文件

phpunit ArrayTest
PHPUnit 3.2.10 by Sebastian Bergmann.
..
Time: 0 seconds
OK (2 tests)

命令行参数

01 phpunit --help
02 PHPUnit 3.2.10 by Sebastian Bergmann.
03  
04 Usage: phpunit [switches] UnitTest [UnitTest.php]
05  
06   --log-graphviz <file>  Log test execution in GraphViz markup.
07   --log-json <file>      Log test execution in JSON format.
08   --log-tap <file>       Log test execution in TAP format to file.
09   --log-xml <file>       Log test execution in XML format to file.
10   --log-metrics <file>   Write metrics report in XML format.
11   --log-pmd <file>       Write violations report in PMD XML format.
12  
13   --coverage-html <dir>  Generate code coverage report in HTML format.
14   --coverage-xml <file>  Write code coverage information in XML format.
15  
16   --test-db-dsn <dsn>    DSN for the test database.
17   --test-db-log-rev <r>  Revision information for database logging.
18   --test-db-prefix ...   Prefix that should be stripped from filenames.
19   --test-db-log-info ... Additional information for database logging.
20  
21   --testdox-html <file>  Write agile documentation in HTML format to file.
22   --testdox-text <file>  Write agile documentation in Text format to file.
23  
24   --filter <pattern>     Filter which tests to run.
25   --group ...            Only runs tests from the specified group(s).
26   --exclude-group ...    Exclude tests from the specified group(s).
27  
28   --loader <loader>      TestSuiteLoader implementation to use.
29   --repeat <times>       Runs the test(s) repeatedly.
30  
31   --tap                  Report test execution progress in TAP format.
32   --testdox              Report test execution progress in TestDox format.
33  
34   --no-syntax-check      Disable syntax check of test source files.
35   --stop-on-failure      Stop execution upon first error or failure.
36   --verbose              Output more verbose information.
37   --wait                 Waits for a keystroke after each test.
38  
39   --skeleton             Generate skeleton UnitTest class for Unit in Unit.php.
40  
41   --help                 Prints this usage information.
42   --version              Prints the version and exits.
43  
44   --configuration <file> Read configuration from XML file.
45   -d key[=value]         Sets a php.ini value.

高级功能

你是否已经厌烦了在每一个测试方法命名前面加一个test,是否因为只是调用的参数不同,却要写多个测试用例而纠结?我最喜欢的高级功能,现在隆重推荐给你,叫做框架生成器

1 <?php
2 class Calculator
3 {
4     public function add($a, $b)
5     {
6         return $a + $b;
7     }
8 }
9 ?>

命令行启动测试用例

phpunit --skeleton Calculator
PHPUnit 3.2.10 by Sebastian Bergmann.

Wrote test class skeleton for Calculator to CalculatorTest.php.

简单么?简单,但是它其实没有什么意义,因为没有测试数据,怎样加数据,哦哦哦,重头戏来了

01 <?php
02 class Calculator
03 {
04     /**
05      * @assert (0, 0) == 0
06      * @assert (0, 1) == 1
07      * @assert (1, 0) == 1
08      * @assert (1, 1) == 2
09      */
10     public function add($a, $b)
11     {
12         return $a + $b;
13     }
14 }
15 ?>

原始类中的每个方法都进行@assert注解的检测。这些被转变为测试代码,像这样

1 /**
2   * Generated from @assert (0, 0) == 0.
3   */
4 public function testAdd() {
5     $o = new Calculator;
6     $this->assertEquals(0, $o->add(0, 0));
7 }

下面是运行输出结果:

phpunit CalculatorTest
PHPUnit 3.2.10 by Sebastian Bergmann.

....

Time: 0 seconds


OK (4 tests)

文章出处:http://blog.csdn.net/fly_heart_yuan/article/details/6998603

posted on 2011-12-02 11:59  没想到啊  阅读(2087)  评论(0)    收藏  举报