Skip to content

Instantly share code, notes, and snippets.

@hanglearning
Last active July 28, 2020 11:13
Show Gist options
  • Save hanglearning/1c97aeb791301108cfec240c89aff02f to your computer and use it in GitHub Desktop.
Save hanglearning/1c97aeb791301108cfec240c89aff02f to your computer and use it in GitHub Desktop.
2020 Summer 106 TA Writing test cases on Edfinity to evaluate Python expression
# assume expression is one line of code
# must have "# write your expression below" as a starting code
# thanks to Prof. Wassil and Dr. Bart's suggestion https://gist.github.com/acbart/32ecc194870eb98a452448b35c11378e
# Write an expression which will be True if and only if a previously-defined variable result is not equal to 100. Please wrap your expression in the provided print() function.
from sources import get_student_source, set_student_source
student_code = get_student_source()
class TestCase(EdfinityTestCase):
def inject_student_expression_in_print(self):
# must get called first
student_lines_of_code = student_code.split("\n")
student_expression = student_lines_of_code[student_lines_of_code.index("# write your expression below")+1]
set_student_source(f"{student_code}\nprint({student_expression})")
def injectBefore(self, new_code):
# must get called second
final_code = new_code + "\n" + get_student_source()
set_student_source(final_code)
def test_1(self):
self.inject_student_expression_in_print()
self.injectBefore("result = 0")
self.assertIn('True', self.student().stdout.getvalue(), "0 -> True")
def test_2(self):
self.inject_student_expression_in_print()
self.injectBefore("result = 100")
self.assertIn('False', self.student().stdout.getvalue(), "100 -> False")
def test_3(self):
self.inject_student_expression_in_print()
self.injectBefore("result = -100")
self.assertIn('True', self.student().stdout.getvalue(), "-100 -> True")
# For some questions, it is necessary to test the output length
''' Write an expression that refers to the first three characters in the previously-assigned string-valued variable dow.
For example:
Test Result
dow="Monday". Mon
'''
from sources import get_student_source, set_student_source
student_code = get_student_source()
class TestCase(EdfinityTestCase):
def inject_student_expression_in_print(self):
# must get called first
student_lines_of_code = student_code.split("\n")
student_expression = student_lines_of_code[student_lines_of_code.index("# write your expression below")+1]
set_student_source(f"{student_code}\nprint({student_expression})")
def injectBefore(self, new_code):
# must get called second
final_code = new_code + "\n" + get_student_source()
set_student_source(final_code)
def test_1(self):
self.inject_student_expression_in_print()
self.injectBefore('dow="Monday"')
self.assertEqual(3, len(self.student().stdout.getvalue().strip()), "length 3")
self.assertIn('Mon', self.student().stdout.getvalue(), "Monday -> Mon")
def test_2(self):
self.inject_student_expression_in_print()
self.injectBefore('dow="Tuesday"')
self.assertEqual(3, len(self.student().stdout.getvalue().strip()), "length 3")
self.assertIn('Tue', self.student().stdout.getvalue(), "Tuesday -> Tue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment