Setting up Unit Tests
CodeGrade has a Help Center, with better guides, more videos and updated documentation. The documentation and guides on this website are deprecated and will not be updated in the future. Please click here to go to this page on the Help Center!
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.
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 “ Unit Test” button to add a new Unit Test test to your Test Category.
Enter the command that will run your test suite. The command should write a JUnit XML file to the location given in the
For most unit testing frameworks it is necessary to install additional software and to figure out how to make the framework output its results to the correct location. We provide wrapper scripts for a growing list of frameworks that handle all of this for you. Check out the Unit Test documentation for a list of frameworks that are already supported, or contact us at firstname.lastname@example.org if your preferred framework is not listed there so we can discuss the possibilities!
All wrapper scripts share a similar interface and procedure for running tests, which we describe below.
If extra software needs to be installed to run the testing framework the
wrapper script provides an
install command that will install all required
dependencies. This should be run in the “Global setup script” section of your AutoTest
For example, to install JUnit 4 and its dependencies you would run
Compiling code and tests
For compiled languages it can be tricky to get the compiler to find all
libraries and other dependencies needed to compile the students’ code and your
tests. For these languages the wrapper scripts provide a
that configures the compiler to be able to find the required libraries. The
compile command takes file names as arguments and compiles those files.
For example, to compile all
.java files in the current directory, including
a JUnit version 4 test class, you would run
cg-junit4 compile *.java
Running the tests can be similarly tricky when a language needs to know about
certain locations of libraries it depends on, but also because you need to
figure out how you can make the framework output its results in the correct
format, or in the correct location. The wrapper scripts handle this in their
The scripts extract the output location from
then unset it before running the tests. The testing framework is then
configured to output its results at that location and the tests are executed.
For example, to run a JUnit 4 test class named
MyTestClass you would run
cg-junit4 run MyTestClass
The exact arguments to the
run command of each script may differ