標籤: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的簡單使用