When setting up Python assignments in CodeGrade, the right testing approach depends on your students' experience level and the type of code they're writing.
For beginner assignments that focus on short code snippets—like variable manipulations or list operations—the Simple Python Test Block is the best fit. It lets you insert student code into a controlled script for easy grading without requiring them to write full programs.
On the other hand, if students are writing full programs and functions, you'll want to use Pytest, the industry-standard framework for structured unit testing. Pytest is ideal for grading more advanced assignments where function correctness and edge cases matter.
What’s the Difference?
Simple Python Test Block: Best for beginner-friendly assignments involving code snippets rather than full programs. It offers more grading flexibility than basic input-output testing.
Pytest: A robust framework for unit and integration testing, perfect for grading assignments where students write their own functions and need structured test cases.
Not sure which one to use? Keep reading to see them in action!
Simple Python Test
This block is perfect for beginner assignments where students write small code snippets instead of full programs. Their code gets inserted into a larger Python script, giving you more grading options than simple input-output testing.
For example, in one assignment, students work with two pre-defined variables. The solution is: reversed_sentence = sentence[::-1].
Since this snippet doesn’t include the full context, it wouldn’t run on its own as a complete program.
No need to worry about installations—anything required is automatically set up for you.

To use this block, just drag it into the setup.
With the # CG_INSERT filename.py
directive, you can drop the student’s code into a larger Python script where you define the necessary variables and run tests on their snippet.

Here’s the test code:
sentence = "This is another sentence"
answer = sentence[::-1]
# CG_INSERT reversed_sentence.py
print("your answer was: ", reversed_sentence)
print("correct answer is: ", answer)
if reversed_sentence == answer:
"Your answer was correct!"
exit(0)
else:
"Your answer was incorrect!"
exit(1)
With this block, you can import the asserter from the cg_feedback_helpers
module to run more detailed tests on your students' code using assertions.
from cg_feedback_helpers import asserter, NO_FEEDBACK
numbers = [34, 12, 99, 54, 28]
sorted_numbers = [12, 28, 34, 54, 99]
# CG_INSERT bubble_sort.py
print("your answer was: ", numbers)
print("correct answer is: ", sorted_numbers)
# Assert the answer is correct
asserter.equals(
numbers,
sorted_numbers,
positive_feedback = "Your answer was correct!",
negative_feedback = "Your answer was incorrect!"
)
# Display the feedback
asserter.emit_success(feedback=NO_FEEDBACK)
This asserter lets you check whether variables and structures exist, as well as their type, value, and state.