數(shù)據(jù)供給器

2018-02-24 15:41 更新

數(shù)據(jù)供給器

測試方法可以接受任意參數(shù)。這些參數(shù)由數(shù)據(jù)供給器方法(在 Example?2.5, “使用返回數(shù)組的數(shù)組的數(shù)據(jù)供給器”中,是 additionProvider() 方法)提供。用 @dataProvider 標(biāo)注來指定使用哪個數(shù)據(jù)供給器方法。

數(shù)據(jù)供給器方法必須聲明為 public,其返回值要么是一個數(shù)組,其每個元素也是數(shù)組;要么是一個實現(xiàn)了 Iterator 接口的對象,在對它進(jìn)行迭代時每步產(chǎn)生一個數(shù)組。每個數(shù)組都是測試數(shù)據(jù)集的一部分,將以它的內(nèi)容作為參數(shù)來調(diào)用測試方法。

Example?2.5.?使用返回數(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ù)集時,最好逐個用字符串鍵名對其命名,避免用默認(rèn)的數(shù)字鍵名。這樣輸出信息會更加詳細(xì)些,其中將包含打斷測試的數(shù)據(jù)集所對應(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.?使用返回迭代器對象的數(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++;
    }
}
?>

如果測試同時從 @dataProvider 方法和一個或多個 @depends 測試接收數(shù)據(jù),那么來自于數(shù)據(jù)供給器的參數(shù)將先于來自所依賴的測試的。來自于所依賴的測試的參數(shù)對于每個數(shù)據(jù)集都是一樣的。參見Example?2.9, “在同一個測試中組合使用 @depends 和 @dataProvider”

Example?2.9.?在同一個測試中組合使用 @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

如果一個測試依賴于另外一個使用了數(shù)據(jù)供給器的測試,僅當(dāng)被依賴的測試至少能在一組數(shù)據(jù)上成功時,依賴于它的測試才會運(yùn)行。使用了數(shù)據(jù)供給器的測試,其運(yùn)行結(jié)果是無法注入到依賴于此測試的其他測試中的。

所有的數(shù)據(jù)供給器方法的執(zhí)行都是在對 setUpBeforeClass 靜態(tài)方法的調(diào)用和第一次對 setUp 方法的調(diào)用之前完成的。因此,無法在數(shù)據(jù)供給器中使用創(chuàng)建于這兩個方法內(nèi)的變量。這是必須的,這樣 PHPUnit 才能計算測試的總數(shù)量。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號