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:
Using the .NET framework, we compile the students' files using the
command, which produces an output that potentially contains compilation errors or warnings. We can report and highlight these inline within the student submission using the custom CodeGrade command cg comments
.
For example, to build a dotnet console
project, we can use a Script Block that runs the following commands:
This will result in compilation errors or warnings being visible to the students within the student submission folder, as shown below:
Consider the following Calculator.cs
submission that implements a simple arithmetic calculator:
Our tests are defined in the following CalculatorTests.cs
file:
As displayed above, we can use the xUnit's Fact
attribute to set a custom name for each test. Moreover, by using naming the test as "[n] My Custom Test Name"
, we can also set a custom weight equal to n
, with the default weight being 1.
In order to run unit tests with xUnit, in the Setup Phase we first have to install dotnet using the corresponding block and create an xUnit project.
After installing dotnet, use a Script Block and type the following bash script:
If the class you want to test contains a main
method you should add the following line at the bottom of the script:
Once we are in the Tests phase, before running the actual tests, we have to:
Upload the fixture file that defines the xUnit tests;
Move both the xUnit testing file and the student submission files to the xUnit project folder;
Compile the student's code by building the xUnit Project.
After using an Upload File block to upload the xUnit testing file, use a Script Block to run the following script:
Notice that:
you can hide the script's content from the student using a Hide block as shown in the image above.
We are finally ready to run our tests. For this, we can use a Custom Test Block.
The Custom Test Block runs the following commands:
As shown in the image above, you can optionally:
hide the configuration of the Custom Test Block from the student;
run the tests only if the Compilation step succeeded using a Run-If Block.
The results of the tests will be shown to the student as below:
xUnit is the standard unit testing tool within the .NET Framework and can be used to test .NET languages such as C#. xUnit is especially appropriate for grading assignments that require students to write functions and classes. xUnit unit tests offer several advantages over conventional IO tests including the ability to use assertions, parametrize test cases, and provide custom feedback for students. For an overview of the testing attributes available with xUnit, we recommend .
we can report potential compilation errors inline into the student's submission, as explained at the .