Creating Unit Tests
Upload your Unit Tests to CodeGrade and show their output in a way that is clear for students.
The Unit Test runs a unit testing framework that produces a JUnit XML file with the results of the tests at a location stored in the $CG_JUNIT_XML_LOCATION environment variable. With our AutoTest UI, it is very easy to run unit tests with one of our many built in unit testing frameworks.
Want to use a unit test framework that is not (yet) built in? Follow this guide to create your own wrapper script to use it in our unit test wrapper script!​
The resulting score of this test is the fraction of successful tests divided by the total number of tests that were run, multiplied by the weight of the test.
    Press the "Add Unit Test" button to add a new Unit Test test to your Test Category.
    Select your Unit Testing Framework from the dropdown.
    Fill in the optional fields to customize the behaviour of the unit test.
Want to use a custom unit testing framework? Choose the "Custom" option and enter the command that will run your test suite. This command should write a JUnit XML file to the location given in the $CG_JUNIT_XML_LOCATION environment variable. Use our built-in scripts to do this for you automatically.
Unit Test results as seen by the student

Compatibility scripts

We provide wrapper scripts for a growing list of frameworks that handle all of this for you. All supported frameworks are listed in the dropdown menu and can be found on the Unit Test documentation page. Contact us at [email protected] if your preferred framework is not listed there so we can discuss the possibilities!

Compiling code and tests

Are you using the Unit Test function for a compiled language (e.g. JUnit5 for Java), then you will have to compile the students' code and your tests using the built in compile command. The compile command takes file names as arguments and compiles those files. Compiling can be done either in the "Per student setup script", so that compiled files are available in all categories, or in a "Run Program step" before your Unit Test step.
For example, to compile all .java files in the current directory, including a JUnit version 5 test class, you would run:
cg-junit5 compile *.java
Using a Run Program step with weight 0 to compile Java code for our JUnit5 Unit Test

Learn more about Unit Tests:

Last modified 1mo ago