Skip to content
Snippets Groups Projects
Commit f20a5fc8 authored by tuhe's avatar tuhe
Browse files

Changes

parent 0eb4f293
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -414,6 +414,8 @@ class ActiveProgress(): ...@@ -414,6 +414,8 @@ class ActiveProgress():
self.time_started = time.time() self.time_started = time.time()
def terminate(self): def terminate(self):
if not self._running:
raise Exception("Stopping a stopped progress bar. ")
self._running = False self._running = False
self.thread.join() self.thread.join()
if hasattr(self, 'pbar') and self.pbar is not None: if hasattr(self, 'pbar') and self.pbar is not None:
...@@ -528,6 +530,8 @@ def giveupthefunc(): ...@@ -528,6 +530,8 @@ def giveupthefunc():
from collections import defaultdict from collections import defaultdict
class UTextResult(unittest.TextTestResult): class UTextResult(unittest.TextTestResult):
nL = 80
show_progress_bar = True
def __init__(self, stream, descriptions, verbosity): def __init__(self, stream, descriptions, verbosity):
super().__init__(stream, descriptions, verbosity) super().__init__(stream, descriptions, verbosity)
self.successes = [] self.successes = []
...@@ -535,9 +539,24 @@ class UTextResult(unittest.TextTestResult): ...@@ -535,9 +539,24 @@ class UTextResult(unittest.TextTestResult):
def printErrors(self) -> None: def printErrors(self) -> None:
# if self.dots or self.showAll: # if self.dots or self.showAll:
# self.stream.writeln() # self.stream.writeln()
# if hasattr(self, 'cc'):
# self.cc.terminate()
# self.cc_terminate(success=False)
self.printErrorList('ERROR', self.errors) self.printErrorList('ERROR', self.errors)
self.printErrorList('FAIL', self.failures) self.printErrorList('FAIL', self.failures)
def addError(self, test, err):
super(unittest.TextTestResult, self).addFailure(test, err)
self.cc_terminate(success=False)
def addFailure(self, test, err):
super(unittest.TextTestResult, self).addFailure(test, err)
self.cc_terminate(success=False)
# if self.showAll:
# self.stream.writeln("FAIL")
# elif self.dots:
# self.stream.write('F')
# self.stream.flush()
def addSuccess(self, test: unittest.case.TestCase) -> None: def addSuccess(self, test: unittest.case.TestCase) -> None:
# super().addSuccess(test) # super().addSuccess(test)
...@@ -552,19 +571,20 @@ class UTextResult(unittest.TextTestResult): ...@@ -552,19 +571,20 @@ class UTextResult(unittest.TextTestResult):
# (current, possible) = item.get_points(show_expected=show_expected, show_computed=show_computed,unmute=unmute, passall=passall, silent=silent) # (current, possible) = item.get_points(show_expected=show_expected, show_computed=show_computed,unmute=unmute, passall=passall, silent=silent)
# q_[j] = {'w': item.weight, 'possible': possible, 'obtained': current, 'hidden': hidden, 'computed': str(item._computed_answer), 'title': item.title} # q_[j] = {'w': item.weight, 'possible': possible, 'obtained': current, 'hidden': hidden, 'computed': str(item._computed_answer), 'title': item.title}
# tsecs = np.round(time.time()-start, 2) # tsecs = np.round(time.time()-start, 2)
show_progress_bar = True self.cc_terminate()
nL = 80
if show_progress_bar:
def cc_terminate(self, success=True):
if self.show_progress_bar:
tsecs = np.round(self.cc.terminate(), 2) tsecs = np.round(self.cc.terminate(), 2)
sys.stdout.flush() sys.stdout.flush()
ss = self.item_title_print ss = self.item_title_print
print(self.item_title_print + ('.' * max(0, nL - 4 - len(ss))), end="") print(self.item_title_print + ('.' * max(0, self.nL - 4 - len(ss))), end="")
#
# if not hidden:
current = 1 current = 1
possible = 1 possible = 1
# tsecs = 2 # current == possible
ss = "PASS" if current == possible else "*** FAILED" ss = "PASS" if success else "FAILED"
if tsecs >= 0.1: if tsecs >= 0.1:
ss += " (" + str(tsecs) + " seconds)" ss += " (" + str(tsecs) + " seconds)"
print(ss) print(ss)
...@@ -574,16 +594,19 @@ class UTextResult(unittest.TextTestResult): ...@@ -574,16 +594,19 @@ class UTextResult(unittest.TextTestResult):
# super().startTest(test) # super().startTest(test)
self.testsRun += 1 self.testsRun += 1
# print("Starting the test...") # print("Starting the test...")
show_progress_bar = True # show_progress_bar = True
n = 1 n = 1
j = 1 j = 1
item_title = self.getDescription(test) item_title = self.getDescription(test)
item_title = item_title.split("\n")[0] item_title = item_title.split("\n")[0]
# test.countTestCases()
self.item_title_print = "*** q%i.%i) %s" % (n + 1, j + 1, item_title) self.item_title_print = "*** q%i.%i) %s" % (n + 1, j + 1, item_title)
estimated_time = 10 estimated_time = 10
nL = 80 nL = 80
# #
if show_progress_bar: if self.show_progress_bar:
self.cc = ActiveProgress(t=estimated_time, title=self.item_title_print) self.cc = ActiveProgress(t=estimated_time, title=self.item_title_print)
else: else:
print(self.item_title_print + ('.' * max(0, nL - 4 - len(self.item_title_print))), end="") print(self.item_title_print + ('.' * max(0, nL - 4 - len(self.item_title_print))), end="")
...@@ -625,7 +648,7 @@ class UTextTestRunner(unittest.TextTestRunner): ...@@ -625,7 +648,7 @@ class UTextTestRunner(unittest.TextTestRunner):
super().__init__(*args, stream=stream, **kwargs) super().__init__(*args, stream=stream, **kwargs)
def _makeResult(self): def _makeResult(self):
stream = self.stream # not you! # stream = self.stream # not you!
stream = sys.stdout stream = sys.stdout
stream = _WritelnDecorator(stream) stream = _WritelnDecorator(stream)
return self.resultclass(stream, self.descriptions, self.verbosity) return self.resultclass(stream, self.descriptions, self.verbosity)
......
...@@ -44,7 +44,6 @@ parser.add_argument('--unmute', action="store_true", help='Show result of prin ...@@ -44,7 +44,6 @@ parser.add_argument('--unmute', action="store_true", help='Show result of prin
parser.add_argument('--passall', action="store_true", help='Automatically pass all tests. Useful when debugging.') parser.add_argument('--passall', action="store_true", help='Automatically pass all tests. Useful when debugging.')
def evaluate_report_student(report, question=None, qitem=None, unmute=None, passall=None, ignore_missing_file=False, show_tol_err=False): def evaluate_report_student(report, question=None, qitem=None, unmute=None, passall=None, ignore_missing_file=False, show_tol_err=False):
args = parser.parse_args() args = parser.parse_args()
if question is None and args.q is not None: if question is None and args.q is not None:
...@@ -141,6 +140,12 @@ class UnitgradeTextRunner(unittest.TextTestRunner): ...@@ -141,6 +140,12 @@ class UnitgradeTextRunner(unittest.TextTestRunner):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
class SequentialTestLoader(unittest.TestLoader):
def getTestCaseNames(self, testCaseClass):
test_names = super().getTestCaseNames(testCaseClass)
testcase_methods = list(testCaseClass.__dict__.keys())
test_names.sort(key=testcase_methods.index)
return test_names
def evaluate_report(report, question=None, qitem=None, passall=False, verbose=False, show_expected=False, show_computed=False,unmute=False, show_help_flag=True, silent=False, def evaluate_report(report, question=None, qitem=None, passall=False, verbose=False, show_expected=False, show_computed=False,unmute=False, show_help_flag=True, silent=False,
show_progress_bar=True, show_progress_bar=True,
...@@ -161,14 +166,7 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa ...@@ -161,14 +166,7 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa
nL = 80 nL = 80
t_start = time.time() t_start = time.time()
score = {} score = {}
# Use the sequential test loader instead. See here: # Use the sequential test loader instead. See here:
class SequentialTestLoader(unittest.TestLoader):
def getTestCaseNames(self, testCaseClass):
test_names = super().getTestCaseNames(testCaseClass)
testcase_methods = list(testCaseClass.__dict__.keys())
test_names.sort(key=testcase_methods.index)
return test_names
loader = SequentialTestLoader() loader = SequentialTestLoader()
# loader = unittest.TestLoader() # loader = unittest.TestLoader()
# loader.suiteClass = MySuite # loader.suiteClass = MySuite
...@@ -267,14 +265,15 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa ...@@ -267,14 +265,15 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa
# ws, possible, obtained = upack(q_) # ws, possible, obtained = upack(q_)
possible = res.testsRun possible = res.testsRun
obtained = possible - len(res.errors) obtained = len(res.successes)
assert len(res.successes) + len(res.errors) + len(res.failures) == res.testsRun
# possible = int(ws @ possible) # possible = int(ws @ possible)
# obtained = int(ws @ obtained) # obtained = int(ws @ obtained)
# obtained = int(myround(int((w * obtained) / possible ))) if possible > 0 else 0 # obtained = int(myround(int((w * obtained) / possible ))) if possible > 0 else 0
obtained = w * int(obtained * 1.0 / possible ) obtained = int(w * obtained * 1.0 / possible ) if possible > 0 else 0
score[n] = {'w': w, 'possible': w, 'obtained': obtained, 'items': q_, 'title': qtitle} score[n] = {'w': w, 'possible': w, 'obtained': obtained, 'items': q_, 'title': qtitle}
q.obtained = obtained q.obtained = obtained
q.possible = possible q.possible = possible
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment