2017-08-03 11 views
1

私はパラメータを受け入れるphpunitテストケースメソッドを書いています。私はLinuxのターミナルからテストケースの上で実行しようとしていますphpunitテストメソッドにデータを渡すにはどうしたらいいですか?

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

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

次のように

私のコードです。

phpunit --filter testMyCase myTestCase.php 

しかし、パラメータを渡す方法はわかりません。助けてください。

ありがとうございます。

+0

mmmが可能ではないようです。この答えが何らかの方法で役立つことを確認してください。https://stackoverflow.com/a/32467538/2270041 – Matteo

+0

assertTrue()の第2引数は 'string'であると予想されます。https://github.com/を参照してください。 sebastianbergmann/phpunit/blob/6.3.0/src/Framework/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に依存しています。あなたはそれをdataprovider(以前の答えに概説されているように)与えるか、それを日付を返す別のテストメソッドに依存させることができます。

... 

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

    return $date; 
} 

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

    ... 

これらの2つの選択肢(1秒は、あなたがより簡単に複数の異なる依存値を提供できるよう、おそらく優れているデータ・プロバイダである)私が知っています。 PHPUnitのドキュメントにはさらに多くのオプションが表示されることがあります。

関連する問題