W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
測(cè)試方法可以接受任意參數(shù)。這些參數(shù)由數(shù)據(jù)供給器方法(在 Example?2.5, “使用返回?cái)?shù)組的數(shù)組的數(shù)據(jù)供給器”中,是 additionProvider()
方法)提供。用 @dataProvider
標(biāo)注來(lái)指定使用哪個(gè)數(shù)據(jù)供給器方法。
數(shù)據(jù)供給器方法必須聲明為 public
,其返回值要么是一個(gè)數(shù)組,其每個(gè)元素也是數(shù)組;要么是一個(gè)實(shí)現(xiàn)了 Iterator
接口的對(duì)象,在對(duì)它進(jìn)行迭代時(shí)每步產(chǎn)生一個(gè)數(shù)組。每個(gè)數(shù)組都是測(cè)試數(shù)據(jù)集的一部分,將以它的內(nèi)容作為參數(shù)來(lái)調(diào)用測(cè)試方法。
Example?2.5.?使用返回?cái)?shù)組的數(shù)組的數(shù)據(jù)供給器
<?php
class DataTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);
}
}
?>
phpunit DataTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
...F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) DataTest::testAdd with data set #3 (1, 1, 3)
Failed asserting that 2 matches expected 3.
/home/sb/DataTest.php:9
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
當(dāng)使用到大量數(shù)據(jù)集時(shí),最好逐個(gè)用字符串鍵名對(duì)其命名,避免用默認(rèn)的數(shù)字鍵名。這樣輸出信息會(huì)更加詳細(xì)些,其中將包含打斷測(cè)試的數(shù)據(jù)集所對(duì)應(yīng)的名稱。
Example?2.6.?使用帶有命名數(shù)據(jù)集的數(shù)據(jù)供給器
<?php
class DataTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return array(
'adding zeros' => array(0, 0, 0),
'zero plus one' => array(0, 1, 1),
'one plus zero' => array(1, 0, 1),
'one plus one' => array(1, 1, 3)
);
}
}
?>
phpunit DataTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
...F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) DataTest::testAdd with data set "one plus one" (1, 1, 3)
Failed asserting that 2 matches expected 3.
/home/sb/DataTest.php:9
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
Example?2.7.?使用返回迭代器對(duì)象的數(shù)據(jù)供給器
<?php
require 'CsvFileIterator.php';
class DataTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return new CsvFileIterator('data.csv');
}
}
?>
phpunit DataTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
...F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) DataTest::testAdd with data set #3 ('1', '1', '3')
Failed asserting that 2 matches expected '3'.
/home/sb/DataTest.php:11
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
Example?2.8.?CsvFileIterator 類
<?php
class CsvFileIterator implements Iterator {
protected $file;
protected $key = 0;
protected $current;
public function __construct($file) {
$this->file = fopen($file, 'r');
}
public function __destruct() {
fclose($this->file);
}
public function rewind() {
rewind($this->file);
$this->current = fgetcsv($this->file);
$this->key = 0;
}
public function valid() {
return !feof($this->file);
}
public function key() {
return $this->key;
}
public function current() {
return $this->current;
}
public function next() {
$this->current = fgetcsv($this->file);
$this->key++;
}
}
?>
如果測(cè)試同時(shí)從 @dataProvider
方法和一個(gè)或多個(gè) @depends
測(cè)試接收數(shù)據(jù),那么來(lái)自于數(shù)據(jù)供給器的參數(shù)將先于來(lái)自所依賴的測(cè)試的。來(lái)自于所依賴的測(cè)試的參數(shù)對(duì)于每個(gè)數(shù)據(jù)集都是一樣的。參見Example?2.9, “在同一個(gè)測(cè)試中組合使用 @depends 和 @dataProvider”
Example?2.9.?在同一個(gè)測(cè)試中組合使用 @depends 和 @dataProvider
<?php
class DependencyAndDataProviderComboTest extends PHPUnit_Framework_TestCase
{
public function provider()
{
return array(array('provider1'), array('provider2'));
}
public function testProducerFirst()
{
$this->assertTrue(true);
return 'first';
}
public function testProducerSecond()
{
$this->assertTrue(true);
return 'second';
}
/**
* @depends testProducerFirst
* @depends testProducerSecond
* @dataProvider provider
*/
public function testConsumer()
{
$this->assertEquals(
array('provider1', 'first', 'second'),
func_get_args()
);
}
}
?>
phpunit --verbose DependencyAndDataProviderComboTest
PHPUnit 5.0.0 by Sebastian Bergmann and contributors.
...F
Time: 0 seconds, Memory: 3.50Mb
There was 1 failure:
1) DependencyAndDataProviderComboTest::testConsumer with data set #1 ('provider2')
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 0 => 'provider1'
+ 0 => 'provider2'
1 => 'first'
2 => 'second'
)
/home/sb/DependencyAndDataProviderComboTest.php:31
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
Note
如果一個(gè)測(cè)試依賴于另外一個(gè)使用了數(shù)據(jù)供給器的測(cè)試,僅當(dāng)被依賴的測(cè)試至少能在一組數(shù)據(jù)上成功時(shí),依賴于它的測(cè)試才會(huì)運(yùn)行。使用了數(shù)據(jù)供給器的測(cè)試,其運(yùn)行結(jié)果是無(wú)法注入到依賴于此測(cè)試的其他測(cè)試中的。
所有的數(shù)據(jù)供給器方法的執(zhí)行都是在對(duì)
setUpBeforeClass
靜態(tài)方法的調(diào)用和第一次對(duì)setUp
方法的調(diào)用之前完成的。因此,無(wú)法在數(shù)據(jù)供給器中使用創(chuàng)建于這兩個(gè)方法內(nèi)的變量。這是必須的,這樣 PHPUnit 才能計(jì)算測(cè)試的總數(shù)量。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: