CS 139 Algorithm Development
Lab09B: Test Coverage

Objectives

Students learn to write tests to fully cover decisions and iteration.

Background

For this lab, you will write test methods and submit them to WebCAT, one at a time, for evaluation.

New Terms

  • code coverage: the percentage of code that is completely exercised during testing
  • white-box testing: writing test cases to exercise specific program statements
  • black-box testing: writing test cases to validate program specifications (i.e., testing the final output against the input)

Materials

  • Lab09B.java
Submission
  • Complete the CodeBat exercises indicated below at http://codingbat.com. Be sure to include your instructor e-mail in your profile.

  • Submit your completed Lab09B_Test.java to WebCAT, as instructed in the steps below.

Acknowledgment

V1.0; by Ralph Grove, 10/22/2012



Part 1 - Set Up
  1. Create a new folder named lab09b on your Desktop or removable drive.

  2. Download Lab09B.java to the folder.

  3. Create a new class named Lab09B_Test in the same folder. Include an import for junit.framework.TestCase and make your class an extension of TestCase.

Part 2 - Warmup

  1. In the CodingBat Java / Logic-1 group, do inOrderEqual - codingbat.com/prob/p140272. You should do this part individually, but may receive help from a partner.


Part 3 - Testing IF Statements

Testing an IF statement requires that every possible outcome be exercised in the test code.
  1. Examine the code in Lab09B.ifMethod(), then write test cases in Lab09B_Test.testIfMethod() that will cover every possibility for each of the if statements.

  2. After you've finished this step, zip Lab09B.java and Lab09B_Test.java, submit the zip file to WebCAT, and then examine the code for Lab09B.ifMethod() on WebCAT to ensure that it was fully covered. Any red lines indicate a lack of coverage. Revise your tests and resubmit if necessary.


Part 4 - Testing SWITCH Statements

Testing a SWITCH statement requires that every case be examined, including the default case.
  • This SWITCH statement has four branches, plus the default.
    Sample test cases: a = 1; a = 2; a = 11; a = 12; a = 5

    switch (a) {
    case 1:
      return 100;
      break;
    case 2:
      return 200;
      break;
    case 11:
      return 300;
      break;
    case 12:
      return 400;
      break;
    default:
      return 500;
    }

  1. Examine the code in Lab09B.caseMethod(), then write test cases in Lab09B_Test.testCaseMethod() that will cover every possibility.

  2. After you've finished this step, zip Lab09B.java and Lab09B_Test.java, submit the zip file to WebCAT, and then examine the code for Lab09B.caseMethod() on WebCAT to ensure that it was fully covered. Any red lines indicate a lack of coverage. Revise your tests and resubmit if necessary.


Part 5 - Testing Iteration (WHILE, DO-WHILE, FOR) Statements

When testing a loop, provide test data that will execute a varying number of iterations, including (if possible) zero iterations, one iteration only, and multiple iterations.
  1. Examine the code in Lab09B.loopMethod(), then write test cases in Lab09B_Test.testLoopMethod() that will execute the loop zero, one, and multiple times. Also make sure that your test cases execute each branch of the if statement included in the loop.

  2. After you've finished this step, zip Lab09B.java and Lab09B_Test.java, submit the zip file to WebCAT, and then examine the code for Lab09B.loopMethod() on WebCAT to ensure that it was fully covered. Any red lines indicate a lack of coverage. Revise your tests and resubmit if necessary.
     

Part 6 - Black Box Testing

It's also necessary to test code according to its specifications (i.e. what it's supposed to do) rather than how it works inside. Specification-based testing is also called black-box testing, since testing is based on documentation only and the inside of the code either can't be seen or is ignored. This type of testing requires that every possible scenario described in the documentation for the code be examined with one or more test cases. Black-box testing also entails trying to "break" the code by creating test cases that the programmers might not have foreseen.
  1. The documentation for the method squareRoot() in Lab09B.java explains its purpose. Create a variety of test cases for this method in Lab09B_Test.testSquareRoot(), ensuring that you cover every possibility. Also try to break the method with weird test cases.

    Remember that this method promises only limited precision. So, for example, the square root of 4.0 won't necessarily be 2.0, but the absolute value of the difference between the two should be within the stated precision. Absolute value can be computed with the function Math.abs(). You should use the TestCase method assertTrue() rather than assertEquals() to test that the absolute value is less than or equal to the stated precision:   

          assertTrue("...error message... ", Math.abs(expected - actual) <= PRECISION);

  2. After you've finished this step, zip Lab09B.java and Lab09B_Test.java, submit the zip file to WebCAT, and then examine the code for Lab09B.squareRoot() on WebCAT to ensure that it was fully covered. Any red lines indicate a lack of coverage. Revise your tests and resubmit if necessary.