def test_command_block(self): with Execution(''' class Fruit: def __init__(self, name, weight=0): self.name = name self.weight = weight def do_math(a, b): return a + b - 5 def weigh_fruits(fruits): return sum(fruit.weight for fruit in fruits) ''', run_tifa=False) as e: with CommandBlock(): orange = call("Fruit", "Orange", 30, target="orange") self.assertIsInstance(orange, e.student.data['Fruit']) pineapple = call("Fruit", "Pineapple", 60, target="pineapple") run("fruits = [orange, pineapple]") total_weight = call('weigh_fruits', args_locals=["fruits"]) assert_equal(evaluate('pineapple.weight'), 61) self.assertFeedback( e, """Failed Instructor Test Student code failed instructor test. I ran the code: orange = Fruit('Orange', 30) pineapple = Fruit('Pineapple', 60) fruits = [orange, pineapple] weigh_fruits(fruits) I evaluated the expression: pineapple.weight The value of the result was: 60 But I expected the result to be equal to: 61""")
def test_bad_recursion(self): contextualize_report(""" def to_pig_latin(str): first = str[0] str = str[1:] str = str + first + "ay" to_pig_latin("hello")""") commands.run() commands.call('to_pig_latin', 'test', threaded=True) self.assertNotIsInstance(commands.get_exception(), RecursionError)
def unit_test(function, *tests, else_message=None, score=None, partial_credit=False, **kwargs): """ Helper function for quickly unit testing. * Specify exact score for each test case, individually | list[str] * Specify exact score for each test case, with single value | str * Specify total score for all test cases, no partial credit | False, using `score` * Specify total score for all test cases, divide by # of cases passed | True, using `score` Args: function (): *tests (): else_message (str): The message to present as a positive if the tests all pass. score (str): The score to give overall/per test case, depending on partial_credit. partial_credit (bool or str or list[str]): Whether or not to give credit for each unit test as a percentage of the whole (True), or to only give points for passing all the tests (False). Defaults to False. If a list is passed, those scores will be used per test case. **kwargs (): Returns: """ if isinstance(partial_credit, bool): if partial_credit: # Specify total score for all test cases, divide by # of cases passed | True, using `score` if score: each_score = [str(Score.parse(score) / len(tests)) for test in tests] else: each_score = [None for test in tests] else: # Specify total score for all test cases, no partial credit | False, using `score` each_score = [None for test in tests] elif isinstance(partial_credit, (int, str, float)): # Specify exact score for each test case, with single value | str each_score = [partial_credit for test in tests] else: # Specify exact score for each test case, individually | list[str] each_score = partial_credit with assert_group(function, else_message=else_message, **kwargs) as group_result: for test_index, test in enumerate(tests): args, expected = test assert_equal(call(function, *args), expected, score=each_score[test_index], **kwargs) if partial_credit is False: group_result.score = score else: group_result.valence = group_result.POSITIVE_VALENCE group_result.score = combine_scores([success.score for success in group_result.successes], [failure.score for failure in group_result.failures] + [error.score for error in group_result.errors]) return not group_result
with phase("records", score=1 / 10): assert_has_variable(student, "World") # I expected the variable "World" to be a dict assert_is_instance(student['World'], dict) with phase("introduction", score=1 / 10): assert_has_variable(student, "INTRODUCTION") assert_is_instance(student['INTRODUCTION'], str) assert_true(student['INTRODUCTION']) with phase("make_world", before="make_world_components"): ensure_signature('make_world', 0, returns='World') assert_has_function(student, 'make_world') call('make_world', target='initial_world') assert_is_instance(student["initial_world"], student["World"]) with phase("make_world_components", after="win_and_lose_paths"): student.start_grouping_context() call('make_world', target="initial_world") assert_in("status", student['initial_world']) assert_equal(evaluate("initial_world['status']", target='status'), 'playing') student.stop_grouping_context() with phase("make_world_components", after="win_and_lose_paths"): initial_world = student.call('make_world', target='world') @phase("make_world_components", after="win_and_lose_paths")