U E D R , A S I H C RSS

Test Driven Development By Example/x Unit Example

C++로 UnitTest프레임워크를 만들기 전에 책에 있는 파이선 예제를 따라하고 있습니다. 따라가는 과정은 책에 있습니다.

아직 다 안 해서 미완성..;;
~cpp 

class TestCase:
    def __init__(self, name):
        self.name = name
    def run(self):
        result = TestResult()
        result.testStarted()
        self.setUp()
        try:
            method = getattr( self, self.name )
            method()
        except:
            result.testFailed()
        self.tearDown()
        return result
    def setUp(self):
        pass
    def tearDown(self):
        pass
    
class WasRun(TestCase):
    def testMethod(self):
        self.wasRun = True
        self.log += "testMethod "
    def testBrokenMethod(self):
        raise Exception
    def setUp(self):
        self.wasRun = None
        self.log = "setUp "
    def tearDown(self):
        self.log += "tearDown "

class TestResult:
    def __init__(self):
        self.runCount = 0
        self.failCount = 0
    def testStarted(self):
        self.runCount += 1
    def testFailed(self):
        self.failCount += 1
    def summary(self):
        return "%d run, %d failed" %(self.runCount, self.failCount)
    
class TestCaseTest(TestCase):
    def setUp(self):
        self.test = WasRun("testMethod")
    def testTemplateMethod(self):
        self.test.run()
        assert "setUp testMethod tearDown " == self.test.log
    def testResult(self):
        result = self.test.run()
        assert "1 run, 0 failed" == result.summary()
    def testFailedResult(self):
        self.test = WasRun("testBrokenMethod")
        result = self.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()
        
TestCaseTest("testTemplateMethod").run()
TestCaseTest("testResult").run()
TestCaseTest("testFailedResult").run()
TestCaseTest("testFailedResultFormatting").run()


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:11
Processing time 0.0116 sec