2017-08-03 45 views
1

我正在編寫接受參數的phpunit測試用例方法。如何將數據傳遞給phpunit測試方法?

我的代碼如下

class myTestCase extends PHPUnit_Framework_TestCase 
{ 
    public function testMyCase($date){ 

     $resultSet=$this->query("select * from testCase where date='$date'"); 
     $this->assertTrue($resultSet,True); 
    } 
} 

我想從上面Linux終端運行測試用例。

phpunit --filter testMyCase myTestCase.php 

但是不知道如何傳遞參數。請幫忙。

謝謝。

+0

mmm似乎不可能。檢查這個答案可以以某種方式幫助你https://stackoverflow.com/a/32467538/2270041 – Matteo

+0

assertTrue()的第二個參數預計是一個字符串,參見https://github.com/ sebastianbergmann/PHPUnit的/ BLOB/6.3.0/src目錄/框架/ Assert.php#L1148-L1151。 – localheinz

+0

請參閱https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers。 – localheinz

回答

2

首先,您應該使用最新版本的PHPUnit(v6.x)。 PHPUnit_Framework_TestCase的存在使它看起來像你使用的是舊版本。但我離題...

你想要一個數據提供者。

class myTestCase extends PHPUnit_Framework_TestCase 
{ 
    /** 
    * @dataProvider providerTestMyCase 
    **/ 

    public function testMyCase($date, $expectedRowCount) { 

     $sql = "select * from testCase where date = '$date'"; 
     $stmt = self::$pdo->prepare($sql); 
     $result = $stmt->execute($sql); 

     //We expect the query will always work. 
     $this->assertTrue($result); 

     //We are expecting the rowcount will be consistent with the expected rowcounts. 
     $this->assertSame($expectedRowCount,$stmt->rowCount()); 
    } 

    public funtion providerTestMyCase() { 
     return [ [ '2017-08-01' , 76 ] 
       , [ '2017-08-02' , 63 ] 
       , [ '2017-08-03' , 49 ] 
       , [ '2017-08-04' , 31 ] 
       , [ '2017-08-05' , 95 ] 
       ] 
    } 
} 

閱讀和重新閱讀:Database Testing以及@dataProvider

+0

建議使用最新版本的'phpunit/phpunit'可以,但可能有原因不能使用最新版本:'phpunit/phpunit:^ 6'需要'php:^ 7.0',但是該項目可能需要一個不滿足此要求的版本。 – localheinz

+0

5.6 [6個月前失去了積極支持](http://php.net/supported-versions.php)。 [當前最佳實踐](http://www.phptherightway.com/#use_the_current_stable_version)是使用7.x.如果你正在編寫新的代碼,它應該是7.0兼容的,否則你會有一些胃灼熱。 [7更快](http://www.zend.com/en/resources/php7_infographic),更安全(從7.2開始使用libsodium),PHPUnit <6.0不再受支持。堅持5.6只是將[技術債務](https://en.wikipedia.org/wiki/Technical_debt)建立到他們的項目中。如果你沒有別的選擇,只留5.6。 – DrDamnit

+0

我知道,但是:我一直在爲數百萬$$$的公司開發大型遺留項目,這些項目最初的測試覆蓋率爲0%,並且沒有辦法只是切換到PHP7。儘管有潛在的好處,升級還有很多原因 - 尚未 - 可能。 – localheinz

0

也許你不需要它。

class myTestCase extends PHPUnit_Framework_TestCase 
{ 
    public function testMyCases() 
    { 
     $dates = [ 
      '2017-01-01 00:00:00', 
      '2017-08-03 15:00:00' 
     ]; 

     foreach ($dates as $date) { 
      $resultSet = $this->query("select * from testCase where date='$date'"); 
      $this->assertTrue($resultSet, true); 
     } 

    } 
} 
+3

最好使用[@dataProvider](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers),但這個想法是正確的 - 所有測試的排列都應該記錄在測試用例中。 –

-1

你的測試方法對$date的依賴。你可以給它一個數據提供者(如前面的答案所概述的),或者讓它依賴於另一個返回日期的測試方法。

... 

function testDateDependency() 
{ 
    $date = '...'; 
    $this->assertInternalType('string', $date, 'dependency check'); 

    return $date; 
} 

/** 
* @depends testDateDependency 
*/ 
function testMyCase($date) { 

    ... 

這是兩個選項(第二個是數據提供者,其可能是優視就可以更方便地提供更多不同的依賴性值),我知道的。 PHPUnit文檔可能會顯示更多選項。