Mark Incomplete

새로운 테스트 케이스 클래스를 만들고 아래와 같은 테스트 메소드를 작성중인데 단위 테스트를 수행해야 합니다.

이 테스트 메소드는 아직 작성되지 않았지만 phpunit 에서 성공적으로 실행되는데 이는 별로 의미있는 정보가 아닙니다.

public function testSomething()
{
}
CODE

그렇다고 $this->fail() 를 호출해서 실패하게 만드는 것도 그리 좋은 방법은 아닙니다. 아직 미구현일 뿐이지 테스트 케이스가 실패한 것은 아니니까요.

 

phpunit 에는 이를 위해 markTestIncomplete() 메소드가 준비되어 있습니다. 미작성된 단위 테스트 메소드에서 호출하면 phpunit 은 이를 실행하지 않고 넘어 갑니다.

 

 

<?php
class SampleTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        // Optional: Test anything here, if you want.
        $this->assertTrue(TRUE, 'This should already work.');

        // Stop here and mark this test as incomplete.
        $this->markTestIncomplete(
          'This test has not been implemented yet.'
        );
    }
}
?>

CODE

이제 phpunit 을 실행하면 markTestIncomplete 로 설정된 부분은 실행하지 않고 최종 결과 보고서의 Incomplete 항목에 표시되므로 몇 개의 테스트 메소드가 미완성인지 알수 있습니다.

$ phpunit --verbose tests/SampleTest
 
Time: 130 ms, Memory: 5.00Mb
There was 1 incomplete test:
1) SampleTest::testSomething
This test has not been implemented yet.


tests/SampleTest.php:11
phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/Command.php:152
phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/Command.php:104
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 1, Incomplete: 1.
CODE

 

 

Skip Test

환경에 따라서 특정 테스트 케이스는 실행되지 말아야 하는 경우가 있습니다. 예로 DBMS 를 추상화하여 사용하고 있고 개발 환경에서는 sqlite 를 운영 환경에서는 MySQL 을 사용하고 있다면 개발 환경 단위 테스트시 MySQL 과 연결하는 테스트 메소드를 실행할 필요는 없습니다.

phpunit 은 이를 위해 markTestSkipped 메소드를 제공하고 있으며 이 메소드가 있을 경우 테스트 케이스를 건너뜁니다.

<?php
class DatabaseTest extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        if (!extension_loaded('oci')) {
            $this->markTestSkipped(
              'The Oracle extension is not available.'
            );
        }
    }

    public function testConnection()
    {
        // ...
    }
}
?>
CODE

 

실행하면 아래와 같이 테스트 케이스를 건너뛰고 보고서의 Skipped: 항목에 추가됩니다.

$ phpunit --verbose tests/DatabaseTest.php 
 
Time: 146 ms, Memory: 5.00Mb
There was 1 skipped test:
1) DatabaseTest::testConnection
The Oracle extension is not available.

tests/DatabaseTest.php:9
phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/Command.php:152
phar:///usr/local/bin/phpunit.phar/phpunit/TextUI/Command.php:104


OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Skipped: 1.
CODE

 

Ref