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(10, 5);
}

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)".

Housekeeping - return class name of unit
getUnitName() {
	//Housekeeping - return class name of unit
	return 'Rectangle';
}
Test that the unit (testee) returns width as expected (10) for "getWidth"
testGetWidth() {
	this.assertEquals(10, this.unit.getWidth());
}
Assert that the unit returns height as expected (5) for "getHeight"
testGetHeight() {
	this.assertEquals(5, this.unit.getHeight());
}
Assert that the unit returns an area as expected (10*5) for "getArea"
testGetArea() {
	this.assertEquals(10 * 5, this.unit.getArea());
}
Assert that testee returns a perimeter as expected (10+10+5+5) for "getPerimeter"
testGetPerimeter() {
	this.assertEquals(10 + 10 + 5 + 5, 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(11.18034, this.unit.getDiagonal(), 0.0001);
}
Flip the unit and then assert that dimensions have flipped
testFlip() {
	const rec = this.unit;
	rec.flip();
	this.assertEquals(5, this.unit.getWidth());
	this.assertEquals(10, 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(10 * 2, this.unit.getWidth());
	this.assertEquals(5 * 2, this.unit.getHeight());
}

References



Complete Source Code


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

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

	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(10, 5);
	}
	
	//-----------------
	//Tests
	
	testGetWidth() {
		this.assertEquals(10, this.unit.getWidth());
	}
	
	testGetHeight() {
		this.assertEquals(5, this.unit.getHeight());
	}

	testGetArea() {
		this.assertEquals(10 * 5, this.unit.getArea());
	}
	
	testGetPerimeter() {
		this.assertEquals(10 + 10 + 5 + 5, this.unit.getPerimeter());
	}
	
	testGetDiagonal() {
		this.assertFloatEquals(11.18034, this.unit.getDiagonal(), 0.0001);
	}

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