Want to give your students instant feedback and use CodeGrade's latest features as they come out? Navigate to the AutoTest V2 guide to learn how to set up your Python assignment with our new and improved (beta) AutoTest system!
Learn how to set up a basic Python assignment in CodeGrade.

Step 1: Setup

CodeGrade AutoTest runs on Ubuntu (18.04.2 LTS) machines which you can configure in any way that you want and Python comes preinstalled with packages like Numpy.
In the setup section of your AutoTest, you can upload any files you might need for testing, if you are intending on using Python Unit Testing using PyTest for instance, this is where you will upload your unit test file. These files are called Fixtures and will be placed in the $FIXTURES directory on the Virtual Server.
The file structure of the server is like this:
All of your uploaded fixtures will be here.
This is where the submission of student is placed.
Tests are executed from this directory.
After uploading any files you might need, you can run some setup to install any packages you might need.
  • Global setup script: this is where you install your additional pip packages you want to be available on the server, for instance a package like pandas using python3 -m pip install pandas. The global setup only runs once and is then cached, so the student submission won't be available here yet.
  • Per-student setup script: this will run once when running the autograding for a student and will run before all tests. This is a good place to move $FIXTURES to the $STUDENT directory in case the student solution needs some input files to run correctly.
Use any command in the Global Setup Script field to install software or run your setup script

Step 2: Create your tests

Now that you have created the setup, it's time to create the actual tests. Do this by pressing the "Add Level" button and then the "Add Category" button.
All tests in AutoTest fill in a specific rubric category that you select. After selecting the category, you can start creating tests that will fill it in. Multiple test types are available in CodeGrade, which can be used together depending on your needs and wishes.

Set up an IO Test

IO Tests are great for console based programs and allow you to give an input to it and specify an expected output.
  1. 1.
    In Program to Test, write python3 followed by the filename of the student's program.
  2. 2.
    Give a name, so students know what the test is about.
  3. 3.
    Specify input and expected output. Several options are available to match a wider range of outputs.

Using IO Tests to run Python code

In Python, you can also execute the student's program interactively and specify Python code as input.
  1. 1.
    In the Program to test write python3 -ic "import calculator" where you replace calculator with the name of the student's program (without the .py extension).
  2. 2.
    This will import everything in their program and allow you to access variables and call any functions they create.
  3. 3.
    Then under Input, you can write any Python code to test the student's program.

Set up a PyTest unit test

Upload a PyTest unit test file as a $FIXTURE and automatically run unit tests on the code.
Imagine students have to code a very simple calculator in a file called
def add(x, y):
ans = x + y
return ans
def subtract(x, y):
ans = x - y
return ans
def multiply(x, y):
ans = x * y
return ans
def divide(x, y):
if y == 0:
raise ValueError("Can not divide by zero!")
ans = x / y
return ans
A powerful way to test the individual methods, is to write unit tests. In this example, we can test the student's code with the following unit tests in a file called
import pytest
import calculator
def test_add():
assert calculator.add(3, 2) == 5
assert calculator.add(1, -1) == 0
assert calculator.add(-1, -1) == -2
def test_subtract():
assert calculator.subtract(5, 2) == 3
assert calculator.subtract(1, -1) == 2
assert calculator.subtract(-1, -1) == 0
def test_multiply():
assert calculator.multiply(3, 2) == 6
assert calculator.multiply(1, -1) == -1
assert calculator.multiply(-1, -1) == 1
def test_divide():
assert calculator.divide(10, 2) == 5
assert calculator.divide(1, -1) == -1
assert calculator.divide(-1, -1) == 1
assert calculator.divide(5, 2) == 2.5
with pytest.raises(ValueError):
calculator.divide(10, 0)

Creating the unit test in CodeGrade

  1. 1.
    Upload the unit test file as a $FIXTURE in the CodeGrade setup.
  2. 2.
    In the category where you want the test to live create a Unit Test.
  3. 3.
    Select Pytest.
  4. 4.
    As the File to test give the following file: $FIXTURES/
Unit Testing with pytest

Set up a Pylint or Flake8 Code Quality test

Automatic run static code analysis on the student's code. For Python you can simply create the Code Quality test and select Flake8 or Pylint. Optionally you can upload a config file as a $FIXTURE and specify it in the code quality test.
This will then run automated quality checking on the student's code and assign a score. Code Quality comments will be placed in the code viewer for students.

Set up a Code Structure test

CodeGrade integrates a tool called Semgrep to make it easy to perform more complex code analysis by allowing you to write rules in a human readable format. You can provide generic or language specific patterns, which are then found in the code. With its pattern syntax, you can find:
  • Equivalences: Matching code that means the same thing even though it looks different.
  • Wildcards / ellipsis (...): Matching any statement, expression or variable.
  • Metavariables ($X): Matching unknown expressions that you do not yet know what they will exactly look like, but want to be the same variable in multiple parts of your pattern.

Writing the tests

Semgrep is pre-installed in the Unit Test. You will write your Semgrep Code Structure tests in a YAML file and upload this as a fixture in the setup section of AutoTest. Semgrep has an online editor that can be used to check and create your patterns:
For instance, this is a ruleset to detect a for-loop in the student code, and make sure they do not use a while-loop.
- id: for-loop
match-expected: true
pattern: |
for $EL in $LST:
message: A for-loop was used
severity: INFO
- python
- id: no-while-loop
match-expected: false
pattern: |
while $COND:
message: No while-loop was used
severity: INFO
- python
In this file, rules.yml, we define two rules: for-loop and no-while-loop. within these rules we define a few things:
  • Patterns
    • The ellipsis (...) is used to capture anything
    • metavariables $EL (element) and $LST (list) capture the two parts of the for-loop declaration (the naming of these metavariables is irrelevant and could have been anything else).
    • The same is done for the while loop condition $COND
  • Messages We are able to provide understandable messages that are parsed by the wrapper script making our tests understandable for our students.
  • Match-expected Importantly, we have added the match-expected field (this is added in the CodeGrade wrapper script and cannot be tested in regular semgrep), with putting this field to True for the for-loop rule, we specify that we are expecting a match in order to pass that test.
  • Severity This dictates the level of severity of failing the rule. The penalty for each severity level can be set in the Unit Test step.
  • languages Here we specify the language of the scripts which semgrep will be checking.

Creating the test in CodeGrade

  1. 1.
    Upload the YAML (.yml) with your Code Structure tests in it as a $FIXTURE in the CodeGrade setup.
  2. 2.
    In the category where you want the test to live create a Unit Test.
  3. 3.
    Select semgrep.
  4. 4.
    Give the follow extra argument: $FIXTURES/your-test-file.yml $STUDENT.
If you run this on your student's code, it will then show up like this:

Other test types

  • Run Program Tests: Run a command and if the command runs successfully this test will pass. Great for custom tests or just checking if the Python file runs.
  • Capture Points Tests: Use your own custom grading script, upload it as a $FIXTURE, execute it in this test and make sure it outputs a float between 0.0 and 1.0, this score will then be the number of points a student receives.
  • Checkpoints: Only execute tests below the checkpoint if the tests above the checkpoint reach a certain score. Great for separating simple and advanced tests.

Step 3: Start the AutoTest

Once you have created a configuration, press start and your AutoTest will start running. In the General Settings, you should have already uploaded a Test Submission, so you will see the results of this straight away.
Once it's started and your assignment is set to Open or Done, students can hand in and get immediate feedback!

More Python autograding