API reference

Functions from grader.core, grader.wrappers and grader.decorators modules can be used by simply importing grader module.

grader.core module

Core functions for the module.

See grader.core.test_module() for how to

grader.core.get_setting(test_function, setting_name)[source]

Returns a test setting. Used internally.

grader.core.get_test_name(function)[source]

Returns the test name as it is used by the grader. Used internally.

grader.core.reset()[source]

Removes all loaded tests from test case table.

grader.core.set_setting(test_function, setting_name, value)[source]

Sets a test setting. Used internally.

grader.core.test(test_function)[source]

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.

grader.core.test_code(tester_code, user_code, other_files=, []*args, **kwargs)[source]

Tests code. See test_module() for argument and return value description.

grader.core.test_module(tester_path, solution_path, other_files=, []sandbox_cmd=None)[source]

Runs all tests for the solution given as argument.

Parameters:
  • tester_path (str) – Path to the tester used.
  • solution_path (str) – Path to the solution being tested.
  • other_files (list) – Paths to other files to put into same directory while testing.
  • sandbox_cmd – Sandbox to use. Set this to ‘docker’ to use the built-in docker sandbox.
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
}

grader.wrappers module

Wrappers to make writing tests easier

grader.wrappers.check_function(function_name, args, expected_result, description=None)[source]

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”

grader.wrappers.io_test(description, writes_list, expected_read)[source]

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.

grader.wrappers.test_cases(test_args, description=None, **arg_functions)[source]

Decorator for generating multiple tests with additional arguments.

Parameters:
  • test_args ([args...]) – Each element of the list is a list of arguments to add to test function call.
  • description (str) – String or function, gets passed in keywords and arguments.
  • arg_functions (function) – Keyword arguments for the test function.
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

grader.decorators module

grader.decorators.add_value(value_name, value_or_fn)[source]

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):
    ...
grader.decorators.after_test(action)[source]

Decorator for a post-hook on a tested function. Makes the tester execute the function action after running the decorated test.

grader.decorators.before_test(action)[source]

Decorator for a pre-hook on a tested function. Makes the tester execute the function action before running the decorated test.

grader.decorators.create_file(filename, contents='')[source]

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()
        # ...
grader.decorators.create_temporary_file(filename, contents='')[source]

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()
grader.decorators.delete_file(filename)[source]

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()
        # ...
grader.decorators.expose_ast(test_function)[source]

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):
    ...
grader.decorators.set_description(d)[source]

Decorator for setting the description of a test.

Example usage:

@grader.test
@grader.set_description("New description")
def a_test_case(m):
    ...
grader.decorators.test_decorator(decorator)[source]

Decorator for test decorators.

This makes the decorator work testcases decorated with grader.wrappers.test_cases().

grader.decorators.timeout(seconds)[source]

Decorator for a test. Indicates how long the test can run.

grader.assertions module

Assertion methods that can be used within tests.

grader.assertions.assertEquals(got, expected, template='Expected {expected} but got {got}', **kw)[source]
grader.assertions.assertNContains(input, collection, N, template=None)[source]
grader.assertions.assertOneContains(input, collection, template=None)[source]
grader.assertions.indent(text, spaces)[source]
grader.assertions.require_contains(input, what, message=None, **extraparams)[source]
grader.assertions.require_each_contains(input_list, expected_list, message=None, **extraparams)[source]

Table Of Contents

Previous topic

Welcome to python-grader’s documentation!

Next topic

Extensions API

This Page