Functions from grader.core, grader.wrappers and grader.decorators modules can be used by simply importing grader module.
Core functions for the module.
See grader.core.test_module() for how to
Returns a test setting. Used internally.
Returns the test name as it is used by the grader. Used internally.
Sets a test setting. Used internally.
Decorator for a test. The function should take a single argument which is the object containing stdin, stdout and module (the globals of users program).
The function name is used as the test name, which is a description for the test that is shown to the user. If the function has a docstring, that is used instead.
Raising an exception causes the test to fail, the resulting stack trace is passed to the user.
Tests code. See test_module() for argument and return value description.
Runs all tests for the solution given as argument.
Parameters: |
|
---|---|
Returns: | Dictionary of test results. |
Return value format:
{
"results": [
{
"description": str, # test description
"success": bool, # indicates whether the test case was successful
"time": "0.101", # float indicating how long test took
"error_message": str, # error message if test was not successful
"traceback": str, # full error traceback if test was not successful
},
...
],
"success": bool, # indicates whether tests were run or not
"reason": str, # short string describing why tester failed to run
"extra_info": dict, # extra information about why tester failed to run
}
Wrappers to make writing tests easier
Tests that calling function with the given name exists and calling it with args gives expected_result.
If description is given, it is used as test name, else the description will before similar to “Check add(1, 2, 3) == 6”
Tests whether after writing each element of writes_list to stdin we can find expected_read in the resulting output.
Note that this also tests whether the program is waiting for before each write and that it’s not after the last one.
Decorator for generating multiple tests with additional arguments.
Parameters: |
|
---|---|
Returns: | list of test functions |
Example usage:
@test_cases(
[[1, 2], [3, 4]],
expected=lambda x, y: x+y,
description="Adding {0} and {1} should yield {expected}"
)
def t(m, a, b, expected):
# a and b are either 1 and 2 or 3 and 4
assert a + b == expected
This is equivelent to:
@test
@set_description("Adding 1 and 2 should yield 3")
def t_1(m):
assert 1+2 == 3
@test
@set_description("Adding 3 and 4 should yield 7")
def t_2(m):
assert 3+4 == 7
Post-test hook which as the value or the result of evaluating function on result to the test result dict.
Example usage:
@test
@after_test(add_value("grade", 7))
def graded_testcase(m):
...
Decorator for a post-hook on a tested function. Makes the tester execute the function action after running the decorated test.
Decorator for a pre-hook on a tested function. Makes the tester execute the function action before running the decorated test.
Hook for creating files before a test.
Example usage:
@grader.test
@grader.before_test(create_file('hello.txt', 'Hello world!'))
@grader.after_test(delete_file('hello.txt'))
def hook_test(m):
with open('hello.txt') as file:
txt = file.read()
# ...
Decorator for constructing a file which is available during a single test and is deleted afterwards.
Example usage:
@grader.test
@create_temporary_file('hello.txt', 'Hello world!')
def hook_test(m):
with open('hello.txt') as file:
txt = file.read()
Hook for deleting files after a test.
Example usage:
@grader.test
@grader.before_test(create_file('hello.txt', 'Hello world!'))
@grader.after_test(delete_file('hello.txt'))
def hook_test(m):
with open('hello.txt') as file:
txt = file.read()
# ...
Pre-test hook for exposing the ast of the solution module as an argument to the tester.
Example usage:
@grader.test
@grader.expose_ast
def ast_test(m, AST):
...
Decorator for setting the description of a test.
Example usage:
@grader.test
@grader.set_description("New description")
def a_test_case(m):
...
Decorator for test decorators.
This makes the decorator work testcases decorated with grader.wrappers.test_cases().