DEV Community

loading...

Haxe Testing With Tink

nevergarden profile image Nevergarden ・2 min read

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
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

We then write our test class:

class CalculatorTests {
    public function new() {}

    public function addTest() {
        return Assert.assert(Calculator.add(100, 20) == 120);
    }
}
Enter fullscreen mode Exit fullscreen mode

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)

pic
Editor guide