Access keys

TestCase

Kind of class: class
Inherits from: LogSupport < BasicClass
Implements:
Known subclasses:
Version: 2.0
Author: Martin Heidegger
Classpath: org.as2lib.test.unit.TestCase
File last modified: Wednesday, 21 September 2005, 18:39:14
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 namecondition
assertTruevalue === true
assertFalsevalue === false
assertEqualsObjectUtil.compare(a,b);
assertAlmostEquals(a < b && a+x > b) ||
(a > b && a-x < b)
assertNotEquals!ObjectUtil.compare(a,b);
assertSamea === b
assertNotSamea !== b
assertNullvalue === null
assertNotNullvalue !== null
assertUndefinedvalue === undefined
assertNotUndefinedvalue !== undefined
assertEmptyvalue == null (equals == undefined)
assertNotEmptyvalue != null (equals != undefined)
assertInfinityvalue === Infinity
assertNotInfinityvalue !== Infinity
assertThrowscall(arguments) throws exception
assertNotThrowscall(arguments) doesnt throw exception
assertTypeOfvalue typeof type
assertInstanceOfvalue 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);
  }
}

Summary

Class properties
Instance properties
Instance properties inherited from LogSupport
Instance methods
Instance methods inherited from LogSupport
Instance methods inherited from BasicClass

Class properties

DEFAULT_MAX_DIFF

static DEFAULT_MAX_DIFF:Number = 1e-10
(read,write)

Class methods

blockCollecting

static function blockCollecting (
Void) : Boolean
Blocks the collection of
org.as2lib.test.unit.TestSuiteFactory#collectAllTestCases.
Returns:
true to block the collection

Instance methods