Skip to content

Commit a16bf4a

Browse files
committed
1025 TDD 18장
1 parent ac148f0 commit a16bf4a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

TDD/test.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 2부 - xunit 만들기
2+
3+
class TestCase:
4+
def __init__(self, name):
5+
self.name = name
6+
7+
def run(self):
8+
'''
9+
메서드 동적 호출
10+
getattr(object, name, default)는 object 내에서 주어진 string(name)과 동일한 method를 반환해준다.
11+
따라서 테스트케이스의 이름을 전달했을 때, 해당 테스트케이스가 호출되었는지를 기록할 수 있다.
12+
'''
13+
method = getattr(self, self.name) # 상수를 변수(method)로 변화시켜 일반화하는 리팩토링의 예
14+
method()
15+
16+
17+
class WasRun(TestCase):
18+
def __init__(self, name):
19+
self.wasRun = None # 테스트케이스가 호출되었는지를 알려주는 어트리뷰트
20+
TestCase.__init__(self, name)
21+
22+
def testMethod(self):
23+
'''
24+
메서드 호출여부 기록
25+
메서드가 호출되었는지를 기억(flag)하는 메서드
26+
'''
27+
self.wasRun = 1
28+
29+
30+
class TestCaseTest(TestCase):
31+
def testRunning(self):
32+
'''
33+
test 코드 실행 메서드
34+
실행 과정을 print문이 아니라 assertion 형태로 구현
35+
'''
36+
test = WasRun("testMethod")
37+
assert(not test.wasRun) # not None이므로, True 반환
38+
test.run() # testMethod()를 직접 호출하지 않고, run()이라는 함수를 두어 두 부분을 분리함
39+
assert(test.wasRun) # 1
40+
41+
# main
42+
43+
TestCaseTest("testRunning").run() # 성공!
44+

0 commit comments

Comments
 (0)