PHPUnitManual:4.3

From 흡혈양파의 번역工房
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
4.3 예외테스트

예외 테스트

예제 4.7 "@expectedException 주석의 사용법" 은, 테스트할 코드 안에 예외가 throw 되었는지를 @expectedException 주석을 사용하여 조사하는 방법입니다.


예제 4.7 "@expectedException 주석의 사용법"

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
	/**
	 * @expectedException InvalidArgumentException
	 */
	public function testException()
	{
	}
}
?>
PHPUnit 3.7.0 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 4.75Mb
There was 1 failure:
1) ExceptionTest::testException
Expected exception InvalidArgumentException
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.phpunit ExceptionTest
PHPUnit 3.7.0 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 4.75Mb
There was 1 failure:
1) ExceptionTest::testException
Expected exception InvalidArgumentException
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.


또한, @expectedExceptionMessage 나 @expectedExceptionCode 를 @expectedException 과 조합하여 사용하면, 예외 메세지나 예외 코드를 예제 4.8 "@expectedExceptionMessage 및 @expectedExceptionCode 선언의 사용법" 에 나와 있는 것처럼 테스트할 수 있습니다.


예제 4.8 @expectedExceptionMessage 및 @expectedExceptionCode 선언의 사용법

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
	/**
	 * @expectedException
	 InvalidArgumentException
	 * @expectedExceptionMessage Right Message
	 */
	public function testExceptionHasRightMessage()
	{
		throw new InvalidArgumentException('Some Message', 10);
	}
	/**
	 * @expectedException
	 InvalidArgumentException
	 * @expectedExceptionCode 20
	 */
	public function testExceptionHasRightCode()
	{
		throw new InvalidArgumentException('Some Message', 10);
	}
}
?>
PHPUnit 3.7.0 by Sebastian Bergmann.
FF
Time: 0 seconds, Memory: 3.00Mb
There were 2 failures:
1) ExceptionTest::testExceptionHasRightMessage
Failed asserting that exception message 'Some Message' contains 'Right Message'.
2) ExceptionTest::testExceptionHasRightCode
Failed asserting that expected exception code 20 is equal to 10.
FAILURES!
Tests: 2, Assertions: 4, Failures: 2.phpunit ExceptionTest
PHPUnit 3.7.0 by Sebastian Bergmann.
FF
Time: 0 seconds, Memory: 3.00Mb
There were 2 failures:
1) ExceptionTest::testExceptionHasRightMessage
Failed asserting that exception message 'Some Message' contains 'Right Message'.
2) ExceptionTest::testExceptionHasRightCode
Failed asserting that expected exception code 20 is equal to 10.
FAILURES!
Tests: 2, Assertions: 4, Failures: 2.


@expectedExceptionMessage 나 @expectedExceptionCode 를 사용한 다른 예가 "@expectedExceptionMessage" 와 "@expectedExceptionCode" 에 있습니다.


한편, setExpectedException() 메소드를 사용하여 발생할 예외를 지정할 수도 있습니다. 지정하는 방법이 예제 4.9 "테스트 대상 코드에서 발생할 예외의 지정" 에 나와 있습니다.


예제 4.9 테스트 대상 코드에서 발생할 예외의 지정

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
	public function testException()
	{
		$this->setExpectedException('InvalidArgumentException');
	}
	public function testExceptionHasRightMessage()
	{
		$this->setExpectedException(
				'InvalidArgumentException', 'Right Message'
				);
		throw new InvalidArgumentException('Some Message', 10);
	}
	public function testExceptionHasRightCode()
	{
		$this->setExpectedException(
				'InvalidArgumentException', 'Right Message', 20
				);
		throw new InvalidArgumentException('The Right Message', 10);
	}
}?>
PHPUnit 3.7.0 by Sebastian Bergmann.
FFF
Time: 0 seconds, Memory: 3.00Mb
There were 3 failures:
1) ExceptionTest::testException
Expected exception InvalidArgumentException
2) ExceptionTest::testExceptionHasRightMessage
Failed asserting that exception message 'Some Message' contains 'Right Message'.
3) ExceptionTest::testExceptionHasRightCode
Failed asserting that expected exception code 20 is equal to 10.
FAILURES!
Tests: 3, Assertions: 6, Failures: 3.phpunit ExceptionTest
PHPUnit 3.7.0 by Sebastian Bergmann.
FFF
Time: 0 seconds, Memory: 3.00Mb
There were 3 failures:
1) ExceptionTest::testException
Expected exception InvalidArgumentException
2) ExceptionTest::testExceptionHasRightMessage
Failed asserting that exception message 'Some Message' contains 'Right Message'.
3) ExceptionTest::testExceptionHasRightCode
Failed asserting that expected exception code 20 is equal to 10.
FAILURES!
Tests: 3, Assertions: 6, Failures: 3.


표4.1 "예외 테스트용 메소드" 는 예외를 테스트하기 위해 준비된 메소드들을 정리한 것입니다.


메소드 의미
void setExpectedException(string $exceptionName[, string $exceptionMessage = , integer $exceptionCode = NULL]) 예상되는 $exceptionName, $exceptionMessage 및 $exceptionCode 를 설정합니다.
String getExpectedException() 발생할 것이 예상되는 예외의 이름을 반환합니다
표4.1 예외 테스트용 메소드


한편, 예제 4.10 "예외를 테스트하는 다른 방법" 과 같은 방법으로 예외를 테스트할 수도 있습니다.


예제 4.10 예외를 테스트하는 다른 방법

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase {
	public function testException() {
		try {
			// ... 예외를 발생시키는 코드 ...
		}
		catch (InvalidArgumentException $expected) {
			return;
		}
		$this->fail('예상한 예외가 발생하지 않았습니다.');
	}
}
?>


예외가 발생할 예정인 예 4.10 "예외를 테스트하는 다른 방법" 에서 예외가 발생하지 않은 경우, 이어지는 fail() 로 인해 테스트가 종료하여 문제가 보고됩니다. 예상대로 예외가 발생한 경우, catch 블록이 실행되어 테스트가 정상 종료됩니다


Notes