Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 9b494ce

Browse files
committed
Add aborted test status
1 parent cedaf27 commit 9b494ce

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

tests.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
RANDOM_TO_LENGTH = 5
2626
STATUS_PASSED = "PASS"
2727
STATUS_FAILED = "FAIL"
28+
STATUS_ABORTED = "ABORTED"
2829

2930
last_test_executed = None
3031
final_output = ""
@@ -59,12 +60,17 @@ def execute(self):
5960
command += f" {self.integer2}"
6061
command += f" > {TEST_FILE}"
6162

62-
start_time = perf_counter()
63-
system(command)
64-
self.execution_time = perf_counter() - start_time
6563
global last_test_executed
6664
last_test_executed = self
6765

66+
start_time = perf_counter()
67+
try:
68+
system(command)
69+
except KeyboardInterrupt:
70+
self.execution_time = perf_counter() - start_time
71+
raise KeyboardInterrupt
72+
self.execution_time = perf_counter() - start_time
73+
6874
def self_execute(self):
6975
"""
7076
Counts the right answer for the test
@@ -86,22 +92,28 @@ def check(self):
8692
if last_test_executed != self:
8793
raise Exception("This test's execution output has been expired")
8894

89-
with open(TEST_FILE, 'r') as fin:
90-
its_result = fin.read()
95+
if self.status != STATUS_ABORTED:
96+
with open(TEST_FILE, 'r') as fin:
97+
its_result = fin.read()
98+
else:
99+
its_result = 0
91100

92101
output = ""
93-
if self.success_criteria(its_result):
94-
self.status = STATUS_PASSED
95-
else:
96-
self.status = STATUS_FAILED
102+
if self.status != STATUS_ABORTED:
103+
if self.success_criteria(its_result):
104+
self.status = STATUS_PASSED
105+
else:
106+
self.status = STATUS_FAILED
97107
output += self.status
98108
if self.integer2 is not None:
99109
integer_length = max([len(str(self.integer1)), len(str(self.integer2)), len(str(self.my_result)), len(str(its_result))])
100-
output += f" {self.name} executed in {self.execution_time} s\n\tIntegers:\n\t\t{self.integer1:>{integer_length}}\n\t\t{self.integer2:>{integer_length}}\n\tExpected output:\n\t\t{self.my_result:>{integer_length}}\n\tResult:\n\t\t{its_result:>{integer_length}}"
110+
output += f" {self.name} executed in {self.execution_time} s\n\tIntegers:\n\t\t{self.integer1:>{integer_length}}\n\t\t{self.integer2:>{integer_length}}"
101111
else:
102112
integer_length = max([len(str(self.integer1)), len(str(self.my_result)), len(str(its_result))])
103-
output += f" {self.name} executed in {self.execution_time} s\n\tInteger:\n\t\t{self.integer1:>{integer_length}}\n\tExpected output:\n\t\t{self.my_result:>{integer_length}}\n\tResult:\n\t\t{its_result:>{integer_length}}"
104-
113+
output += f" {self.name} executed in {self.execution_time} s\n\tInteger:\n\t\t{self.integer1:>{integer_length}}"
114+
output += f"\n\tExpected output:\n\t\t{self.my_result:>{integer_length}}"
115+
if self.status != STATUS_ABORTED:
116+
output += f"\n\tResult:\n\t\t{its_result:>{integer_length}}"
105117
return output
106118

107119

@@ -331,12 +343,17 @@ def summarise(tests: list[Test]):
331343
global final_output
332344

333345
tests_time = sum([test.execution_time for test in tests])
346+
if tests[-1].status == STATUS_ABORTED:
347+
tests.pop(-1)
334348
successful_tests = sum([1 for test in tests if test.status == STATUS_PASSED])
335349
all_tests = len(tests)
336350
if successful_tests == all_tests:
337351
status = STATUS_PASSED
338352
else:
339353
status = STATUS_FAILED
354+
if last_test_executed.status == STATUS_ABORTED:
355+
status = f"{STATUS_ABORTED} {status}"
356+
340357
final_output = f"{status} {successful_tests}/{all_tests} in {perf_counter() - testing_start_time} s ({tests_time} s in the tested app)" + final_output
341358

342359
with open(TEST_FILE, 'w', encoding="UTF-8") as fout:
@@ -387,6 +404,8 @@ def main():
387404

388405
except KeyboardInterrupt:
389406
print("Testing was forcefully stopped")
407+
last_test_executed.status = STATUS_ABORTED
408+
update_output()
390409
for i in range(len(all_tests) - 1, -1, -1):
391410
if all_tests[i].execution_time is None:
392411
del all_tests[i]

0 commit comments

Comments
 (0)