AMAPstudio

User Tools


Writing tests

Introduction

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 : http://www.junit.org/

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

test/src

Tests can be written in Java or Groovy.

Java example

test/src/maddmodule/MaddModuleScriptTest.java

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 {
 
    @Test
    public void testC4Script() throws Throwable {
 
	// init
        C4Script s = new C4Script("maddmodule");
        MaddInitialParameters i = new MaddInitialParameters (s.getModel(), 
							     s.getDataDir() + "/maddmodel/A.inv");
        s.init(i);
        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

test/src/test/test.groovy

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

JUnit

Assertions

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

http://junit.sourceforge.net/javadoc/org/junit/Assert.html

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