Skip to content

Commit c4f7442

Browse files
committed
feat: add import_as_non_testcase and SetupThenTearDown
1 parent ae55bed commit c4f7442

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

grading_lib/qa.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Set of utilities for quality assurance of the grading script."""
2+
3+
from importlib import import_module
4+
5+
6+
def import_as_non_testcase(module_name, name, package=None):
7+
"""Import the name from a module and prevent pytest from running it as testcase."""
8+
mod = import_module(module_name, package=package)
9+
cls = mod.__dict__[name]
10+
cls.__test__ = False
11+
return cls
12+
13+
14+
class SetupThenTearDown:
15+
"""Automatically call setUp and tearDown.
16+
17+
Intended to be used in the QA script.
18+
"""
19+
20+
def __init__(self, obj):
21+
self.obj = obj
22+
23+
def __enter__(self):
24+
if hasattr(self.obj, "setUp"):
25+
self.obj.setUp()
26+
27+
def __exit__(self, exc_type, exc_val, exc_tb):
28+
if hasattr(self.obj, "tearDown"):
29+
self.obj.tearDown()

0 commit comments

Comments
 (0)