CodeGrade Feedback Helpers¶
This package provides support for CodeGrades Feedback messages in AutoTest
V2. It was developed mainly to be used with the Simple Python Test block
and provides an assertion-based API to provide feedback during the execution of
a piece of student code, as well as providing some useful helper functions to
more easily do input/output testing on said code.
To install locally, run python3 -mpip install cg-feedback-helpers. To start,
take a look at Asserter.
Example usage¶
The following example demonstrates how you can quickstart using the feedback helpers
pacakge. In this example, we are using the cg-atv2-python-inserter to insert code
from the answer file provided by the user.
"""answer.py
print(input())
"""
from cg_feedback_helpers import asserter, helpers, NO_FEEDBACK
with (
# Use `capture_output` to store what gets printed to `stdout`
helpers.capture_output() as buffer,
# Use `as_stdin` to provide a custom `stdin` buffer. Each string is
# a separate line of input.
helpers.as_stdin("Hello world"),
):
# CG_INSERT answer.py
pass
# Now we can retrieve the output lines produced.
output = helpers.get_lines_from_buffer(buffer)
# And we check if the output matches
asserter.equals(
output,
["Hello world"],
# We can provide custom positive feedback
positive_feedback="The output matched!",
# And custom negative feedback
negative_feedback="The output is wrong.",
)
# If the assertion passed, the test finished correctly.
# As we already provided positive feedback, we don't need more.
asserter.emit_success(feedback=NO_FEEDBACK)
Pytest usage¶
The asserter is written to also help in the writing of pytest test suites. The pacakge
can be used almost exactly the same, however you need to use
ExceptionsWriter, or use the default
pytest_asserter. The difference is that negative feedback will be reported through
FeedbackAssertionError, instead of using the
structured feedback messages. Check out the documentation of the aforementioned exception
class for more details.
# Either import the default pytest asserter...
from cg_feedback_helpers import pytest_asserter
# ... or create one with your configuration
from cg_feedback_helpers import Asserter, ExceptionsWriter, Config
config = Config(writer=ExceptionsWriter())
pytest_asserter = Asserter(config=config)
def sum(a, b):
return a + b
def test_sum_positive_with_positive():
pytest_asserter.equals(sum(1, 2), 3)
def test_sum_positive_with_negative():
pytest_asserter.equals(sum(1, -2), -1)
def test_will_fail():
# A `FeedbackAssertionError` will be raised.
pytest_asserter.equals(sum("1", "2"), "3")