Test
Testcase
is the basic class to extend for unit-tests.
A TestCase
contains the API to write unit-tests.
It is handled as an abstract class this means you have to extend it if you want to work with the system (similar to the most other unit testing systems).
Example for a simple test:
import org.as2lib.test.unit.TestCase;
class MyTestCase extends TestCase {}
To execute a TestCase
you just have to call run()
. It logs
automatically with LoggerTestListener.
The test will return detailed informations about the executed method. The
method fail
will simply fail the test.
Example for a test that fails:
import org.as2lib.test.unit.TestCase;
class MyTestCase extends TestCase {
public function testOne(Void):Void {
if (1 == 0) { // example to show that fail can be used everywhere.
fail ("Error occured");
}
}
}
Within the "test"-methods you have access to different assert methods. Those methods fail if a certain condition is given or not and add a information for the failure to the informations about the execution.
Example for a assertion:
import org.as2lib.test.unit.TestCase;
class MyTestCase extends TestCase {
public function testOne(Void):Void {
assertEquals("1 is not 0", 1, 0);
}
}
Supported assert methods are:
method name | condition |
---|---|
assertTrue | value === true |
assertFalse | value === false |
assertEquals | ObjectUtil.compare(a,b); |
assertAlmostEquals | (a < b && a+x > b) || (a > b && a-x < b) |
assertNotEquals | !ObjectUtil.compare(a,b); |
assertSame | a === b |
assertNotSame | a !== b |
assertNull | value === null |
assertNotNull | value !== null |
assertUndefined | value === undefined |
assertNotUndefined | value !== undefined |
assertEmpty | value == null (equals == undefined) |
assertNotEmpty | value != null (equals != undefined) |
assertInfinity | value === Infinity |
assertNotInfinity | value !== Infinity |
assertThrows | call(arguments) throws exception |
assertNotThrows | call(arguments) doesnt throw exception |
assertTypeOf | value typeof type |
assertInstanceOf | value instanceof type |
The system will fetch all methods with a name that starts with "test" and
execute them in a new, isolated instance of the TestCase
to prevent
tests inteferences.
Example for a test with instance properties:
import org.as2lib.test.unit.TestCase;
class MyTestCase extends TestCase {
private var a:String;
private function testOne(Void):Void {
trace(a); // undefined
a = "1";
trace(a); // 1
}
private function testTwo(Void):Void {
trace(a); // undefined
a = "1";
trace(a); // 1
}
}
The method setUp allows preparing the new instance. The method tearDown allows to clear those preparations.
Example with setUp
and tearDown
:
import org.as2lib.test.unit.TestCase;
class MyTestCase extends TestCase {
private static var content:String="1";
private static function setContent(newContent:String):Void {
content = newContent;
}
private static function getContent(Void):String {
return content;
}
private var cache:String;
private function setUp() {
cache = getContent();
}
private function testOne() {
trace(getContent()); // 1
setContent("2");
trace(getContent()); // 2
}
private function testTwo() {
trace(getContent()); // 1
setContent("3");
trace(getContent()); // 3
}
private function tearDown() {
setContent(cache);
}
}
static public function blockCollecting(Void):Boolean
Blocks the collection of
.
true
to block the collection
public function setUp(Void):Void
Template method to set up the testcase before running a method.
This method will get called before executing each method in a clean, new instance.
public function tearDown(Void):Void
Template method to tear down the testcase after running a method.
This method will get called after the execution of each method of this testcase.
public function run(Void):TestRunner
Runs this testcase.
Runs all methods of this testcase in an new container and logs all its events.
TestRunner
to the TestCase
run() in org.as2lib.test.unit.Test
public function getTestRunner(Void):TestRunner
Returns the TestRunner
that executes this TestCase
.
TestRunner
that executes this TestCase