PHP PHPUnit的簡單使用

來源:互聯網
上載者:User

標籤:case   osi   cal   ica   xdebug   ges   href   open   bug   

1.window安裝pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html

2.在工作目錄下,放兩個檔案:

1)Calculator.php

 1 <?php 2  3 class Calculator{ 4  5     public function add($a,$b){ 6         return $a + $b; 7     } 8  9     public function sub($a,$b){10         return $a - $b;11     }12 }
View Code

2)CalculatorTest.php

 1 <?php 2  3 //PEAR安裝在系統中 4 require "PHPUnit/TestCase.php"; 5 require "Calculator.php"; 6  7 class CalculatorTest extends PHPUnit_Framework_TestCase{ 8     private $calculator; 9 10     function setUp()11     {12         parent::setUp(); // TODO: Change the autogenerated stub13         $this->calculator = new Calculator();14     }15 16     function tearDown()17     {18         parent::tearDown(); // TODO: Change the autogenerated stub19         unset($this->calculator);20     }21 22     public function testAddBothPositive(){23         $result = $this->calculator->add(3,4);24         $this->assertEquals(8,$result);25     }26 27     public function testAddPositiveAndNegative(){28         $result = $this->calculator->add(3,-4);29         $this->assertEquals(-1,$result);30     }31 32     public function testAddNegativeAndPositive(){33         $result = $this->calculator->add(-4,3);34         $this->assertEquals(-1,$result);35     }36 37     public function testAddPositiveAndZero(){38         $result = $this->calculator->add(5,0);39         $this->assertEquals(5,$result);40     }41 42     public function testAddNegativeAndZero(){43         $result = $this->calculator->add(-5,0);44         $this->assertEquals(-5,$result);45     }46 47     public function testAddNegativeAndNegative(){48         $result = $this->calculator->add(-5,-5);49         $this->assertEquals(-10,$result);50     }51 }
View Code

當前工作目錄下,在控制台運行 : phpunit  CalculatorTest

注意:如果出現找不到PHPUnit相關的標頭檔,可以用在相關檔案輸出get_include_path()的結果查看. 在php.ini 可以找 “”include_path" 關鍵字,定位原因。

3. 在安裝XDebug的前提下,可以運行:phpunit  --coverage-html  "OUTPUT_PATH"  CalculatorTest ,產生一個報表,HTML格式,可以瞭解此次測試代碼的覆蓋率。

 

PHP PHPUnit的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.