Skip to content

Working with test classes

misprintt edited this page Aug 7, 2012 · 13 revisions

Naming your test classes

Test classes should always end with 'Test.hx'

ExampleTest.hx

This ensures that the munit build tool only picks up test classes when generating the TestSuite.

Test

@Test
public function testExample():Void
{
	Assert.isTrue(true);
}

Async Test

import massive.munit.util.Timer;

...

@AsyncTest
public function testAsyncExample(asyncFactory:AsyncFactory):Void
{
	var handler:Dynamic = asyncFactory.createHandler(this, onTestAsyncExampleComplete, 300);
	timer = Timer.delay(handler, 200);
}

private function onTestAsyncExampleComplete():Void
{
	Assert.isFalse(false);
}

Setup and teardown

@BeforeClass
public function beforeClass():Void
{
	// This will run once per test class
}

@AfterClass
public function afterClass():Void
{
	// This will run once per test class
}

@Before
public function setup():Void
{
	// This will run before every test
}

@After
public function tearDown():Void
{
	// This will run after every test
}

Ignoring a Test

@Ignore("Runs too slow, needs optimised") @Test
public function testExample():Void
{
	Assert.isTrue(true);
}

Sometimes you want to temporarily disable a test. Methods annotated with @Test that are also annotated with @Ignore will not be executed as tests. @Ignore takes an optional explanation of why the test was disabled. This can be useful for reporting and when returning to these later.

The default PrintClient will log ignored tests with a comma (,) and include the number of ignored tests in the final statistics. If you wish to see a more complete list of tests that have been ignored, pass "true" to the PrintClient constructor (you'll find this in your TestMain.hx) and the results will then include a list of ignored tests along with any explanation provided in the @Ignore annotation.

Testing a single method

If you just want to target one or a couple of tests while debugging then mark them up with a @TestDebug tag

@Test
@TestDebug
public function testExample():Void

and then run your tests as before but this time add the -debug flag. For example:

	> haxelib run munit test -as3 -debug 

This will run only those tests marked with @TestDebug.

Under the hood this adds both testDebug and debug compiler flags to the build. The former is just for munit and should not be used in your own code. The latter is a common one used for enabling debugging logic so feel free to use it for this purpose.

Platform specific tests

If a platform specific test is wrapped in conditional compilation statements (#if .... #elseif ... #end) then it will only execute in that target platform:

#if js
@Test
public function testExample()
{
   Assert.isTrue(true);
}
#end

If the code is platform agnostic (I.e. Not using target specific APIs) you can also ignore that test for a single target and provide a description that will appear in the runner:

#if js @Ignore("Not supported in JS") #end
@Test
public function testExample():Void
{
	Assert.isTrue(true);
}

Finally, if an entire test class is wrapped in conditional statements then you will get compilation errors on other targets. To avoid this, just provide a stub class implementation for other platforms like the example below:

#if js
class SomeTest
{
	public function new(){}

	@Test
	public function shouldFail()
	{
	   Assert.isTrue(true);
	}
}
#else
class SomeTest
#end

Full Example

package;

import massive.munit.util.Timer;
import massive.munit.Assert;
import massive.munit.async.AsyncFactory;

/**
* Auto generated ExampleTest for MassiveUnit. 
* This is an example test class can be used as a template for writing normal and async tests 
* Refer to munit command line tool for more information (haxelib run munit)
*/
class ExampleTest 
{
	private var timer:Timer;

	public function new() 
	{
	
	}

	@BeforeClass
	public function beforeClass():Void
	{
	}

	@AfterClass
	public function afterClass():Void
	{
	}

	@Before
	public function setup():Void
	{
	}

	@After
	public function tearDown():Void
	{
	}


	@Test
	public function testExample():Void
	{
		Assert.isTrue(true);
	}

	@AsyncTest
	public function testAsyncExample(asyncFactory:AsyncFactory):Void
	{
		var handler:Dynamic = asyncFactory.createHandler(this, onTestAsyncExampleComplete, 300);
		timer = Timer.delay(handler, 200);
	}

	private function onTestAsyncExampleComplete():Void
	{
		Assert.isFalse(false);
	}

	#if js
	@Test
	public function testSinglePlatform()
	{
	   Assert.isTrue(true);
	}
	#end

}
You can’t perform that action at this time.