SqueakByExample:7.8: Difference between revisions

From 흡혈양파의 번역工房
Jump to navigation Jump to search
(번역수정)
 
(번역수정)
Line 1: Line 1:
==SUnit 의 내부구현==
==SUnit 의 내부구현==


SUnit의 실행은 스몰토크 프레임 워크의 흥미로운 케이스 스터디를 만듭니다. 다음 테스트의 실행에 대한 구현의 몇 가지 핵심 측면들을 살펴봅시다.
SUnit의 내부구현은 스몰토크 프레임웍에 대한 흥미로운 사례가 되기도 합니다. 이제부터 테스트의 실행과정을 통해서 SUnit 내부구현중 핵심이 되는 부분을 살펴보겠습니다.




Line 7: Line 7:
===테스트 실행하기===
===테스트 실행하기===


어떤 테스트를 실행하기 위해, 우리는 표현식 (aTestClass selector: aSymbol)run을 평가합니다.
'''(aTestClass selector: aSymbol) run''' 을 진행해서 프로그램식에 대한 테스트를 해보도록 하겠습니다.  
 


[[image:sunit-scenario.png|none|664px|thumb|그림 7.3: 테스트 실행하기]]
[[image:sunit-scenario.png|none|664px|thumb|그림 7.3: 테스트 실행하기]]




메서드 TestCase»run은 테스트들의 결과들을 축적할 TestResult의 인스턴스를 만들고, 그 다음 자신에게 message run:을 보냅니다. (그림 7.3을 보십시오)
TestCase>>run: 메서드의 구현을 보면, 테스트의 결과를 모으기 위해서 TestResult 의 인스턴스를 만들어서 자기 자신에게 run: 메세지를 전송합니다. (그림 7.3을 참고해주세요)




메서드 7.10: test case 실행하기
메서드 7.10: test case 실행
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestCase»run
TestCase>>run
   | result |
   | result |
   result := TestResult new.
   result := TestResult new.
Line 26: Line 25:




메서드 TestCase»run: 는 메시지 runCase:테스트 결과에 발송합니다.
TestCase>>run: 메서드는 runCase: 라는 메시지를 테스트 결과로 전송합니다.
 


메서드 7.11: test case를 테스트 결과에 패스하기
메서드 7.11: test case 를 테스트 결과로 전달하기
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestCase»run: aResult
TestCase>>run: aResult
   aResult runCase: self
   aResult runCase: self
</syntaxhighlight>
</syntaxhighlight>




메서드 TestResult»runCase:는 테스트를 실행하기 위해 개별적 테스트에 메시지 runCase를 발송합니다. TestResult»runCase는 테스트가 실행되는 동안 제기될 모든 예외들을 다루며, 메시지 runCase를 그것에 보내어 TestCase를 실행하고, 오류, 실패 그리고 통과 횟수를 카운트합니다.
TestResult>>runCase: 메서드는, 인수로서 건네받은 TestCase 에 runCase 메세지를 송신합니다. 그 다음 테스트로 에러가 발생한 회수와 실패한 회수, 성공한 회수를 카운트합니다. 예외 핸들러를 설정하고, 예외의 발생과 assertion의 실패에 대비합니다.




Line 61: Line 59:




메서드 TestCase»runCase는 메시지 setUp 와 tearDown을 아래에 보이는 것 처럼 발송합니다.  
TestCase>>runCase 메서드는 setUp 메시지와 tearDown 메시지를 아래의 예처럼 전송합니다.  
 


메서드 7.13: Test case 템플릿 메서드 (Test Case Template Method)
메서드 7.13: Test case 템플릿 메서드 (Test Case Template Method)
Line 70: Line 67:
   [self performTest] ensure: [self tearDown]
   [self performTest] ensure: [self tearDown]
</syntaxhighlight>
</syntaxhighlight>




===TestSuite 실행하기===
===TestSuite 실행하기===


한 개 이상의 테스트를 실행하기 위해, 우리는 관련된 테스트들을 포함하고 있는 TestSuite에 메시지를 발송합니다. TestCase 클래스는 그것의 메서드들로부터, 테스트 슈트(test suite)를 구축하기 위해 몇 가지 기능을 제공합니다. 표현식 MyTestCase buildSuiteFromSelectors는 MyTestCase 클래스에서 정의된 모든 테스트들을 포함하고 있는 수트를 리턴합니다. 이 프로세스의 코어 수는 다음과 같습니다.
여러개의 테스트를 한번에 실행하기 위해, 관련된 테스트들을 포함하고 있는 TestSuite 에 메시지를 발송합니다. TestCase 클래스는 그것의 메서드들로부터, 테스트 슈트(test suite)를 구축하기 위해 몇 가지 기능을 제공합니다. 프로그램식 MyTestCase buildSuiteFromSelectors 는 MyTestCase 클래스에서 정의된 모든 테스트들을 포함하고 있는 suite 를 리턴합니다. 이 진행과정에서 핵심은 아래부분입니다.




Line 85: Line 83:




메서드 TestSuite»run는 TestResult의 인스턴스를 만들고 모든 리소스가 사용가능한지를 확인하고 그 다음 자신에게 실행중인 메시지를 발송합니다: 그 작업은 슈트에 있는 모든 테스트를 실행시킵니다. 그 이후 모든 리소스가 릴리즈됩니다.
TestSuite>>run 메서드는 TestResult 의 인스턴스를 만들고 모든 리소스가 사용가능한지를 확인한 뒤, 그 다음 자신에게 run: 메시지를 전송하게 되는데, 이 작업은 suite 에 속한 모든 테스트를 실행시키게 되빈다. 그 이후 모든 리소스가 해제됩니다.




메서드 7.15: 테스트 수트 실행하기
메서드 7.15: 테스트 suite 실행하기
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestSuite»run
TestSuite>>run
   | result |
   | result |
   result := TestResult new.
   result := TestResult new.
Line 102: Line 100:




메서드 7.16: 테스트 결과에 테스트 수트 통과하기
메서드 7.16: TestResult 를 TestSuite 로 전달하기
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestSuite»run: aResult
TestSuite>>run: aResult
   self tests do:
   self tests do:
     [:each |
     [:each |
Line 112: Line 110:




클래스 TestResource와 그것의 서브클래스들은 현재의 클래스 메서드를 사용하여 접근할 수 있고 만들 수 있는 현재 만든 그 하위 클래스들의 (클래스당 하나의)인스턴스들을 지속적으로 파악합니다. 이 인스턴스는 테스트가 실행을 마치거나 리소스가 리셋될 대 클리어 됩니다.
TestResource 클래스와 그것의 하위클래스들은 current 클래스 메서드를 사용하여 접근할 수 있으며, 만들어진 하위 클래스들의 (클래스당 하나의)인스턴스들을 지속적으로 파악합니다. 이 인스턴스는 테스트가 실행을 마치거나 리소스가 리셋될 때 자동으로 정리됩니다.


리소스 유효성 점검은 클래스 메서드 '''TestResource class»isAvailable'''에 보이는 것 처럼 필요할 경우 다시 만들어지는 작업을 가능하게 합니다. TestResouce 인스턴스 생성을 하는 동안, 그것은 초기화되고 메서드 setUp이 호출됩니다.  
리소스 유효성 점검은 클래스 메서드인 '''TestResource class>>isAvailable'''에서 확인할 수 있듯이 필요할 경우 다시 만들어지는 작업을 가능하게 합니다. TestResouce 의 인스턴스를 생성 할때, 인스턴스는 초기화되고 메서드 setUp 이 호출됩니다.




메서드 7.17: 테스트 리소스 유효성(Test resource availability)
메서드 7.17: 테스트 리소스 유효성(Test resource availability)
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestResource class»isAvailable
TestResource class>>isAvailable
   ↑self current notNil
   ↑self current notNil
</syntaxhighlight>
</syntaxhighlight>
Line 126: Line 124:
메서드 7.18:테스트 리소스 생성
메서드 7.18:테스트 리소스 생성
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestResource class»current
TestResource class>>current
   current isNil ifTrue: [current := self new].
   current isNil ifTrue: [current := self new].
   ↑current
   ↑current
Line 134: Line 132:
메서드 7.19: 테스트 리소스
메서드 7.19: 테스트 리소스
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TestResource»initialize
TestResource>>initialize
   self setUp
   self setUp
</syntaxhighlight>
</syntaxhighlight>

Revision as of 10:27, 22 March 2013

SUnit 의 내부구현

SUnit의 내부구현은 스몰토크 프레임웍에 대한 흥미로운 사례가 되기도 합니다. 이제부터 테스트의 실행과정을 통해서 SUnit 내부구현중 핵심이 되는 부분을 살펴보겠습니다.


테스트 실행하기

(aTestClass selector: aSymbol) run 을 진행해서 프로그램식에 대한 테스트를 해보도록 하겠습니다.

그림 7.3: 테스트 실행하기


TestCase>>run: 메서드의 구현을 보면, 테스트의 결과를 모으기 위해서 TestResult 의 인스턴스를 만들어서 자기 자신에게 run: 메세지를 전송합니다. (그림 7.3을 참고해주세요)


메서드 7.10: test case 실행

TestCase>>run
  | result |
  result := TestResult new.
  self run: result.
  result


TestCase>>run: 메서드는 runCase: 라는 메시지를 테스트 결과로 전송합니다.

메서드 7.11: test case 를 테스트 결과로 전달하기

TestCase>>run: aResult
  aResult runCase: self


TestResult>>runCase: 메서드는, 인수로서 건네받은 TestCase 에 runCase 메세지를 송신합니다. 그 다음 테스트로 에러가 발생한 회수와 실패한 회수, 성공한 회수를 카운트합니다. 예외 핸들러를 설정하고, 예외의 발생과 assertion의 실패에 대비합니다.


메서드 7.12: test case 오류와 실패를 잡아내기

TestResult»runCase: aTestCase
  | testCasePassed |
  testCasePassed := true.
  [[aTestCase runCase]
    on: self class failure
    do:
      [:signal |
      failures add: aTestCase.
      testCasePassed := false.
      signal return: false]]
          on: self class error
          do:
          [:signal |
          errors add: aTestCase.
          testCasePassed := false.
          signal return: false].
  testCasePassed ifTrue: [passed add: aTestCase]


TestCase>>runCase 메서드는 setUp 메시지와 tearDown 메시지를 아래의 예처럼 전송합니다.

메서드 7.13: Test case 템플릿 메서드 (Test Case Template Method)

TestCase»runCase
  self setUp.
  [self performTest] ensure: [self tearDown]


TestSuite 실행하기

여러개의 테스트를 한번에 실행하기 위해, 관련된 테스트들을 포함하고 있는 TestSuite 에 메시지를 발송합니다. TestCase 클래스는 그것의 메서드들로부터, 테스트 슈트(test suite)를 구축하기 위해 몇 가지 기능을 제공합니다. 프로그램식 MyTestCase buildSuiteFromSelectors 는 MyTestCase 클래스에서 정의된 모든 테스트들을 포함하고 있는 suite 를 리턴합니다. 이 진행과정에서 핵심은 아래부분입니다.


메서드 7.14: 자동 구축 테스트 슈트(Auto-building the test suite)

TestCase class»testSelectors
  self selectors asSortedCollection asOrderedCollection select: [:each |
    ('test*' match: each) and: [each numArgs isZero]]


TestSuite>>run 메서드는 TestResult 의 인스턴스를 만들고 모든 리소스가 사용가능한지를 확인한 뒤, 그 다음 자신에게 run: 메시지를 전송하게 되는데, 이 작업은 suite 에 속한 모든 테스트를 실행시키게 되빈다. 그 이후 모든 리소스가 해제됩니다.


메서드 7.15: 테스트 suite 실행하기

TestSuite>>run
  | result |
  result := TestResult new.
   self areAllResourcesAvailable
    ifFalse: [TestResult signalErrorWith:
      'Resource could not be initialized'].
  [self run: result] ensure: [self resources do:
                     [:each | each reset]].
  result


메서드 7.16: TestResult 를 TestSuite 로 전달하기

TestSuite>>run: aResult
  self tests do:
    [:each |
    self sunitChanged: each.
    each run: aResult]


TestResource 클래스와 그것의 하위클래스들은 current 클래스 메서드를 사용하여 접근할 수 있으며, 만들어진 하위 클래스들의 (클래스당 하나의)인스턴스들을 지속적으로 파악합니다. 이 인스턴스는 테스트가 실행을 마치거나 리소스가 리셋될 때 자동으로 정리됩니다.

리소스 유효성 점검은 클래스 메서드인 TestResource class>>isAvailable에서 확인할 수 있듯이 필요할 경우 다시 만들어지는 작업을 가능하게 합니다. TestResouce 의 인스턴스를 생성 할때, 인스턴스는 초기화되고 메서드 setUp 이 호출됩니다.


메서드 7.17: 테스트 리소스 유효성(Test resource availability)

TestResource class>>isAvailable
  self current notNil


메서드 7.18:테스트 리소스 생성

TestResource class>>current
  current isNil ifTrue: [current := self new].
  current


메서드 7.19: 테스트 리소스

TestResource>>initialize
  self setUp

Notes