Phpunit: ๋ฐ์ดํ„ฐ ๊ณต๊ธ‰์ž ์ธ์ˆ˜ ๊ฐœ์ˆ˜ ์˜ค๋ฅ˜

์— ๋งŒ๋“  2016๋…„ 12์›” 19์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: sebastianbergmann/phpunit

์ตœ์‹  ๋ฒ„์ „์˜ 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']
       ];
    }
}

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์ด ๋ฌธ์ œ์— ์ง๋ฉดํ–ˆ๋‹ค๋ฉด.
๋‚˜๋Š”์ด ํ…Œ์ŠคํŠธ๋ฅผํ–ˆ๋‹ค

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์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๊ณ  ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  4 ๋Œ“๊ธ€

์ œ๊ณตํ•˜์‹  ์ •๋ณด๋กœ๋Š” ๋ฌธ์ œ๋ฅผ ์žฌํ˜„ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ํ•œ ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

$ 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๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰