Initial unittest skeleton using python and TAP

This commit is contained in:
Renato Alves 2014-02-03 00:26:47 +00:00 committed by Paul Beckingham
parent 523dce8f32
commit 93f0e14073
2 changed files with 156 additions and 0 deletions

View file

@ -0,0 +1,98 @@
#
# Code from https://github.com/vit1251/unittest-tap-reporting
# No explicit license
#
# With modifications by Renato Alves
#
import sys
import time
import unittest
class TAPTestResult(unittest.result.TestResult):
version = 13
def __init__(self, stream=None, descriptions=None, plan=None):
super(TAPTestResult, self).__init__()
#self.stream = sys.stdout
self.stream = stream
self._current = 0
self.descriptions = descriptions
#self.stream.write("TAP version %d\n" % (self.version, ))
self.stream.write("%d..%d\n" % (1, plan, ))
def getDescription(self, test):
doc_first_line = test.shortDescription()
if self.descriptions and doc_first_line:
return doc_first_line
#return ' - '.join((str(test), doc_first_line))
else:
return str(test)
def addSuccess(self, test):
#super(TAPTestResult, self).addSuccess(test)
#print test
self.stream.write("ok %d - %s\n" % (self._current+1, self.getDescription(test)))
self.stream.flush()
self._current += 1
def addError(self, test, err):
(exctype, value, tb) = err
self.stream.write("not ok %d - %s\n# ERROR: %s\n" % (self._current+1, self.getDescription(test), value))
self.stream.flush()
self._current += 1
def addFailure(self, test, err):
(exctype, value, tb) = err
self.stream.write("not ok %d - %s\n# FAIL: %s\n" % (self._current+1, self.getDescription(test), value))
self.stream.flush()
self._current += 1
def addSkip(self, test, reason):
self.stream.write("not ok %d - %s # SKIP: %s\n" % (self._current+1, self.getDescription(test), reason))
self.stream.flush()
self._current += 1
class TAPTestRunner(object):
"""A test runner ia abstract class that displays results in user defined form.
It prints out the names of tests as they are run, errors as they
occur, and a summary of the results at the end of the test run.
"""
resultclass = TAPTestResult
def __init__(self, stream=sys.stderr, descriptions=True, plan=None, failfast=False, buffer=False, resultclass=None):
self.stream = stream
self.descriptions = descriptions
self.plan = plan
self.failfast = failfast
self.buffer = buffer
if resultclass is not None:
self.resultclass = resultclass
def _makeResult(self, test):
if self.plan is None:
self.plan = test.countTestCases()
return self.resultclass(self.stream, self.descriptions, self.plan)
def run(self, test):
"Run the given test case or test suite."
result = self._makeResult(test=test)
#registerResult(result)
result.failfast = self.failfast
result.buffer = self.buffer
startTime = time.time()
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
try:
test(result)
finally:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun()
stopTime = time.time()
timeTaken = stopTime - startTime
return result

58
test/version.py Normal file
View file

@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
import sys
import os
# Ensure python finds the local taprunner module
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import unittest
from subprocess import Popen, PIPE, STDOUT
from datetime import datetime
class TestVersion(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Executed once before any test in the class"""
# Empty rc file
open("version.rc", 'w').close()
def setUp(self):
"""Executed before each test in the class"""
def testVersion(self):
"""Copyright is current"""
command = ["../src/task", "rc:version.rc", "version"]
# Merge STDOUT and STDERR
p = Popen(command, stdout=PIPE, stderr=STDOUT)
out, err = p.communicate()
expected = "Copyright \(C\) \d{4} - %d" % (datetime.now().year,)
self.assertRegexpMatches(out.decode("utf8"), expected)
def testFailVersion(self):
"""Copyright is one year old"""
command = ["../src/task", "rc:version.rc", "version"]
# Merge STDOUT and STDERR
p = Popen(command, stdout=PIPE, stderr=STDOUT)
out, _ = p.communicate()
expected = "Copyright \(C\) \d{4} - %d" % (datetime.now().year - 1,)
self.assertRegexpMatches(out.decode("utf8"), expected)
def tearDown(self):
"""Executed after each test in the class"""
@classmethod
def tearDownClass(cls):
"""Executed once after all tests in the class"""
os.remove("version.rc")
if __name__ == "__main__":
from taprunner import TAPTestRunner
unittest.main(testRunner=TAPTestRunner())
# vim: ai sts=4 et sw=4