<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://trans.onionmixer.net/wiki/index.php?action=history&amp;feed=atom&amp;title=PHPUnitManual%3A4.3</id>
	<title>PHPUnitManual:4.3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://trans.onionmixer.net/wiki/index.php?action=history&amp;feed=atom&amp;title=PHPUnitManual%3A4.3"/>
	<link rel="alternate" type="text/html" href="https://trans.onionmixer.net/wiki/index.php?title=PHPUnitManual:4.3&amp;action=history"/>
	<updated>2026-05-02T09:42:57Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.3</generator>
	<entry>
		<id>https://trans.onionmixer.net/wiki/index.php?title=PHPUnitManual:4.3&amp;diff=3374&amp;oldid=prev</id>
		<title>Onionmixer: PHPUnit 4.3 예외 테스트 페이지 추가</title>
		<link rel="alternate" type="text/html" href="https://trans.onionmixer.net/wiki/index.php?title=PHPUnitManual:4.3&amp;diff=3374&amp;oldid=prev"/>
		<updated>2013-06-06T10:07:30Z</updated>

		<summary type="html">&lt;p&gt;PHPUnit 4.3 예외 테스트 페이지 추가&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;;4.3 예외테스트&lt;br /&gt;
&lt;br /&gt;
==예외 테스트==&lt;br /&gt;
&lt;br /&gt;
예제 4.7 &amp;quot;@expectedException 주석의 사용법&amp;quot; 은, 테스트할 코드 안에 예외가 throw 되었는지를 @expectedException 주석을 사용하여 조사하는 방법입니다.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
예제 4.7 &amp;quot;@expectedException 주석의 사용법&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class ExceptionTest extends PHPUnit_Framework_TestCase&lt;br /&gt;
{&lt;br /&gt;
	/**&lt;br /&gt;
	 * @expectedException InvalidArgumentException&lt;br /&gt;
	 */&lt;br /&gt;
	public function testException()&lt;br /&gt;
	{&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
PHPUnit 3.7.0 by Sebastian Bergmann.&lt;br /&gt;
F&lt;br /&gt;
Time: 0 seconds, Memory: 4.75Mb&lt;br /&gt;
There was 1 failure:&lt;br /&gt;
1) ExceptionTest::testException&lt;br /&gt;
Expected exception InvalidArgumentException&lt;br /&gt;
FAILURES!&lt;br /&gt;
Tests: 1, Assertions: 1, Failures: 1.phpunit ExceptionTest&lt;br /&gt;
PHPUnit 3.7.0 by Sebastian Bergmann.&lt;br /&gt;
F&lt;br /&gt;
Time: 0 seconds, Memory: 4.75Mb&lt;br /&gt;
There was 1 failure:&lt;br /&gt;
1) ExceptionTest::testException&lt;br /&gt;
Expected exception InvalidArgumentException&lt;br /&gt;
FAILURES!&lt;br /&gt;
Tests: 1, Assertions: 1, Failures: 1.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
또한, @expectedExceptionMessage 나 @expectedExceptionCode 를 @expectedException 과 조합하여 사용하면, 예외 메세지나 예외 코드를 예제 4.8 &amp;quot;@expectedExceptionMessage 및 @expectedExceptionCode 선언의 사용법&amp;quot; 에 나와 있는 것처럼 테스트할 수 있습니다.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
예제 4.8 @expectedExceptionMessage 및 @expectedExceptionCode 선언의 사용법&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class ExceptionTest extends PHPUnit_Framework_TestCase&lt;br /&gt;
{&lt;br /&gt;
	/**&lt;br /&gt;
	 * @expectedException&lt;br /&gt;
	 InvalidArgumentException&lt;br /&gt;
	 * @expectedExceptionMessage Right Message&lt;br /&gt;
	 */&lt;br /&gt;
	public function testExceptionHasRightMessage()&lt;br /&gt;
	{&lt;br /&gt;
		throw new InvalidArgumentException(&amp;#039;Some Message&amp;#039;, 10);&lt;br /&gt;
	}&lt;br /&gt;
	/**&lt;br /&gt;
	 * @expectedException&lt;br /&gt;
	 InvalidArgumentException&lt;br /&gt;
	 * @expectedExceptionCode 20&lt;br /&gt;
	 */&lt;br /&gt;
	public function testExceptionHasRightCode()&lt;br /&gt;
	{&lt;br /&gt;
		throw new InvalidArgumentException(&amp;#039;Some Message&amp;#039;, 10);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
PHPUnit 3.7.0 by Sebastian Bergmann.&lt;br /&gt;
FF&lt;br /&gt;
Time: 0 seconds, Memory: 3.00Mb&lt;br /&gt;
There were 2 failures:&lt;br /&gt;
1) ExceptionTest::testExceptionHasRightMessage&lt;br /&gt;
Failed asserting that exception message &amp;#039;Some Message&amp;#039; contains &amp;#039;Right Message&amp;#039;.&lt;br /&gt;
2) ExceptionTest::testExceptionHasRightCode&lt;br /&gt;
Failed asserting that expected exception code 20 is equal to 10.&lt;br /&gt;
FAILURES!&lt;br /&gt;
Tests: 2, Assertions: 4, Failures: 2.phpunit ExceptionTest&lt;br /&gt;
PHPUnit 3.7.0 by Sebastian Bergmann.&lt;br /&gt;
FF&lt;br /&gt;
Time: 0 seconds, Memory: 3.00Mb&lt;br /&gt;
There were 2 failures:&lt;br /&gt;
1) ExceptionTest::testExceptionHasRightMessage&lt;br /&gt;
Failed asserting that exception message &amp;#039;Some Message&amp;#039; contains &amp;#039;Right Message&amp;#039;.&lt;br /&gt;
2) ExceptionTest::testExceptionHasRightCode&lt;br /&gt;
Failed asserting that expected exception code 20 is equal to 10.&lt;br /&gt;
FAILURES!&lt;br /&gt;
Tests: 2, Assertions: 4, Failures: 2.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@expectedExceptionMessage 나 @expectedExceptionCode 를 사용한 다른 예가 &amp;quot;@expectedExceptionMessage&amp;quot; 와 &amp;quot;@expectedExceptionCode&amp;quot; 에 있습니다.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
한편, setExpectedException() 메소드를 사용하여 발생할 예외를 지정할 수도 있습니다. 지정하는 방법이 예제 4.9 &amp;quot;테스트 대상 코드에서 발생할 예외의 지정&amp;quot; 에 나와 있습니다.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
예제 4.9 테스트 대상 코드에서 발생할 예외의 지정&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class ExceptionTest extends PHPUnit_Framework_TestCase&lt;br /&gt;
{&lt;br /&gt;
	public function testException()&lt;br /&gt;
	{&lt;br /&gt;
		$this-&amp;gt;setExpectedException(&amp;#039;InvalidArgumentException&amp;#039;);&lt;br /&gt;
	}&lt;br /&gt;
	public function testExceptionHasRightMessage()&lt;br /&gt;
	{&lt;br /&gt;
		$this-&amp;gt;setExpectedException(&lt;br /&gt;
				&amp;#039;InvalidArgumentException&amp;#039;, &amp;#039;Right Message&amp;#039;&lt;br /&gt;
				);&lt;br /&gt;
		throw new InvalidArgumentException(&amp;#039;Some Message&amp;#039;, 10);&lt;br /&gt;
	}&lt;br /&gt;
	public function testExceptionHasRightCode()&lt;br /&gt;
	{&lt;br /&gt;
		$this-&amp;gt;setExpectedException(&lt;br /&gt;
				&amp;#039;InvalidArgumentException&amp;#039;, &amp;#039;Right Message&amp;#039;, 20&lt;br /&gt;
				);&lt;br /&gt;
		throw new InvalidArgumentException(&amp;#039;The Right Message&amp;#039;, 10);&lt;br /&gt;
	}&lt;br /&gt;
}?&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
PHPUnit 3.7.0 by Sebastian Bergmann.&lt;br /&gt;
FFF&lt;br /&gt;
Time: 0 seconds, Memory: 3.00Mb&lt;br /&gt;
There were 3 failures:&lt;br /&gt;
1) ExceptionTest::testException&lt;br /&gt;
Expected exception InvalidArgumentException&lt;br /&gt;
2) ExceptionTest::testExceptionHasRightMessage&lt;br /&gt;
Failed asserting that exception message &amp;#039;Some Message&amp;#039; contains &amp;#039;Right Message&amp;#039;.&lt;br /&gt;
3) ExceptionTest::testExceptionHasRightCode&lt;br /&gt;
Failed asserting that expected exception code 20 is equal to 10.&lt;br /&gt;
FAILURES!&lt;br /&gt;
Tests: 3, Assertions: 6, Failures: 3.phpunit ExceptionTest&lt;br /&gt;
PHPUnit 3.7.0 by Sebastian Bergmann.&lt;br /&gt;
FFF&lt;br /&gt;
Time: 0 seconds, Memory: 3.00Mb&lt;br /&gt;
There were 3 failures:&lt;br /&gt;
1) ExceptionTest::testException&lt;br /&gt;
Expected exception InvalidArgumentException&lt;br /&gt;
2) ExceptionTest::testExceptionHasRightMessage&lt;br /&gt;
Failed asserting that exception message &amp;#039;Some Message&amp;#039; contains &amp;#039;Right Message&amp;#039;.&lt;br /&gt;
3) ExceptionTest::testExceptionHasRightCode&lt;br /&gt;
Failed asserting that expected exception code 20 is equal to 10.&lt;br /&gt;
FAILURES!&lt;br /&gt;
Tests: 3, Assertions: 6, Failures: 3.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
표4.1 &amp;quot;예외 테스트용 메소드&amp;quot; 는 예외를 테스트하기 위해 준비된 메소드들을 정리한 것입니다.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;border: 1px solid black;&amp;quot;&lt;br /&gt;
|- style=&amp;quot;color: white; background-color: black;&amp;quot;&lt;br /&gt;
|&amp;#039;&amp;#039;&amp;#039;메소드&amp;#039;&amp;#039;&amp;#039;||&amp;#039;&amp;#039;&amp;#039;의미&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
|void setExpectedException(string $exceptionName[, string $exceptionMessage = &amp;#039;&amp;#039;, integer $exceptionCode = NULL])||예상되는 $exceptionName, $exceptionMessage 및 $exceptionCode 를 설정합니다.&lt;br /&gt;
|- style=&amp;quot;vertical-align:top;&amp;quot;&lt;br /&gt;
|String getExpectedException()||발생할 것이 예상되는 예외의 이름을 반환합니다&lt;br /&gt;
|- style=&amp;quot;color: black; background-color: gray;&amp;quot;&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; |표4.1 예외 테스트용 메소드&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
한편, 예제 4.10 &amp;quot;예외를 테스트하는 다른 방법&amp;quot; 과 같은 방법으로 예외를 테스트할 수도 있습니다.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
예제 4.10 예외를 테스트하는 다른 방법&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
class ExceptionTest extends PHPUnit_Framework_TestCase {&lt;br /&gt;
	public function testException() {&lt;br /&gt;
		try {&lt;br /&gt;
			// ... 예외를 발생시키는 코드 ...&lt;br /&gt;
		}&lt;br /&gt;
		catch (InvalidArgumentException $expected) {&lt;br /&gt;
			return;&lt;br /&gt;
		}&lt;br /&gt;
		$this-&amp;gt;fail(&amp;#039;예상한 예외가 발생하지 않았습니다.&amp;#039;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
예외가 발생할 예정인 예 4.10 &amp;quot;예외를 테스트하는 다른 방법&amp;quot; 에서 예외가 발생하지 않은 경우, 이어지는 fail() 로 인해 테스트가 종료하여 문제가 보고됩니다. 예상대로 예외가 발생한 경우, catch 블록이 실행되어 테스트가 정상 종료됩니다&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:PHPUnitManual]]&lt;/div&gt;</summary>
		<author><name>Onionmixer</name></author>
	</entry>
</feed>