🐍Advanced Python autograding
Discover the advanced autograding options available for Python assignments
In this guide, we explore the advanced grading options available for Python assignments. For more information about setting up a Python assignment from scratch, see:
🐍Create your first Python assignmentSimple Python Test
The simple Python test block is a great tool for building assignments geared towards beginner programmers. It allows you to insert your students' code into a Python script, enabling more grading and feedback options than just IO testing.
Example 1
One use of the Simple Python Test block is to ask students to write basic Python code without writing a complete program. Consider the example submission below:
As you can see, this program would not be able to run on its own since sentence
is not defined. Using the Simple Python Test, we can define the variable and insert the student's code to make a complete program. The student's code is inserted in the Simple Python Test block using the comment # CG_INSERT filename
.
Example 2
Another use case for the Simple Python Test block is to provide better feedback to students using the cg_feedback_helpers
Python package. Consider the example submission below:
Using the cg_feedback_helpers
module with the Simple Python Test block we can run our students' code in the context of a full program and use assertions to provide better feedback. The cg_feedback_helpers
package can be installed in the AutoTest setup using the command python -m pip install cg_feedback_helpers
.
Pytest
Pytest is the industry standard unit testing framework for Python. The Pytest block is a dedicated test block for running Pytest unit tests on your students' submissions. This block is great for grading assignments in which students create their own functions. Consider the example submission below:
Using the Pytest block, we can easily create multiple test cases to assess the functionality of the square()
function. Additionally, we can use the cg_pytest_reporter
PyPi module to enhance the feedback from our test cases. The @name
and @description
decorators allow us to make the test cases clearer and more readable for students. The @weight
decorator allows us to adjust the grading weight of separate test cases.
The feedback from each test case is also beautifully rendered, making it easy for students to read.
Flake8
Flake8 is an industry-standard linter for Python that enforces the PEP8 style guide. It is a useful tool for enforcing code styling best practices for beginner programmers. The Flake8 block is an off-the-shelf testing tool that doesn't require any additional setup or configuration other than providing the name of the file to be tested. The Flake8 block generates comments directly on students' code. Each target line is highlighted according to the severity of the comment and the comment can be read by hovering over the line number with the mouse cursor. The Flake8 block also works in the built-in editor making it a powerful combination for beginners.
You can also update the configuration of Flake8 to ignore or select certain code style rules. This can be done by editing the config field. For example, this configuration would ignore the E203 error (Whitespace before ','.) as defined by the pycodestyle
package:
Conclusion
These advanced Python testing options open the door to more rigorous testing methods and better student feedback. However, these are just the readily available code blocks. There are many more testing tools and packages available that you can use with some simple setup. For more information, contact us at support@codegrade.com.
Last updated