Quick Index
Overview


This is an example of a test case that runs a collection of tests on a rectangle object.

beforeEach


The "beforeEach" is is called before each test. It allows general pre-test initialization.

It is optional method. It is helpful for this example, so we will implement it.

We will construct a rectangle object instance that will be the "testee" (the object to be tested).

This method is called before each test.
We initialize the unit (a rectangle) that will be tested. It has a width of 10 and height of 5 (it is reconstructed for each test).
beforeEach() {
	//Called before each test for test setup (if needed)
	//Construct a unit model for testing (we use ivar "unit")
	super.beforeEach();
	this.unit = new Rectangle();
}

Tests


This section contains the test methods. Each test will "assert" that a certain condition is true and the result is that the assert passes or fails. Failing tests help us pinpoint where we have a bug that needs fixing.

A common assert we use is "assertEquals(expectedValue, actualValue)".

Simply return the name of the tested unit class (housekeeping method).

getUnitName() {
	//Return class name of unit
	return 'Rectangle';
}
Test that the unit (testee) returns width as expected (10) for "getWidth"
testGetWidth() {
	this.assertEquals(0, this.unit.getWidth());
}
Assert that the unit returns height as expected (5) for "getHeight"
testGetHeight() {
	this.assertEquals(0, this.unit.getHeight());
}
Assert that the unit returns an area as expected (10*5) for "getArea"
testGetArea() {
	this.assertEquals(0, this.unit.getArea());
}
Assert that testee returns a perimeter as expected (10+10+5+5) for "getPerimeter"
testGetPerimeter() {
	this.assertEquals(0, this.unit.getPerimeter());
}
The expected result for getDiagonal is 11.18034 (see "References" at bottom of this page).

Floating point (float) comparisons are not exact comparisons. Thus we use a "tolerance" here of 0.0001 to allow a little "fuzz".

testGetDiagonal() {
	this.assertFloatEquals(0, this.unit.getDiagonal(), 0.0001);
}
Flip the unit and then assert that dimensions have flipped
testFlip() {
	const rec = this.unit;
	rec.flip();
	this.assertEquals(0, this.unit.getWidth());
	this.assertEquals(0, this.unit.getHeight());
}
Multiply the unit dimensions and then assert the dimensions match expected values
testMultiply() {
	const rec = this.unit;
	rec.multiply(2);
	this.assertEquals(0 * 2, this.unit.getWidth());
	this.assertEquals(0 * 2, this.unit.getHeight());
}

References



Complete Source Code


/*
	RectangleEdgeTestCase
	Similar to RectangleTestCase in format.
	The only change is we construct a model unit (testee)
	with "edge case" condtions (use constructor
	with no params to make sure Rectangle is
	constructed and operates properly for this case.
*/

//=========================================================

//Housekeeping statement (add name of test class)
//Note: param must be a string
addTestCase("RectangleEdgeTestCase");

//=========================================================

class RectangleEdgeTestCase extends TestCase {
	//ivars: unit
	
	//-----------------
	//Unit Test Support
	
	getUnitName() {
		//Return class name of unit
		return 'Rectangle';
	}

	beforeEach() {
		//Called before each test for test setup (if needed)
		//Construct a unit model for testing (we use ivar "unit")
		super.beforeEach();
		this.unit = new Rectangle();
	}
	
	//-----------------
	//Tests
	
	testGetWidth() {
		this.assertEquals(0, this.unit.getWidth());
	}
	
	testGetHeight() {
		this.assertEquals(0, this.unit.getHeight());
	}

	testGetArea() {
		this.assertEquals(0, this.unit.getArea());
	}
	
	testGetPerimeter() {
		this.assertEquals(0, this.unit.getPerimeter());
	}
	
	testGetDiagonal() {
		this.assertFloatEquals(0, this.unit.getDiagonal(), 0.0001);
	}

	testFlip() {
		const rec = this.unit;
		rec.flip();
		this.assertEquals(0, this.unit.getWidth());
		this.assertEquals(0, this.unit.getHeight());
	}
	
	testMultiply() {
		const rec = this.unit;
		rec.multiply(2);
		this.assertEquals(0 * 2, this.unit.getWidth());
		this.assertEquals(0 * 2, this.unit.getHeight());
	}
	
}
	


//---------------------------------------------------