PHP PHPUnit的簡單使用

1.Pear的官方教程:https://phpunit.de/manual/current/zh_cn/phpunit-book.html#writing-tests-for-phpunitphp

2.將Pear的安裝目錄的路徑,加入Path;在CMD中,執行 "pear list" ,html

若是沒有,PHPUnit模塊,執行數組

 

3.在php.ini文件的 include_path 路徑,將pear的相關的文件路徑加進去 ,這是個人:ide

4. 在編寫測試文件時,直接include下面,是找不到TestCase類的,函數

會報錯:測試

PHP Fatal error:  Class 'TestCase' not found in H:\Slg\SlgCardsServer\actions\MyTest.php on line 26
這是,由於include_path路徑下面的文件TestCase.php的TestCase並不存在,這是要本身編寫的。this

5. 若是不使用本身編寫的TestCase類,能夠繼承 PHPUnit_Framework_TestCase 類。這個類繼承了PHPUnit_Framework_Assert,提供了一些基本的asset***的斷言方法。spa

6.以下代碼:code

 1 <?php
 2 
 3 include_once 'PHPUnit\TestCase.php';
 4 
 5 class Test extends PHPUnit_Framework_TestCase
 6 {
 7     public function testEmpty()
 8     {
 9         $stack = [];
10         $this->assertNotEmpty($stack);
11 
12         return $stack;
13     }
14 
15     /**
16      * @depends testEmpty
17      */
18     public function testPush(array $stack)
19     {
20         array_push($stack, 'foo');
21         $this->assertEquals('foo', $stack[count($stack)-1]);
22         $this->assertNotEmpty($stack);
23 
24         return $stack;
25     }
26 
27     /**
28      * @depends testPush
29      */
30     public function testPop(array $stack)
31     {
32         $this->assertEquals('foo', array_pop($stack));
33         $this->assertEmpty($stack);
34     }
35 }
36 
37 ?>

當第一個函數的測試代碼有問題時,運行結果:htm

7.多重依賴的代碼測試:

 1 <?php
 2 include_once 'PHPUnit\TestCase.php';
 3 
 4 class Test extends PHPUnit_Framework_TestCase
 5 {
 6     public function testProduceFirst()
 7     {
 8         $this->assertTrue(true);
 9         return 'First';
10     }
11 
12     public function testProduceSecond()
13     {
14         $this->assertTrue(true);
15         return 'Second';
16     }
17 
18     /**
19      * 下面的依賴
20      * @depends testProduceFirst                   
21      * @depends testProduceSecond
22      */
23     public function testEquals()
24     {
25         return $this->assertEquals(
26             ['first','second'],
27             func_get_args()
28         );
29     }
30 }
31 
32 ?> 

運行結果:

10 .數據提供器

1)數據供給器方法必須聲明爲 public,其返回值要麼是一個數組,其每一個元素也是數組;要麼是一個實現了 Iterator 接口的對象,在對它進行迭代時每步產生一個數組。每一個數組都是測試數據集的一部分,將以它的內容做爲參數來調用測試方法。

2)測試代碼:

 1 <?php
 2 include_once 'PHPUnit\TestCase.php';
 3 
 4 class DataProviderTest extends PHPUnit_Framework_TestCase
 5 {
 6     /**
 7      * @dataProvider additionProvider
 8      */
 9     public function testAdd($a,$b,$sum)
10     {
11         $this->assertEquals($sum,$a+$b);
12     }
13 
14     public function additionProvider()
15     {
16         return [
17             'Data1' => [1,2,3],
18             'Data2' => [4,7,9],
19             'Data3' => [5,4,9],
20         ];
21     }
22 }
23 
24 ?>

運行的結果:

 

 

 

要點:

1.文檔註釋塊(docblock)中使用 @test 標註將其標記爲測試方法,用 @depends 標註來表達測試方法之間的依賴關係。

2.若是須要傳遞對象的副本而非引用,則應當用 @depends clone 替代 @depends

3.PHPUnit 不會更改測試的運行順序,所以你須要自行保證某個測試所依賴的全部測試均出現於這個測試以前。

4.若是出現找不到PHPUnit相關的頭文件,能夠用在相關文件輸出get_include_path()的結果查看. 在php.ini 能夠找 「」include_path" 關鍵字,定位緣由。

 在 PHPUnit的目錄下有一個文件Autoload.php,在設置了php.ini的include_path的變量以後,在使用文件中,包括該文件。

5.在安裝XDebug的前提下(extension = XDEBUG_PATH  替換爲 zend_extension = XDEBUG_PATH ),能夠運行:phpunit  --coverage-html  "OUTPUT_PATH"  CalculatorTest ,生成一個報表,HTML格式,能夠了解這次測試代碼的覆蓋率。 默認在當前目錄生成OUTPUT_PATH目錄。

 6.不要將被測試的類和測試類放在同一個文件,這樣是生成的XDEBUG代碼覆蓋率報告數據都是空的。

7. 測試類裏面的測試函數必定要以 "test"開頭 !!!

相關文章
相關標籤/搜索