Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Create.
Browse files Browse the repository at this point in the history
  • Loading branch information
takkii committed Oct 31, 2023
1 parent f371ad6 commit fc114dd
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions unit/xunit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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):
result.testStarted()
self.setUp()
try:
method = getattr(self, self.name)
method()
except Exception:
result.testFailed()
self.tearDown()


class TestSuite:
def __init__(self):
self.tests = []

def add(self, test):
self.tests.append(test)

def run(self, result):
for test in self.tests:
test.run(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 setUp(self):
self.result = TestResult()

def testTempleteMethod(self):
test = WasRun("testMethod")
test.run(self.result)
assert ("setUp testMethod tearDown " == test.log)

def testResult(self):
test = WasRun("testMethod")
test.run(self.result)
assert ("1 run, 0 failed" == self.result.summary())

def testFailedResult(self):
test = WasRun("testBrokenMethod")
test.run(self.result)
assert ("1 run, 1 failed" == self.result.summary())

def testFailedResultFormatting(self):
self.result.testStarted()
self.result.testFailed()
assert ("1 run, 1 failed" == self.result.summary())

def testSuite(self):
suite = TestSuite()
suite.add(WasRun("testMethod"))
suite.add(WasRun("testBrokenMethod"))
suite.run(self.result)
assert ("2 run, 1 failed" == self.result.summary())


suite = TestSuite()
suite.add(TestCaseTest("testTempleteMethod"))
suite.add(TestCaseTest("testResult"))
suite.add(TestCaseTest("testFailedResult"))
suite.add(TestCaseTest("testFailedResultFormatting"))
suite.add(TestCaseTest("testSuite"))
result = TestResult()
suite.run(result)
print(result.summary())

0 comments on commit fc114dd

Please sign in to comment.