Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 4740803

Browse files
authored
Merge pull request #5 from code-dot-org/molly/improve-validation-output
Return validation results in a standard format
2 parents f1ffd38 + c24b78b commit 4740803

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

packages/unittest_runner/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "unittest_runner"
7-
version = "0.0.1"
7+
version = "0.1.0"
88
dependencies = ["unittest"]
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
import unittest
22
from .validation_runner import ValidationTestResult
33

4-
# Run the tests in the given file pattern and display the results to the user.
5-
# Validation tests use the ValidationTestResult class to display only the short description of the test.
64
def run_validation_tests(file_pattern):
7-
run_tests(file_pattern, ValidationTestResult)
5+
"""
6+
Run the tests matching the given file pattern and display the results to the user.
7+
Validation tests use the ValidationTestResult class to display only the short description of the test.
8+
9+
Args:
10+
file_pattern (str): A glob pattern to match test files.
11+
12+
Returns:
13+
List[Dict[str, str]]: A simplified list of test results with each entry containing:
14+
- 'name': The name of the test.
15+
- 'result': The outcome, which is one of the following:
16+
'PASS', 'FAIL', 'ERROR', 'SKIP', 'EXPECTED_FAILURE', 'UNEXPECTED_SUCCESS'.
17+
"""
18+
result = run_tests(file_pattern, ValidationTestResult)
19+
return result.simplified_results
20+
821

9-
# Run the tests in the given file pattern and display the results to the user.
10-
# Student tests use the standard TextTestResult class to display the test name and short description.
1122
def run_student_tests(file_pattern):
23+
"""
24+
Run the tests in the given file pattern and display the results to the user.
25+
Student tests use the standard TextTestResult class to display the test name and short description.
26+
27+
Args:
28+
file_pattern (str): A glob pattern to match test files.
29+
"""
1230
run_tests(file_pattern, unittest.TextTestResult)
1331

1432
def run_tests(file_pattern, resultclass):
1533
loader = unittest.TestLoader()
1634
test_suite = loader.discover('.', file_pattern)
1735
runner = unittest.TextTestRunner(verbosity=2, resultclass=resultclass)
18-
result = runner.run(test_suite)
36+
return runner.run(test_suite)

packages/unittest_runner/unittest_runner/validation_runner.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from unittest import TextTestResult
33

44
class ValidationTestResult(TextTestResult):
5+
def __init__(self, stream, descriptions, verbosity):
6+
super(ValidationTestResult, self).__init__(stream, descriptions, verbosity)
7+
self.simplified_results = []
8+
59
# The default behavior is to display the test name and short description.
610
# We want to display only the short description if it exists, otherwise just the test name.
711
# Students don't see the tests, so we don't need to display the test name.
@@ -12,3 +16,27 @@ def getDescription(self, test):
1216
return doc_first_line
1317
else:
1418
return str(test)
19+
20+
def addSuccess(self, test):
21+
super(ValidationTestResult, self).addSuccess(test)
22+
self.simplified_results.append({'name': self.getDescription(test), 'result': "PASS"})
23+
24+
def addError(self, test, err):
25+
super(ValidationTestResult, self).addError(test, err)
26+
self.simplified_results.append({'name': self.getDescription(test), 'result': "ERROR"})
27+
28+
def addFailure(self, test, err):
29+
super(ValidationTestResult, self).addFailure(test, err)
30+
self.simplified_results.append({'name': self.getDescription(test), 'result': "FAIL"})
31+
32+
def addSkip(self, test, reason):
33+
super(ValidationTestResult, self).addSkip(test, reason)
34+
self.simplified_results.append({'name': self.getDescription(test), 'result': "SKIP"})
35+
36+
def addExpectedFailure(self, test, err):
37+
super(ValidationTestResult, self).addExpectedFailure(test, err)
38+
self.simplified_results.append({'name': self.getDescription(test), 'result': "EXPECTED_FAILURE"})
39+
40+
def addUnexpectedSuccess(self, test):
41+
super(ValidationTestResult, self).addUnexpectedSuccess(test)
42+
self.simplified_results.append({'name': self.getDescription(test), 'result': "UNEXPECTED_SUCCESS"})

0 commit comments

Comments
 (0)