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.

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.

  1. Press the "Add Unit Test" button to add a new Unit Test test to your Test Category.

  2. Select your Unit Testing Framework from the dropdown.

  3. 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.

Compatibility scripts

We provide wrapper scripts for a growing list of frameworks that handle all of this for you. Contact us at support@codegrade.com 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.

It is recommended to compile your student code and your unit tests in a separate test step (see example below). If compilation fails, it is more clear to you and your students why this way.

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

Last updated