PHPUnit 入门篇

fmms 13年前
     <p><strong><a href="http://www.open-open.com/lib/view/open1322626665421.html" target="_blank">PHPUnit</a>是什么?</strong></p>    <p>它是一款轻量级的php测试框架</p>    <p><strong>为什么要用PHPUnit?</strong></p>    <p>1. 非死book在用</p>    <p>2. 可以通过命令操控测试脚本</p>    <p>3. 可以测试性能</p>    <p>4. 可以测试代码覆盖率</p>    <p>5. 可以自动化的更新测试用例的参数数据</p>    <p>6. 各种格式的日志</p>    <p>6. 最最重要的是,功能如此炫,使用起来还特别简单</p>    <p><strong>PHPUnit的安装</strong></p>    <pre class="brush:php; toolbar: true; auto-links: false;"><strong>pear channel-discover pear.phpunit.de   pear install phpunit/PHPUnit  </strong></pre>    <strong>快速入门<pre class="brush:php; toolbar: true; auto-links: false;"><?php require_once 'PHPUnit/Framework.php';   class ArrayTest extends PHPUnit_Framework_TestCase {     public function testNewArrayIsEmpty()     {         // 创建数组fixture。         $fixture = array();           // 断言数组fixture的尺寸是0。         $this->assertEquals(0, sizeof($fixture));     } } ?></pre></strong>    <p></p>    <p>1. ArrayTest为测试类</p>    <p>2. ArrayTest 继承于PHPUnit_Framework_TestCase</p>    <p>3.测试方法testNewArrayIsEmpty(),测试方法必须为public权限,一般以test开头,或者你也可以选择给其加注释@test来表明该函数为测试函数</p>    <pre class="brush:php; toolbar: true; auto-links: false;">/** * @test */ public function testNewArrayIsEmpty() {      $fixture = array();      $this->assertEquals(0, sizeof($fixture)); }</pre>    <p></p>    <p><strong>命令行启动测试</strong></p>    <p>phpunit  测试文件名,此处为要测试ArrayTest.php文件</p>    <p>phpunit ArrayTest<br /> PHPUnit 3.2.10 by Sebastian Bergmann.<br /> ..<br /> Time: 0 seconds<br /> OK (2 tests)</p>    <p>命令行参数</p>    <pre class="brush:php; toolbar: true; auto-links: false;">phpunit --help PHPUnit 3.2.10 by Sebastian Bergmann.  Usage: phpunit [switches] UnitTest [UnitTest.php]    --log-graphviz <file>  Log test execution in GraphViz markup.   --log-json <file>      Log test execution in JSON format.   --log-tap <file>       Log test execution in TAP format to file.   --log-xml <file>       Log test execution in XML format to file.   --log-metrics <file>   Write metrics report in XML format.   --log-pmd <file>       Write violations report in PMD XML format.    --coverage-html <dir>  Generate code coverage report in HTML format.   --coverage-xml <file>  Write code coverage information in XML format.    --test-db-dsn <dsn>    DSN for the test database.   --test-db-log-rev <r>  Revision information for database logging.   --test-db-prefix ...   Prefix that should be stripped from filenames.   --test-db-log-info ... Additional information for database logging.    --testdox-html <file>  Write agile documentation in HTML format to file.   --testdox-text <file>  Write agile documentation in Text format to file.    --filter <pattern>     Filter which tests to run.   --group ...            Only runs tests from the specified group(s).   --exclude-group ...    Exclude tests from the specified group(s).    --loader <loader>      TestSuiteLoader implementation to use.   --repeat <times>       Runs the test(s) repeatedly.    --tap                  Report test execution progress in TAP format.   --testdox              Report test execution progress in TestDox format.    --no-syntax-check      Disable syntax check of test source files.   --stop-on-failure      Stop execution upon first error or failure.   --verbose              Output more verbose information.   --wait                 Waits for a keystroke after each test.    --skeleton             Generate skeleton UnitTest class for Unit in Unit.php.    --help                 Prints this usage information.   --version              Prints the version and exits.    --configuration <file> Read configuration from XML file.   -d key[=value]         Sets a php.ini value.</pre>    <p></p>    <p><strong>高级功能</strong></p>    <p>你是否已经厌烦了在每一个测试方法命名前面加一个test,是否因为只是调用的参数不同,却要写多个测试用例而纠结?我最喜欢的高级功能,现在隆重推荐给你,叫做框架生成器</p>    <pre class="brush:php; toolbar: true; auto-links: false;"><?php class Calculator {     public function add($a, $b)     {         return $a + $b;     } } ?></pre>    <p></p>    <p>命令行启动测试用例 </p>    <p>phpunit --skeleton Calculator<br /> PHPUnit 3.2.10 by Sebastian Bergmann.</p>    <p>Wrote test class skeleton for Calculator to CalculatorTest.php.</p>    <p>简单么?简单,但是它其实没有什么意义,因为没有测试数据,怎样加数据,哦哦哦,重头戏来了</p>    <pre class="brush:php; toolbar: true; auto-links: false;"><?php class Calculator {     /**      * @assert (0, 0) == 0      * @assert (0, 1) == 1      * @assert (1, 0) == 1      * @assert (1, 1) == 2      */     public function add($a, $b)     {         return $a + $b;     } } ?></pre>原始类中的每个方法都进行@assert注解的检测。这些被转变为测试代码,像这样    <pre class="brush:php; toolbar: true; auto-links: false;">/**   * Generated from @assert (0, 0) == 0.   */ public function testAdd() {     $o = new Calculator;     $this->assertEquals(0, $o->add(0, 0)); }</pre>    <p></p>    <p>下面是运行输出结果:</p>    <p>phpunit CalculatorTest<br /> PHPUnit 3.2.10 by Sebastian Bergmann.</p>    <p>....</p>    <p>Time: 0 seconds</p>    <p><br /> OK (4 tests)<br /> PHPUnit介绍:<a href="http://www.open-open.com/lib/view/open1322626665421.html">http://www.open-open.com/lib/view/open1322626665421.html</a><br /> </p>    <p>文章出处:<a href="/misc/goto?guid=4959221679215415887" rel="nofollow">http://blog.csdn.net/fly_heart_yuan/article/details/6998603</a></p>    <p></p>    <p></p>    <p></p>    <p></p>