Skip to content

Instantly share code, notes, and snippets.

@got4416
Created February 8, 2018 17:35
Show Gist options
  • Select an option

  • Save got4416/ed44fa84542fe1961fc63ca1b9de5ece to your computer and use it in GitHub Desktop.

Select an option

Save got4416/ed44fa84542fe1961fc63ca1b9de5ece to your computer and use it in GitHub Desktop.
Test-Driven Development By Example - 第22章 失敗の扱い
class TestResult:
def __init__(self):
self.runCount = 0
self.errorCount = 0
def testStarted(self):
self.runCount = self.runCount + 1
def testFailed(self):
self.errorCount = self.errorCount + 1
def summary(self):
return "%d run, %d failed" % (self.runCount, self.errorCount)
class TestCase:
def __init__(self, name):
self.name = name
def setUp(self):
pass
def tearDown(self):
pass
def run(self):
result = TestResult()
result.testStarted()
self.setUp()
try:
method = getattr(self, self.name)
method()
except:
result.testFailed()
self.tearDown()
return result
class WasRun(TestCase):
def setUp(self):
self.log = "setUp "
def testMethod(self):
self.log = self.log + "testMethod "
def testBrokenMethod(self):
raise Exception
def tearDown(self):
self.log = self.log + "tearDown "
class TestCaseTest(TestCase):
def testTemplateMethod(self):
test = WasRun("testMethod")
test.run()
assert("setUp testMethod tearDown " == test.log)
def testResult(self):
test = WasRun("testMethod")
result = test.run()
assert("1 run, 0 failed" == result.summary())
def testFailResult(self):
test = WasRun("testBrokenMethod")
result = test.run()
assert("1 run, 1 failed" == result.summary())
def testFailedResultFormatting(self):
result = TestResult()
result.testStarted()
result.testFailed()
assert("1 run, 1 failed" == result.summary())
print(TestCaseTest("testTemplateMethod").run().summary())
print(TestCaseTest("testResult").run().summary())
print(TestCaseTest("testFailResult").run().summary())
print(TestCaseTest("testFailedResultFormatting").run().summary())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment