➕Advanced C++ autograding
Discover the advanced autograding options available for C++ assignments
Last updated
Discover the advanced autograding options available for C++ assignments
Last updated
In this guide, we explore the advanced grading options available for C++ assignments. For more information about setting up a C++ assignment from scratch, see
Google Test is an industry-standard unit testing framework for C++. It is particularly useful for grading assignments that require students to write functions and classes. Using C++ unit tests offers several advantages over conventional IO tests, including the ability to use assertions, parametrize test cases, and provide better feedback for students. Consider the example submission below, which consists of a simple C++ class and its associated header file:
In the following test file, we can define and create robust tests using Google Test assertions:
As explained in our previous guide, we first need to install a C++ compiler. Our second step is to use a Script Block to install Google Test through the following Bash script:
In the Tests phase, we first need to use an Upload Files block to upload the following files:
the testing file TestCalculator.cpp
defined above;
the CMake file CMakeLists.txt
used to compile our tests:
In line 12, beside runTests
we list the cpp
files that need to be compiled together in order to run our tests, namely the testing file TestCalculator.cpp
and our submission file Calculator.cpp
.
We are now ready to compile our tests using a Script Block with the following script:
To finally run our tests we can use a Custom Test Block with the following script:
You can use a Connect Rubric Block to connect the tests to a rubric category.
You can hide the configuration of the blocks from the student.
To enhance the AutoTest clarity for students, it can be useful to run the Tests conditionally on the success of the compilation block.
Clang-Tidy is an industry-standard linter for C++ that allows you to either enforce rules from various standard coding styles or to create your own set of rules. It is a useful tool for enforcing code styling best practices for beginner programmers.
In the AutoTest Setup phase, in addition to typically installing a C++ compiler, we need to install Clang-Tidy with a simple Script Block that runs the following command:
Running Clang-Tidy is done through the use of a Custom Test Block that runs the following script:
This script first runs Clang-Tidy enforcing all the available checks through the option -checks=*
, and then processes its output through the cg comments command. This will make such that all the coding style violations will be reported inline within the student's files in the submission folder.
You can select which checks to include/exclude among all the available ones. For this, we refer to the Clang Tidy documentation.
You can use a Connect Rubric Block to connect the tests to a rubric category. For a graded test you can decide the penalties to apply for each kind of violation (error, warning, etc...); for more information read here about the cg comments
command.
You can hide the configuration of the blocks from the student.
These advanced C++ testing options open the door to more rigorous testing methods and better student feedback. However, these are just the most commonly used methods. There are many more testing tools available that you can use with some simple setup. For more information, contact us at support@codegrade.com.