Writing Unit Tests
Unit Testing is simple. You create a new TestClass and add it to the MainClass. Then you create testMethods in your TestClass. Thats it. Now, if you compile and run the MainClass, all your tests should be executed.
Create new TestCase
Create a new class extending haxe.unit.TestCase and create your own test methods. Every test method name must start with test.
class TestFoo extends haxe.unit.TestCase { public function testBasic(){ assertEquals( "A", "A" ); } }
Add new TestCase
Now you must have a main class in which you're adding your TestCase to a TestRunner.
class MyTest { static function main(){ var r = new haxe.unit.TestRunner(); r.add(new TestFoo()); // your can add others TestCase here // finally, run the tests r.run(); } }
Compile & Execute
Save the classes in MyTest.hx. To compile the test create compile.hxml with the content:
-neko mytest.n -main MyTest
Now compile it on the commandline:
haxe compile.hxml
The new compiled file mytest.n can now be invoked on the commandline:
neko test.n
The result of the test:
Class: TestFoo . OK 1 tests, 0 failed, 1 success
Test Functions
There are three functions you can use
Succeeds if a and b are equal, Where a is value tested and b is the expected value.
assertEquals(a, b)
Succeeds if a is true
assertTrue(a)
Succeeds if a is false
assertFalse(a)
Setup and Tear down
Some times you have the same code in multiple tests that setup the test or clean up after. You can overload the functions setUp() and tearDown(). setUp() is called before any test runs and tearDown is called after all tests are run.
Ready
From here on you are able to write your own unit tests. Some test are available on subversion : https://svn.motion-twin.com/haxeUnit