User Tools

Site Tools


documentation:writing_tests

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 : https://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.

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

Compiling the tests

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

On Linux/macOS:

sh ant compile-test

On Windows:

ant compile-test

Running tests

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

On Linux/macOS:

sh ant test

On Windows:

ant test

To view the results:

firefox test/report/junit-noframes.html

To run a particular test, type from the Capsis install directory:

On Linux/macOS:

sh ant run-test -Dtest=Full/TestName

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

On Windows:

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: 2021/12/13 09:28 by 127.0.0.1