Unit Testing HaXe JavaScript Project


Today I continued to progress on my Samsung Smart TV project with unit test implementation. My aim is to prepare smooth process for automated builds. First, I tought I would struggle with it much more but at the end it appeared that necessary tools already exists.

My first sreps led me to “Writing Unit Tests” article about resources available directly with HaXe, however it seemed that JavaScript testing was not available. The article also referenced MUnit a cross-platform haxe unit testing framework, which looked to have more capabilities.

MassiveUnit is a metadata driven unit testing framework for cross-platform Haxe development and it pretty much covers all the functionality I need. Kudos to MassiveInteractive for contributing so much for HaXe community. I am not going to explain how to get started with MUnit or how to compile and run tests as it is well described in wiki and on some other webs. But I will rather share some of my findings, and best practices I decided to use for continous integration.

It seems that MUnit requires both test.hxml and .munit config files to exist prior running unit tests. But while I do not want to have configs in any other place than build.xml, I decided to generate these dynamicaly. MUnit can dynamically create config for you so lets do some Ant:

<exec executable="haxelib">
	<arg value="run"/>
	<arg value="munit"/>
	<arg value="config"/>
	<arg value="-src"/><arg value="src/test/haxe"/>
	<arg value="-bin"/><arg value="${TARGET_TEST}"/>
	<arg value="-report"/><arg value="${TARGET_TEST}/report"/>
	<arg value="-classPaths"/><arg value="src/main/haxe,../lib"/>
	<arg value="-hxml"/><arg value="test.hxml"/>

…but it generates some Example* files I am not interested in testing:

<delete file="src/test/haxe/ExampleTest.hx"/>

…so once I have only stuff I need in my classPath, lets test:

<exec executable="haxelib">
	<arg value="run"/>
	<arg value="munit"/>
	<arg value="test"/>
	<arg value="-js"/>
	<arg value="-kill-browser"/>

My project is JavaScript only so we can filter target by -js flag, and the automated build machine should close testing browser (tab) once done – resolved by -kill-browser. Ok, still some files left I am not interested in versioning:

<delete file="src/test/haxe/TestSuite.hx"/>
<delete file="test.hxml"/>
<delete file=".munit"/>

…and finally break Ant if tests failed:

<fail message="Tests failed">
		<resourcecontains resource="${TARGET_TEST}/report/test/results.txt" substring="FAILED"/>

As our CI management system plays nice with JUnit, lets update generated TestMain.hx file

public function new()
	var suites = new Array<Class<massive.munit.TestSuite>>();
	var runner:TestRunner = new TestRunner(new RichPrintClient());
	runner.addResultClient(new HTTPClient(new JUnitReportClient()));
	runner.completionHandler = completionHandler;

so it produces formatted results in:


All the sources are available on github tag tests.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.