PHPUnitManual:10

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.
제10장 테스트 더블(Test Doubles)

Gerard Meszaros 는 [Meszaros2007] 에서 테스트 더블의 개념을 다음과 같이 표현했습니다.

Sometimes it is just plain hard to test the system under test (SUT) because it depends on other components that cannot be used in the test environment. This could be because they aren't available, they will not return the results needed for the test or because executing them would have undesirable side effects. In other cases, our test strategy requires us to have more control or visibility of the internal behavior of the SUT.
테스트 대상 시스템 (SUT:system under test) 를 테스트하는 것은 때로 매우 어려운 일입니다. 그 이유는, 시스템이 의존하는 다른 컴포넌트를 테스트 환경에서 사용할 수 없는 경우가 있기 때문입니다. 애초에 사용 불가능하거나, 테스트를 위한 결과를 반환하지 않거나, 혹은 부작용이 있거나 하는 경우입니다. 다른 경우에도, 테스트 환경의 내부적인 동작을 제어하여 가시화할 필요가 있습니다.
When we are writing a test in which we cannot (or chose not to) use a real depended-on component (DOC), we can replace it with a Test Double. The Test Double doesn't have to behave exactly like the real DOC; it merely has to provide the same API as the real one so that the SUT thinks it is the real one!
실제로 의존하는 컴포넌트 (DOC:depended-on component) 를 사용하지 않는 테스트를 작성하는 경우, 이를 테스트 더블로 대체할 수 있습니다. 테스트 더블은, 실제 DOC 와 완벽하게 동일하게 동작할 필요는 없습니다. 단순히, 실제와 같은 API 를 제공하여, SUT 를 속일 수 있으면 됩니다.
--Gerard Meszaros


PHPUnit 의 getMock($className) 메소드를 사용하여, 지정한 대상 클래스의 테스트 더블의 역할을 하는 오브젝트를 자동 생성할 수 있습니다. 이 테스트 더블 오브젝트는, 대상 클래스의 오브젝트를 필요로 하는 모든 경우에 사용할 수 있습니다.


기본 설정으로 대상 클래스의 모든 메소드를 대체하여, (대상 클래스를 호출하지 않고) NULL 을 반환하는 더미가 구현됩니다. 예를 들어서, will($this->returnValue()) 메소드의 경우, 호출되면 더미 구현이 값을 반환하도록 설정할 수 있습니다.


제한사항
final, private, static 메소드들의 stub 와 mock 은 생성할 수 없습니다. PHPUnit 의 테스트 더블 기능에서는 이 메소드들의 stub 와 mock 을 무시하고 대상 메소드의 동작을 그대로 유지합니다.


경고
파라메터의 관리 방법이 변경되었음에 유의해주시기 바랍니다. 과거의 구현에서는 객체의 모든 파라메터를 복제하여 메소드에 넘겨진 오브젝트의 동일성을 확인할 수 없었습니다. 예10.14 "한 메소드를 호출하여 동일한 메소드가 넘겨졌는지를 조사하는 테스트" 는 새로운 구현의 활용 예입니다. 예10.15 "파라메터의 복제를 가능하게 한 mock 오브젝트 작성" 은 과거의 방식으로 사용하는 방법의 예입니다.


Notes