์ต์ ๋ฒ์ ์ PHPUnit(5.7.4)์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ๋ฌธ์ ๋ ํ ์คํธ๋ฅผ ์คํํ ๋ ํต๊ณผํ๋ค๋ ๊ฒ์ ๋๋ค. ๊ทธ๋ฌ๋ ๋ค์ ํ ๋ฒ ์คํํ๋ฉด ๋ค์ ์ค๋ฅ์ ํจ๊ป ์คํจํฉ๋๋ค.
ArgumentCountError: Too few arguments to function ValidationTest::testValidateType(), 0 passed and at least 3 expected
๋ฐ์ดํฐ ๊ณต๊ธ์ ํจ์๋ฅผ ๋ณ๊ฒฝํ๊ณ (์ฆ, ๋ฐํํ ๋ฐ์ดํฐ, ๊ณต๊ธ์ ํจ์ ์ด๋ฆ ๋๋ ์ ํ ๋ฑ์ ๋ณ๊ฒฝ) ๋ค์ ์คํํ๋ฉด ํ ๋ฒ๋ง ํต๊ณผํ๊ณ ๋ชจ๋ ์ฐ์ ํ ์คํธ ์คํ์ ๋ํด ์์ ์ค๋ฅ์ ํจ๊ป ์คํจํฉ๋๋ค.
ํ์คํ์ง ์์ง๋ง PHPUnit์ ์บ์ฑ ๋ฉ์ปค๋์ฆ์ ์ฌ์ฉํ์ฌ ๊ณต๊ธ์ ๋ฐ์ดํฐ๋ฅผ ์บ์ํฉ๋๊น? ๊ทธ๋ ๋ค๋ฉด ์ฒญ์ํ ๋ฐฉ๋ฒ์ด ์์ต๋๊น( setUp
๋๋ tearDown
)?
๋ค์ ์ฝ๋๋ ํ ๋ฒ ์๋ํฉ๋๋ค(ํ ๋ฒ ์ ๋ฌ).
/**
* <strong i="15">@covers</strong> Validation
* <strong i="16">@coversDefaultClass</strong> Validation
*/
class ValidationTest extends TestCase {
protected $validation;
protected function setUp() {
$this->validation = new Validation();
}
/**
* <strong i="17">@covers</strong> ::validateType
* <strong i="18">@dataProvider</strong> validateTypeProdiver
*/
public function testValidateType($assertion, $argument, $type) {
$result = $this->validation->validateType($argument, $type);
switch ($assertion) {
case 'True':
$this->assertTrue($result);
break;
}
}
public function validateTypeProdiver() {
return [
['True', 'file.txt', 'str']
];
}
}
๋ค์ ์ฝ๋๋ ํญ์ ์๋ํฉ๋๋ค(๋งค๋ฒ ํต๊ณผ).
/**
* <strong i="24">@covers</strong> Validation
* <strong i="25">@coversDefaultClass</strong> Validation
*/
class ValidationTest extends TestCase {
protected $validation;
protected function setUp() {
$this->validation = new Validation();
}
/**
* <strong i="26">@covers</strong> ::validateType
*/
public function testValidateType() {
foreach ($this->validateTypeProdiver() as $args) {
$result = call_user_func_array([$this->validation, 'validateType'], array_slice($args, 1));
switch ($args[0]) {
case 'True':
$this->assertTrue($result);
break;
}
}
}
public function validateTypeProdiver() {
return [
['True', 'file.txt', 'str']
];
}
}
์ ๊ณตํ์ ์ ๋ณด๋ก๋ ๋ฌธ์ ๋ฅผ ์ฌํํ ์ ์์ต๋๋ค. ๋ด๊ฐ ํ ์ผ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
$ composer require phpunit/phpunit:5.7.4
$ vendor/bin/phpunit && vendor/bin/phpunit
$ vendor/bin/phpunit && vendor/bin/phpunit
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml
. 1 / 1 (100%)
Time: 44 ms, Memory: 4.00MB
OK (1 test, 1 assertion)
PHPUnit 5.7.4 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.17-2+deb.sury.org~trusty+1
Configuration: /.../phpunit.xml
. 1 / 1 (100%)
Time: 32 ms, Memory: 4.00MB
OK (1 test, 1 assertion)
์ด๊ฒ์ ๋ด๊ฐ ์ฌ์ฉํ ํ ์คํธ ํด๋์ค์ ๋๋ค.
use PHPUnit\Framework\TestCase;
class TestMeTest extends TestCase
{
protected $validation;
protected function setUp()
{
$this->validation = new Validation();
}
/**
* <strong i="9">@covers</strong> ::validateType
* <strong i="10">@dataProvider</strong> validateTypeProdiver
*/
public function testValidateType($assertion, $argument, $type)
{
$result = $this->validation->validateType($argument, $type);
switch ($assertion) {
case 'True':
$this->assertTrue($result);
break;
}
}
public function validateTypeProdiver()
{
return [
['True', 'file.txt', 'str']
];
}
}
๊ทธ๋ฆฌ๊ณ Validation
ํด๋์ค:
class Validation
{
public function validateType() {
return true;
}
}
PHPUnit์ ๋ฐ์ดํฐ ๊ณต๊ธ์๋ฅผ ์ง์์ ์ผ๋ก ์บ์ํ์ง ์์ต๋๋ค.
๋๋ ๋ํ ์ด๊ฒ์ ์ฌํ ํ ์ ์์ต๋๋ค.
๋๊ตฐ๊ฐ๊ฐ ์ด ๋ฌธ์ ์ ์ง๋ฉดํ๋ค๋ฉด.
๋๋์ด ํ
์คํธ๋ฅผํ๋ค
class Test extends TestCase {
protected $tested;
public function __construct()
{
parent::__construct();
$this->tested = new TestedClass();
}
public function provider(): array
{
return array(
array(0, 1, 1),
array(1, 2, 3),
);
}
/**
* <strong i="7">@dataProvider</strong> provider
*/
public function testSum(int $first, int $second, int $expected)
{
$this->assertEquals($expected, $this->tested->sum($first + $second));
}
}
__construct๋ฅผ setUp์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์๋ํฉ๋๋ค.
@dmirogin ์ ์๋ฃจ์ ์ ๋ด ํ๋ฃจ, phpunit 8.5๋ฅผ ๋ง๋ค์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๊ตฐ๊ฐ๊ฐ ์ด ๋ฌธ์ ์ ์ง๋ฉดํ๋ค๋ฉด.
๋๋์ด ํ ์คํธ๋ฅผํ๋ค
__construct๋ฅผ setUp์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์๋ํฉ๋๋ค.