ํ ์คํธ์ ์์ฑ์๊ฐ ์์ผ๋ฉด ๋๋ฝ๋ ์ธ์์ ๋ํ ์ค๋ฅ๋ก dataProvider ์ฃผ์์ด ์ค๋จ๋ฉ๋๋ค.
PHPUnit 3.6.12 by Sebastian Bergmann.
...E
Time: 0 seconds, Memory: 3.25Mb
There was 1 error:
1) Test::testTheProvider
Missing argument 1 for Test::testTheProvider()
/home/nick/development/PHP-1.php:43
FAILURES!
Tests: 4, Assertions: 5, Errors: 1.
์์ฑ์๋ฅผ ์ ๊ฑฐํ๋ฉด testTheProvider๊ฐ ๋ค์ ์๋ํ๊ณ @depends ์ฃผ์์ด ์์๋์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
<?php
class Test extends PHPUnit_Framework_TestCase {
public function __construct() {
parent::__construct();
}
public function testEmpty() {
$stack = array();
$this->assertEmpty($stack);
return $stack;
}
/**
* <strong i="10">@depends</strong> testEmpty
*/
public function testPush(array $stack) {
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertNotEmpty($stack);
return $stack;
}
/**
* <strong i="11">@depends</strong> testPush
*/
public function testPop(array $stack) {
$this->assertEquals('foo', array_pop($stack));
$this->assertEmpty($stack);
}
public function theProvider() {
return array(
array(true, false)
);
}
/**
* <strong i="12">@dataProvider</strong> theProvider
*/
public function testTheProvider($var1, $var2) {
//$var1 = true;
//$var2 = false;
$this->assertNotEquals($var1, $var2);
}
}
?>
์์๋๋ ๋์์
๋๋ค. PHPUnit_Framework_TestCase::__construct()
๋ฅผ ์ฌ์ ์ํ๋ฉด(ํด์๋ ์ ๋จ) ๊ตฌํ์์ ์๋ ๊ตฌํ์ ๋ชจ๋ ์ธ์๋ฅผ ์๋ฝํ๊ณ parent::__construct()
์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
๋ช ํํ๊ฒ ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๊ทธ๋ฌ๋ PHPUnit_Framework_TestCase์ ์์ฑ์๋ ์ธ์์ ๋ํ ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ๋ฉฐ ์ด์จ๋ ๋์ผํ๊ฒ ์ ์๋ฉ๋๋ค. ํน๋ณํ ๋ณ๊ฒฝํ๋ ค๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฉด ํธ์ถํด๋ ์๋ฌด๋ฐ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
๋ํ ์ด๊ฒ์ ๋ฌธ์(http://www.phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestCase)์ ์ธ๊ธ๋์ด ์์ง ์์ผ๋ฉฐ DBUnit BankAccount ์ํ๋ ์ด ์์ ์ ์ํํฉ๋๋ค(https:/ /github.com/sebastianbergmann/dbunit/blob/master/Samples/BankAccountDB/BankAccountDBTest.php#L60).
์ผ๋ถ ์ฃผ์์ ์๋ํ๊ณ ์ผ๋ถ ์ฃผ์์ ์ด๊ฒ ๋๋ฌธ์ ์๋ํ์ง ์๋ ๊ฒฝ์ฐ ์์๋ ๋์์ด๋ผ๊ณ ๋งํ์ง ์์ ๊ฒ์ ๋๋ค.
๋๋ ๋ค์๊ณผ ๊ฐ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
ํด๋์ค ํ
์คํธ ํ์ฅ PHPUnit_Framework_TestCase {
๊ณต๊ฐ ํจ์ __construct($name = NULL, ๋ฐฐ์ด $data = array(), $dataName = '') {
๋ถ๋ชจ::__construct($์ด๋ฆ, $๋ฐ์ดํฐ, $๋ฐ์ดํฐ์ด๋ฆ);
}
}
์๋
,
์ด ๋ต๋ณ์ ์ฐพ์ ๊ฒ์ ์ด์ด ์ข์๊ณ ๋ง์ ์๊ฐ์ ์ ์ฝํ์ต๋๋ค. ํ์ด, @foued611
๋ด ๊ฒฝ์ฐ๋ ์ด์ ๊ฐ์ผ๋ฉฐ ์คํจํฉ๋๋ค.
# my base test class
use PHPUnit_Framework_TestCase;
class mytest extends PHPUnit_Framework_TestCase {
public function __construct() {
# do some common stuff to my tests...
}
}
class IntegrationTest extends mytest {
public function __construct() {
parent::__construct();
}
/**
* <strong i="9">@dataProvider</strong> additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return [
'adding zeros' => [0, 0, 0],
'zero plus one' => [0, 1, 1],
'one plus zero' => [1, 0, 1],
'one plus one' => [1, 1, 3]
];
}
}
๊ณ ์ ๋ฐ ์๋ ๋ฒ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
# my base test class
use PHPUnit_Framework_TestCase;
class mytest extends PHPUnit_Framework_TestCase {
public function __construct() {
# do some common stuff to my tests...
parent::__construct(...func_get_args());
}
}
class IntegrationTest extends mytest {
public function __construct($name = NULL, array $data = array(), $dataName = '') {
parent::__construct(...func_get_args());
}
/**
* <strong i="13">@dataProvider</strong> additionProvider
*/
public function testAdd($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return [
'adding zeros' => [0, 0, 0],
'zero plus one' => [0, 1, 1],
'one plus zero' => [1, 0, 1],
'one plus one' => [1, 1, 3]
];
}
}
ํ์ฌ PHPUnit ๋ฌธ์๋ dataProvider๋ฅผ ์ฌ์ฉํ๋ ํ
์คํธ ํด๋์ค์ ๋ํด ์ฌ์ฉ์ ์ ์ __constructor()๋ฅผ ์ถ๊ฐํ์ง ์๋ ๋ฐฉ์์ผ๋ก ๊ฒฝ๊ณ ํ์ง ์์ต๋๋ค. ํด๋น ์ ๋ณด๋ ํ์ฌ ๋ฐ์ดํฐ ๊ณต๊ธ์ ๋ฌธ์์ ์ถ๊ฐํ ์ ์์ต๋๋ค. ์ต์ํ ์์ dataProvider ์ฝ๋๋ฅผ ๋ณต์ฌํ์ฌ ๋ถ์ฌ๋ฃ์ ๊ฒฝ์ฐ์๋ ๊ตฌํ์ด ์๋ํ์ง ์๋ ์ด์ ๋ฅผ ์๋ ค์ค ๊ฒ์
๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ฌ์ฉ์ ์ ์ __constructor๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ ์๋ํ๋๋ก ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ํ ์ง์นจ์ ์ถ๊ฐํ์ธ์.
๋ซํ์ ํ @sebastianbergmann
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ๋ค์๊ณผ ๊ฐ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
ํด๋์ค ํ ์คํธ ํ์ฅ PHPUnit_Framework_TestCase {
๊ณต๊ฐ ํจ์ __construct($name = NULL, ๋ฐฐ์ด $data = array(), $dataName = '') {
๋ถ๋ชจ::__construct($์ด๋ฆ, $๋ฐ์ดํฐ, $๋ฐ์ดํฐ์ด๋ฆ);
}
}