10.14 Unit testing
The Haxe Standard Library provides basic unit testing classes from the haxe.unit package.
Creating new test cases
First, create a new class extending haxe.unit.TestCase and add own test methods. Every test method name must start with "test
".
class MyTestCase extends haxe.unit.TestCase {
public function testBasic() {
assertEquals("A", "A");
}
}
Running unit tests
To run the test, an instance of haxe.unit.TestRunner has to be created. Add the TestCase using the add
method and call run
to start the test.
class Main {
static function main() {
var r = new haxe.unit.TestRunner();
r.add(new MyTestCase());
// add other TestCases here
// finally, run the tests
r.run();
}
}
The result of the test looks like this:
Class: MyTestCase
.
OK 1 tests, 0 failed, 1 success
Test functions
The haxe.unit.TestCase
class comes with three test functions.
assertEquals(a, b)
Succeeds ifa
andb
are equal, wherea
is value tested andb
is the expected value.assertTrue(a)
Succeeds ifa
istrue
assertFalse(a)
Succeeds ifa
isfalse
Setup and tear down
To run code before or after the test, override the functions setup
and tearDown
in the TestCase
.
setup
is called before each test runs.tearDown
is called once after all tests are run.
class MyTestCase extends haxe.unit.TestCase {
var value:String;
override public function setup() {
value = "foo";
}
public function testSetup() {
assertEquals("foo", value);
}
}
Comparing Complex Objects
With complex objects it can be difficult to generate expected values to compare to the actual ones. It can also be a problem that assertEquals
doesn't do a deep comparison. One way around these issues is to use a string as the expected value and compare it to the actual value converted to a string using Std.string
. Below is a trivial example using an array.
public function testArray() {
var actual = [1,2,3];
assertEquals("[1, 2, 3]", Std.string(actual));
}
See the haxe.unit package on the API documentation for more details.