Testing

 

Writing a program and getting it to compile does not mean that we have written a program that works correctly. In fact it's extremely rare for anyone to write a program that works correctly the first time. So how do we know if our program is correct? We test it: we run the program with some sample data and check whether the results are correct. If the results are not correct then we need to modify the program.

What data should we use? How many times do we need to do this? It depends on the program, but we always want to run one test for each of the different conditions that might occur in the program.

Formal testing methods require the creation of a test plan, which describes each input to be tested and the expected output for that input. It is a critical step to test all conditions in a program even if a written test plan is not required. Insufficient testing almost always results in a program that doesn't work. When you submit your lab you should know whether it works. You should not be surprised if I find errors when testing your code; if you have tested your program well then you should know whether there are any errors remaining when you hand it in.

Two main types of testing are "black-box testing" and "white-box testing". The "box" in question is the program code. In black-box testing, we ignore the program code. Instead we look at the specs (like this document or the lab assignment) and test all possibilities it describes. In white-box testing we look at the code and make sure to test all paths in the program. We make sure to have test data that goes through all ifs and all elses, we make sure to run through each loop several times.

Most errors come from the unusual conditions in our code. This could be from boundary errors: processing the dividing value incorrectly. For example, a student is part time if he/she takes fewer than 12 credits. This means that full time students take 12 credits or more. If we don't test a student taking 12 credits we are not sure whether we coded the comparisonWe tend to think about the normal case: the typical input value or the middle of the array. The first or last array element may need special processing but we may not think about that when we are coding. A negative input value or some other unusually small or large value may not be handled the same way.

Example Description

Tuition for students at State University is based on the number of credits taken. The rules for tuition charges are:

If you take 12 credits or more, you are a full time student and your tuition is $5400 and your fees are $950.
If you take fewer than 12 credits, you are a part time student and your tuition is $375 per credit plus $80.50 fees per credit.

Your program will have the following input:

Your program will print the following:

Black-Box Test Data

Choose test data that makes a student part time and data that makes a student full time.

It's helpful to use numbers that make your calculations easy. That's why I chose 10 credits.

Example Program

		Scanner keyboard = new Scanner(System.in);

		double partTuition = 375;
		double partFees = 80.50;
		double fullTuition = 5400;
		double fullFees = 950;
		String name;
		int credits;
		double tuition;
		double fees;
		double totalCost;

		// Input for name and credits
		System.out.println("What is your first and last name?");
		name = keyboard.nextLine();

		System.out.println("How many credits are you taking this semester?");
		credits = keyboard.nextInt();

		// Output name, credits, full/part time
		System.out.println("Name: " + name);
		System.out.println("Number of Credits: " + credits);
		System.out.print("Status: ");
		if (credits >= 12)
			System.out.println("full time");
		else
			System.out.println("part time");

		// calculate tuition, fees, and total
		if(credits >= 12) {
			tuition = fullTuition;
			fees = fullFees;
		}
		else {
			tuition = credits * partTuition;
			fees = credits * partFees;
		}

		// print tuition, fees, and total
		totalCost = tuition + fees;
		System.out.printf("Tuition: $ %6.2f\n", tuition);
		System.out.printf("Fees: $ %6.2f\n", fees);
		System.out.printf("Total Payment Due: $ %6.2f\n", totalCost);
	}

White-Box Test Data

Create data that will go through both parts of the if.

Boundary Values

Our test data is good, but it is missing an important issue. A student is part time if he/she takes fewer than 12 credits. So our test values of 10 and 15 credits are good, but they don't test the boundary: is a student with 11 credits part time while a student with 12 credits is full time? An error on a boundary value is a common error and should be checked. We should add tests for 11 credits (should be part time with appropriate calculations) and 12 credits (should be full time with appropriate calculations).

When designing our code we tend to think of the general case, and not the unusual case. This can cause problems at the boundary, such as at the beginning or end of an array. These values should always be tested.

Example Description

Given an array of ints, move all 0's to the end of the array.

Comment: since it is not mentioned, we don't have to keep the other elements in order. This is an important point: if it is not stated as a requirement, then we do not have to follow it. So we need to move the 0's to the end but we can use an algorithm that moves other elements around.

Black Box Testing

Read the description of the problem and decide on some test data. Consider the typical condition and the unusual conditions.

Write your test data below:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

White Box Testing

Consider the following code:

   i = 0;
   last = arr.length - 1;
   while (i < last) {
      if (arr[i] == 0) {
         hold = arr[i];
         arr[i] = arr[last];
         arr[last] = hold;
         last--;
      }
      i++;
   }

Looking at the code, what would your test data be? How does that compare with your previous test data?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Once you have the test data and the code, you run the code with the test data. When you check the results then you know whether there are any bugs in the code.

Are there any bugs in this code? (Hint: um,...,yes...) What is wrong? How can it be fixed?

Practice

Make up test data for Lab 1. Remember to keep the test data simple so it's easier for you to check.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013