Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide stack backtrace for errors in student code #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion gradescope_utils/autograder_utils/json_test_runner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Running tests"""
from __future__ import print_function

import os
import sys
import time
import traceback
import json

from unittest import result
Expand Down Expand Up @@ -74,7 +76,31 @@ def buildResult(self, test, err=None):

output = self.getOutput()
if err:
output += "Test Failed: {0}\n".format(err[1])
# When the error is due to an exception in the user's code,
# we provide a stack backtrace so the user is not left wondering
# why their code failed.
if not isinstance(err[0], AssertionError):
# Get formatted traceback and create a string describing the
# problem in the user code
stackbt = traceback.extract_tb(err[2])
# Number of frames of backtrace, can be changed to provide
# more detail, but we don't want to go all the way to the
# test case.
Nframes = 1
# gather frame information and create details message
msgs = []
for frame in range(-Nframes, 0):
# format as: filename:function line N: program text
msgs.append("%s:%s line %d: %s"%(
os.path.basename(stackbt[frame].filename),
stackbt[frame].name, stackbt[frame].lineno,
stackbt[frame].line))
details = "\n"+"\n".join(msgs)
else:
details = ""
# Report error and method/line on which it occurred
output += "Test Failed: {}{}\n".format(err[1], details)

result = {
"name": self.getDescription(test),
"score": score,
Expand Down