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

Commit 8f24084

Browse files
authored
Merge pull request #3 from code-dot-org/molly/unittest-output
Make unittest_runner package
2 parents 3f59b8b + 380970b commit 8f24084

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ The current patches are:
1010
- We patch `requests` in order to route requests through code.org's request proxy. This protects students
1111
by only allowing requests to an allow-list of urls.
1212

13-
All Python Lab programs are prefixed with `setup_pythonlab()`, the method this package exposes, which applies
14-
the above patches.
13+
All Python Lab programs are prefixed with `setup_pythonlab()`, the method this package exposes, which only applies
14+
the matplotlib patch for now.
15+
16+
## unittest_runner
17+
This tests adds some customization to the output of unit tests, and has a function to either run validation tests
18+
(more customized) or student tests (less customized).
1519

1620
## Setup
1721
- Install `pyenv`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "unittest_runner"
7+
version = "0.0.1"
8+
dependencies = ["unittest"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .run_tests import run_student_tests, run_validation_tests
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from .validation_runner import ValidationTestResult
3+
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.
6+
def run_validation_tests(file_pattern):
7+
run_tests(file_pattern, ValidationTestResult)
8+
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.
11+
def run_student_tests(file_pattern):
12+
run_tests(file_pattern, unittest.TextTestResult)
13+
14+
def run_tests(file_pattern, resultclass):
15+
loader = unittest.TestLoader()
16+
test_suite = loader.discover('.', file_pattern)
17+
runner = unittest.TextTestRunner(verbosity=2, resultclass=resultclass)
18+
result = runner.run(test_suite)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
from unittest import TextTestResult
3+
4+
class ValidationTestResult(TextTestResult):
5+
# The default behavior is to display the test name and short description.
6+
# We want to display only the short description if it exists, otherwise just the test name.
7+
# Students don't see the tests, so we don't need to display the test name.
8+
# See this PR for details: https://github.com/code-dot-org/pythonlab-packages/pull/3
9+
def getDescription(self, test):
10+
doc_first_line = test.shortDescription()
11+
if doc_first_line:
12+
return doc_first_line
13+
else:
14+
return str(test)

0 commit comments

Comments
 (0)