Our main reason to have tests is to ensure we do not loose existing functionality when adding new functionality. Another reason, hopefully getting less important, is to figure out what influences bugs had, respectively what is influenced by a bugfix.
The following list is not complete and likely never will. It is more to give you an idea, what could be tested:
The tests are automatically run every night. When you log in on matsim.org, you'll find a link in the left navigation area ("developer info") where you can look at the nightly status of the tests. If errors or failures occur, an email is sent to Marcel Rieser who will then inform people as necessary.
To run all tests, create a new Run-Configuration. Select to run all Tests in src/test/java with a JUnit 4 runner.

In the Arguments tab, do not forget to increase the available memory for the JVM (-Xmx600m), then click "Run".
export MAVEN_OPTS=-Xmx600m mvn test
Maven should output a summary, listing the number of all tests as well as the number of failed tests (if any).
(The following list has been updated for JUnit 4.7. Many existing tests written for JUnit 3.8 still follow different guidelines.)
MyModuleTest.@Test and name your methods that contain your tests with a starting "test", e.g.: @Test public void testGetParameter();assert keyword from the Java language to check conditions, but use the methods provided by JUnit: Assert.assertEquals(x, y); Assert.assertTrue(x); Assert.assertNull(x); Assert.assertNotNull(y);Assert.assertEquals("different events files.", checksum1, checksum2);
Assert.assertNotNull("did not find person 1.", population.getPersons().get(1));double d1, d2;
Assert.assertEquals(d1, d2); // this matches to assertEquals(Object, Object); likely not what you wanted...
Assert.assertEquals(d1, d2, MatsimTestUtils.EPSILON); // this is the right way to test for double values@Rule MatsimTestRule, see Mini-Introduction to JUnit 4.7.Config loadConfig(String filename);getOutputDirectory(); getInputDirectory(); getClassInputDirectory(); getPackageInputDirectory();EPSILON for comparing double values (see above).MatsimTestUtils.loadConfig().MatsimTestUtils.getOutputDirectory().test/input/<package>/<class>/<test>/. Example:test/input/org/matsim/mymodule/MyModuleUtils/testUtilMethod/config.xml
is a configuration file for the test org.matsim.mymodule.MyModuleUtils.testUtilMethod().JUnit is a framework for writing tests in Java. We currently use JUnit 4.7 for our tests, but many older tests are still written as JUnit 3.8. Thus, the following information is retained only for better understanding of existing tests.
JUnit 3.8 uses three main concepts:
A small example of a test and testcase:
public class MyTests extends TestCase {
protected void setUp() throws Exception {
super.setUp();
// your code here...
}
protected void tearDown() throws Exception {
super.tearDown();
// your code here...
}
public final void testOne() {
// your code here...
assertEquals(expected, actual);
}
public final void testTwo() {
// your code here...
assertNotNull(someObject);
}
}This code defines two tests (testOne, testTwo). Additionally, there are two methods setUp() and tearDown(), which are automatically called by JUnit when executing the TestCase. Make sure to call Gbl.reset() in tearDown() when you use the class Gbl or Config within your tests, so that the following TestCases are not influenced by your TestCase.
JUnit offers many different assert-statements which should be used to verify the results of your tests. Do not use the standardassert() offered by Java, as this must especially be enabled to be executed!
When writing Tests for MATSim, do not extend the class TestCase, but extend MatsimTestCase, as that one offers some convenient methods related to MATSim (see Guidelines for more details).
JUnit is a framework for writing tests in Java. We currently use JUnit 4.7 for our tests, which still supports the older JUnit 3.8 syntax.
A simple example of a testcase:
import org.junit.Assert;
import org.junit.Test;
public class MyTests {
@Test
public final void testOne() {
// your code here...
Assert.assertEquals(expectedValue, actualValue);
}
@Test
public final void testTwo() {
// your code here...
Assert.assertNotNull(someObject);
}
@Test @Ignore("not yet fully implemented")
public final void testThree() {
// your code here...
// TODO complete test
}
} This code defines three tests (testOne, testTwo, testThree). JUnit offers many different assert-statements which should be used to verify the results of your tests. Do not use the standard assert() offered by Java, as this must especially be enabled to be evaluated! If a test is not yet fully implemented but you still want to commit the code, add the annotation @Ignore to the test. In that case, the test will not be executed. Note that your code must still compile when committing ignored tests.
If you need to read in or write out files to disk, use MatsimTestUtils as a rule:
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
public class MyTests {
@Rule public MatsimTestUtils utils = new MatsimTestUtils();
@Test
public final void testOne() {
String inputFile = utils.getInputDirectory() + "myFile.txt";
// your code here...
String outputFile = utils.getOutputDirectory() + "myFile.txt";
Assert.assertEquals(expectedValue, actualValue);
}
} Please note that many methods of MatsimTestUtils can only be used in the actual test (marked with @Test), but not in a constructor or other initialization methods (e.g. in @Before).