Simplify Python grading with Simple Test Blocks or Pytest in CodeGrade
Guides
March 19, 2025

Simple Python Test vs. Pytest: Which One to Use?

In 30 seconds...

Looking to streamline grading in Python assignments? In this post, we break down when to use CodeGrade's Simple Python Test Block for beginner-friendly tasks and when to switch to Pytest for more advanced functions. Whether you're testing small snippets or full programs, we've got the guidance you need to make grading a breeze.

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.

Better Python learning, faster Python grading.

Pytest

Pytest is the go-to unit testing framework for Python. The Pytest block in CodeGrade lets you run Pytest tests on student submissions, making it perfect for grading assignments where students write their own functions.

In this example assignment, we ask students to write a function that takes a number and returns its factorial value.

You don’t need to install Pytest manually—it will be automatically set up if needed.

To use Pytest in your AutoTest, just drag the Pytest block into your setup and write or paste your Pytest cases as you would in a separate file (just like usual).

The import pytest line isn’t required, but it lets you use Pytest features like parametrize, raises, and more.

You’ll also need to import the functions you're testing using from filename import function.

import pytest
from answer import factorial
from cg_pytest_reporter import name


@name("Test - 0")
def test_factorial_zero():
# Testing factorial(0)
assert factorial(0) == 1


@name("Test - Positive")
def test_factorial_positive():
# Testing factorial of a positive integer
# 5! = 120
assert factorial(5) == 120


@name("Test - 1")
def test_factorial_one():
# Testing factorial(1)
assert factorial(1) == 1


@name("Test - ValueError")
def test_factorial_raises_value_error():
# Ensuring an exception is raised for negative values
with pytest.raises(ValueError):
factorial(-1)

It’s also a good idea to import cg_pytest_reporter, which includes decorators for name, weight, and description. You can place these right above your test cases to provide even clearer feedback to your students.

In a nutshell, choosing between the Simple Python Test Block and Pytest depends on what you're teaching. For beginner assignments with small code snippets, the Simple Python Test Block gives you more flexibility and simplicity. But when it comes to full functions and more complex projects, Pytest is your go-to for structured, in-depth testing. Both tools make grading easier and more effective—just pick the one that fits your assignment, and you're good to go! Happy coding and grading!

If you are a current CodeGrade customer, contact us at support@codegrade.com

Not yet a customer? Book a call today to see if we are a good fit.

Continue reading

Jupyter Notebooks Improvements + Release Schedule!

Learn how to automatically grade Jupyter Notebook with our new integration. Watch our latest webinar to see it in action!

Watch now! How to Teach Prompt Engineering

AI Engineering Lab Webinar on AI-Assisted Coding in Introductory Courses

How to set up a Data Structures course with Delaware State

Explore Delaware State University's innovative Java course, where freshmen master object-oriented programming, algorithm design, and clean coding practices to launch their tech careers.

How to use Semgrep for Automatic Grading

Boost your grading workflow with Semgrep in CodeGrade. Discover how to automate code structure checks, enforce coding best practices, and improve student assignments with lightweight static analysis.

Sign up to our newsletter

Transform your coding course today!