PHPUnit入门篇

来源:互联网 发布:mac版spss中文版下载 编辑:程序博客网 时间:2024/05/21 07:19

PHPUnit是什么?

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

为什么要用PHPUnit?

1. facebook在用

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

3. 可以测试性能

4. 可以测试代码覆盖率

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

6. 各种格式的日志

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

PHPUnit的安装

[plain] view plaincopyprint?
  1. pear channel-discover pear.phpunit.de  
  2. pear install phpunit/PHPUnit  

快速入门

[php] view plaincopyprint?
  1. <?php  
  2. require_once 'PHPUnit/Framework.php';  
  3.    
  4. class ArrayTest extends PHPUnit_Framework_TestCase  
  5. {  
  6.     public function testNewArrayIsEmpty()  
  7.     {  
  8.         // 创建数组fixture。   
  9.         $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来表明该函数为测试函数

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

命令行启动测试

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

[plain] view plaincopyprint?
  1. phpunit ArrayTest  
  2. PHPUnit 3.2.10 by Sebastian Bergmann.  
  3. ..  
  4. Time: 0 seconds  
  5. OK (2 tests)  

命令行参数

[plain] view plaincopyprint?
  1. phpunit --help  
  2. PHPUnit 3.2.10 by Sebastian Bergmann.  
  3.   
  4. Usage: phpunit [switches] UnitTest [UnitTest.php]  
  5.   
  6.   --log-graphviz <file>  Log test execution in GraphViz markup.  
  7.   --log-json <file>      Log test execution in JSON format.  
  8.   --log-tap <file>       Log test execution in TAP format to file.  
  9.   --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,是否因为只是调用的参数不同,却要写多个测试用例而纠结?我最喜欢的高级功能,现在隆重推荐给你,叫做框架生成器

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

命令行启动测试用例

[plain] view plaincopyprint?
  1. phpunit --skeleton Calculator  
  2. PHPUnit 3.2.10 by Sebastian Bergmann.  
  3.   
  4. Wrote test class skeleton for Calculator to CalculatorTest.php.  

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

[php] view plaincopyprint?
  1. <?php  
  2. class Calculator  
  3. {  
  4.     /** 
  5.      * @assert (0, 0) == 0 
  6.      * @assert (0, 1) == 1 
  7.      * @assert (1, 0) == 1 
  8.      * @assert (1, 1) == 2 
  9.      */  
  10.     public function add($a$b)  
  11.     {  
  12.         return $a + $b;  
  13.     }  
  14. }  
  15. ?>  

原始类中的每个方法都进行@assert注解的检测。这些被转变为测试代码,像这样
    /**
     * Generated from @assert (0, 0) == 0.
     */
    public function testAdd() {
        $o = new Calculator;
        $this->assertEquals(0, $o->add(0, 0));
    }
下面是运行生成的测试用例类的输出。

[plain] view plaincopyprint?
  1. phpunit CalculatorTest  
  2. PHPUnit 3.2.10 by Sebastian Bergmann.  
  3.   
  4. ....  
  5.   
  6. Time: 0 seconds  
  7.   
  8.   
  9. OK (4 tests)
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 玩具飞机遥控器坏了怎么办 玩具无人机遥控器坏了怎么办 玩具遥控车遥控器坏了怎么办 用遥控器关电视后打不开怎么办 汽车遥控器按键坏了怎么办 用遥控器关了电视打不开怎么办 遥控器一个按键坏了怎么办 电视用遥控器关的打不开怎么办 电动车遥控器按键坏了怎么办 海尔空调遥控器按键坏了怎么办 汽车手机支架吸盘吸不住怎么办 车载手机支架吸盘坏了怎么办 假牙的吸盘坏了怎么办 燃气费用一直未交怎么办 凌度gps模块无法定位怎么办? 放疗定位线掉了怎么办 被网络平台骗了怎么办 手机重力传感器坏了怎么办 锤子手机重力传感器坏了怎么办 平板电脑没有开关键怎么办 手机重力感应器坏了怎么办 苹果手机重力感应器坏了怎么办 苹果手机陀螺仪坏了怎么办 狗狗的爪子肿了怎么办 压缩文件之后显示拒绝访问怎么办 压缩文件解压后全散开了怎么办 dnf助手改名字用完了怎么办 缅甸 佤邦 办中国护照 怎么办? 电脑玩游戏网络延迟大怎么办 qq好友空间锁了怎么办 卡盟进货额不足怎么办 被朋友骗了钱怎么办 联通在学校网差怎么办 前夫把我微信拉黑 孩子的事怎么办 微信好友验证疑似被盗怎么办 我的世界被banip怎么办 dnf深渊宝珠出了怎么办 吞噬魔4个球吃了怎么办 dnf没有支援兵了怎么办 家里没通天然气怎么办 苹果6p16g不够用怎么办