Skip to content

Commit 97d957d

Browse files
committed
Add quiet mode for some tests and option to read expected input and output from files.
1 parent 33992b2 commit 97d957d

1 file changed

Lines changed: 64 additions & 13 deletions

File tree

jmu_gradescope_utils/jmu_test_case.py

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class _JmuTestCase(unittest.TestCase):
102102
module_count = 0
103103

104104
def getScriptOutput(self, filename, string_in, variables=None, args="",
105-
msg=None, processor=None, only_output=False):
105+
msg=None, processor=None, only_output=False, from_file=False):
106106
"""Get output for the provided Python script.
107107
108108
Args:
@@ -116,6 +116,8 @@ def getScriptOutput(self, filename, string_in, variables=None, args="",
116116
processor (func): A function mapping from string to string that will
117117
process the script output before it is returned.
118118
only_output (bool): Return only the stdout (rather than also the stderr).
119+
from_file (bool): Interpret string_in as a file name rather than a string.
120+
The file should be stored in the scaffolding folder.
119121
120122
Returns:
121123
dict: keys include 'stdout', and 'msg' as well as 'stderr' if there
@@ -143,6 +145,11 @@ def getScriptOutput(self, filename, string_in, variables=None, args="",
143145
stdin=subprocess.PIPE,
144146
stdout=subprocess.PIPE,
145147
stderr=subprocess.PIPE)
148+
149+
if from_file:
150+
with open(utils.full_source_path(string_in), 'r') as f:
151+
string_in = f.read()
152+
146153
actual, stderr = proc.communicate(input=string_in.encode())
147154
actual_text = actual.decode()
148155

@@ -181,7 +188,7 @@ def assertScriptOutputEqual(self, filename, string_in, expected,
181188

182189
def assertOutputEqual(self, filename, string_in, expected,
183190
variables=None, args="", msg=None,
184-
processor=None):
191+
processor=None, from_files=False, quiet=False):
185192
"""Assert correct output for the provided Python script.
186193
187194
Args:
@@ -196,59 +203,103 @@ def assertOutputEqual(self, filename, string_in, expected,
196203
processor (func): A function mapping from string to string that will
197204
process the script output before it is compared
198205
to the expected output.
206+
from_files (bool): Interpret string_in and expected as a file names rather
207+
than strings. The files should be stored in the scaffolding folder.
208+
quiet (bool): If true, detailed comparison feedback will be hidden. Only
209+
only the string provided in msg will be displayed.
199210
200211
Raises:
201212
AssertionError: If the expected output doesn't match the actual
202213
output.
203214
204215
"""
205216
result = self.getScriptOutput(filename, string_in, variables=variables,
206-
args=args, msg=msg, processor=processor)
217+
args=args, msg=msg, processor=processor,
218+
from_file=from_files)
207219
if "stderr" in result:
208220
self.fail(result["msg"])
209-
self.assertEqual(result["stdout"], expected, result["msg"])
221+
222+
if from_files:
223+
with open(utils.full_source_path(expected), 'r') as f:
224+
expected = f.read()
225+
226+
if quiet:
227+
if result["stdout"] != expected:
228+
self.fail(msg)
229+
else:
230+
self.assertEqual(result["stdout"], expected, result["msg"])
210231

211232
def assertOutputNotEqual(self, filename, string_in, expected,
212233
variables=None, args="", msg=None,
213-
processor=None):
234+
processor=None, from_files=False, quiet=False):
214235
"""Assert script output is NOT equal to the indicated string.
215236
216237
See :meth:`~jmu_gradescope_utils.jmu_test_case._JmuTestCase.assertOutputEqual` for
217238
description of arguments.
218239
"""
219240
result = self.getScriptOutput(filename, string_in, variables=variables,
220-
args=args, msg=msg, processor=processor)
241+
args=args, msg=msg, processor=processor,
242+
from_file=from_files)
221243
if "stderr" in result:
222244
self.fail(result["msg"])
223-
self.assertNotEqual(result["stdout"], expected, result["msg"])
245+
246+
if from_files:
247+
with open(utils.full_source_path(expected), 'r') as f:
248+
expected = f.read()
249+
250+
if quiet:
251+
if result["stdout"] == expected:
252+
self.fail(msg)
253+
else:
254+
self.assertNotEqual(result["stdout"], expected, result["msg"])
224255

225256
def assertInOutput(self, filename, string_in, expected,
226257
variables=None, args="", msg=None,
227-
processor=None):
258+
processor=None, from_files=False, quiet=False):
228259
"""Assert script output contains the indicated string.
229260
230261
See :meth:`~jmu_gradescope_utils.jmu_test_case._JmuTestCase.assertOutputEqual` for
231262
description of arguments.
232263
"""
233264
result = self.getScriptOutput(filename, string_in, variables=variables,
234-
args=args, msg=msg, processor=processor)
265+
args=args, msg=msg, processor=processor,
266+
from_file=from_files)
235267
if "stderr" in result:
236268
self.fail(result["msg"])
237-
self.assertIn(expected, result["stdout"], result["msg"])
269+
270+
if from_files:
271+
with open(utils.full_source_path(expected), 'r') as f:
272+
expected = f.read()
273+
274+
if quiet:
275+
if result["stdout"] not in expected:
276+
self.fail(msg)
277+
else:
278+
self.assertIn(expected, result["stdout"], result["msg"])
238279

239280
def assertNotInOutput(self, filename, string_in, expected,
240281
variables=None, args="", msg=None,
241-
processor=None):
282+
processor=None, from_files=False, quiet=False):
242283
"""Assert script output does not contain the indicated string.
243284
244285
See :meth:`~jmu_gradescope_utils.jmu_test_case._JmuTestCase.assertOutputEqual` for
245286
description of arguments.
246287
"""
247288
result = self.getScriptOutput(filename, string_in, variables=variables,
248-
args=args, msg=msg, processor=processor)
289+
args=args, msg=msg, processor=processor,
290+
from_file=from_files)
249291
if "stderr" in result:
250292
self.fail(result["msg"])
251-
self.assertNotIn(expected, result["stdout"], result["msg"])
293+
294+
if from_files:
295+
with open(utils.full_source_path(expected), 'r') as f:
296+
expected = f.read()
297+
298+
if quiet:
299+
if result["stdout"] in expected:
300+
self.fail(msg)
301+
else:
302+
self.assertNotIn(expected, result["stdout"], result["msg"])
252303

253304
def assertNoLoops(self, filename, msg=None):
254305
""" Assert that the provided script has no for or while loops.

0 commit comments

Comments
 (0)