Here I will go through a fast project setup for writing tests for Haxe
using tink_testrunner
and tink_unittest
library.
There is no need to use any special editor but since most Haxe developers use VSCode it would be better to install Haxe Test Explorer extension.
Also install:
haxelib install tink_testrunner
and haxelib install tink_unittest
and haxelib install hxnodejs
libraries.
Create a directory called tests
and test.hxml
.
Inside test.hxml
:
--class-path tests
--library hxnodejs
--library tink_unittest
--library tink_testrunner
--main RunTests
--js tests.js
--dce full
--cmd node ./tests.js
So you can write tests inside tests directory and as our main class is RunTests RunTests.hx
will be out entry point.
RunTests
class may always look like this as we write batch tests:
class RunTests {
static function main() {
Runner.run(TestBatch.make([
**ADD_TEST_CLASSES_HERE**
])).handle(Runner.exit);
}
}
Writing Test Classes
So here we imagine we have a class called Calculator and we want to test it's add function.
class Calculator {
public static function add(a:Int, b:Int):Int {
return a + b;
}
}
We then write our test class:
class CalculatorTests {
public function new() {}
public function addTest() {
return Assert.assert(Calculator.add(100, 20) == 120);
}
}
And then add CalculatorTests
to RunTests
class part where we said ــADD_TEST_CLASSES_HEREــ.
As we compile test.hxml
it should show us an error but don't worry if you are not gonna use Haxe Test Explorer to run tests remove --cmd node ./tests.js
in test.hxml
to suppress the error.
If you run the code with node ./tests.js
you can see the report of the test being 1 Successful test.
Discussion (0)