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.
Enter the command that will run your test suite. The 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.
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 [email protected] 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.
First we have to install the dependencies of the unit 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 configuration.
For example, to install JUnit 4 and its dependencies you would run:
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
compile command that configures the compiler to be able to find the required libraries. 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" so that compilation is being graded too.
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
$CG_JUNIT_XML_LOCATION and 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 in a Unit Test step:
cg-junit4 run MyTestClass