User Tools

Writing tests


Writing tests during model development is a good practice:

  1. It ensures that code modification do not modify the coherence of the expected results.
  2. It validates the model on particular cases.

Writing tests

Tests are written with JUnit4 :

All the tests are stored in a particular package hierarchy in the directory:


Tests can be written in Java or Groovy.

Java example


package maddmodule.model;
import static org.junit.Assert.assertTrue;
import maddmodule.script.MaddEvolutionParameters;
import maddmodule.script.MaddInitialParameters;
import org.junit.Test;
import capsis.kernel.Step;
import capsis.script.C4Script;
public class MaddModuleScriptTest {
    public void testC4Script() throws Throwable {
	// init
        C4Script s = new C4Script("maddmodule");
        MaddInitialParameters i = new MaddInitialParameters (s.getModel(), 
							     s.getDataDir() + "/maddmodel/A.inv");
        int nbTree = ((MaddStand)(i.getInitStand())).getTrees().size();
        // evolution
        Step result = s.evolve(new MaddEvolutionParameters (200));
        // nb of trees is growing
        assertTrue(result != null); 
        assertTrue( nbTree < ((MaddStand)(result.getStand())).getTrees().size());;

Groovy example


package test
import org.junit.Test
import static org.junit.Assert.assertEquals
class ArithmeticTest {
    void additionIsWorking() {
        assertEquals 4, 2+2
    void divideByZero() {
        println 1/0



You may use the assertion methods in the doc below in your test methods to check for expected values for some interesting variables.

Compiling the tests

To compile all the tests, type from the Capsis install directory:

ant compile-test

Running tests

To run all the tests, type from the Capsis install directory:

ant test

To run a particular test:

ant run-test -Dtest=Full/TestName

# e.g. for douglas.DouglasTest in test/src/
ant run-test -Dtest=douglas/DouglasTest

To view the results:

firefox test/report/index.html
documentation/writing_tests.txt ยท Last modified: 2017/02/24 14:33 by beudez