Quick Index
Unit Testing Lingo/Jargon


WordMeaning
🚙UnitThe unit of code (e.g., a class) being tested -- examples may be Car, Employee, City, Student, Instructor, Course, ... (whatever your modeling domain subject is)
🚙ModelAnother name for unit
💡Unit TestA class that contains tests that test the unit -- generally named CarTest, EmployeeTest, etc
 
💥Showstopper BugA bug that prevents a program from starting/running or stops it from functioning (program crash)
🐞Functional BugA bug that causes a program has behavior/results that does not match the expected behavior (relative to the program functional requirements)
🐞DefectAnother name for bug


Objectives


The objective of this chapter is to learn how to write minimal tests called smoke tests. 🧑‍🚒

Make sure to review the concepts of showstopper bugs and smoke tests.

Basis for Smoke Test Code


For a given unit, the starting point for using the unit (e.g., class) is generally constructing it.

Therefore, unit construction is the basis of a smoke test. The test typically requires only two steps:


This 1-2 test pattern might have us asking if this is too simplistic to be useful. We'll soon answer that question.

In this chapter, we'll code the smoke tests, and in the next chapter we'll use them to go bug hunting.

Unit (Model)


For this example, we will be testing a unit named City.

The City class summary and key method summaries are shown below. We will use these summaries when coding the tests.

City Structure
class name: City
access: public
package: model.geography
City Method Headers
We will test the following constructors:
/** Returns new City object with passed lat and long values */
public static City fromNameLatLong(String name, double lat, double lon)

/** Returns new City object with name "Unnamed" and location (0, 0) */
public static City newInstance()

/** Returns nice display string for this City */
public String toString()



Smoke Test General Pattern


Test
The test pattern is a simple two-step:

  • Construct an object
  • Call a core method on the object
/**
 * Conduct a smoke test for the construction of
 * a City with no args
*/
public void test_newInstance()  {
	prn("\n-- test_newInstance --");
	
1
City city = City.newInstance();
2
prn(city.toString()); }


Expected Result
This test is not looking for any certain result but rather the test is simply asserting that the code runs (without crashing).

  • If no exception occurs -- code ran -- no showstopper bug encountered
  • If exception occurs -- code did not run -- showstopper bug encountered (i.e., exception occurred)


Smoke Tests


Here are the two smoke tests we will use -- we don't need much code for these tests.
/**
 * Conduct a smoke test for the construction of
 * a City with no args
*/
public void test_newInstance()  {
	prn("\n-- test_newInstance --");
	City city = City.newInstance();
	prn(city.toString());
}

/**
 * Conduct a smoke test for the construction of
 * a City with passed name and location
*/
public void test_fromNameLatLong()  {
	prn("\n-- test_fromNameLatLong --");
	City city =
		City.fromNameLatLong("Brooklyn Center", 45.076076, -93.332728);
	prn(city.toString());
}



Try the Example


We coders learn by playing with and running code.

Download the example and take the project for a spin. Make sure it compiles and runs in the IDE or code editor of your choice. You will be running the class CityTest (full path is test.cases.geography.CityTest).

Let's Go Bug Hunting


Let's proceed to the next chapter to find out if these tests do their job of finding showstopper bugs.