Exercise 3 – Demonstration of a test framework

 

Reminder:  Look at <file:/home/jake/CSC/index.html> for updates or corrections!

 

Goal: Gain familiarity with a typical test framework

 

First, create a local copy of the exercise files.

 

   cd

   tar xf ~jake/CSC/exercise3.tar

   cd exercise3

   source setup.sh

 

You’ll find our sum-of-primes example in the file SumPrimes.java.  Its been recast as a Java class. To compile and run it, printing the sum of primes less than 7:

 

   ./build

   java SumPrimes 7

 

If you want to see the actual compilation commands used, look inside the “build” file.  Note that this is just recompiling everything and leaving the resulting machine-readable files in the current directory.  This is a fine approach in a simple project like this exercise; larger projects will use some form of “make” tool to do this smarter and more efficiently.

 

The file TestSumPrimes.java contains the code to setup JUnit, plus a single test.  Take a look at it, and see if you understand what it contains.  You can run it with or without a GUI, depending on whether you provide an extra argument.  To invoke the GUI:

 

  java TestSumPrimes g

 

or in command-line form, which is faster:

 

  java TestSumPrimes

 

In its initial form, the test just makes sure that you can create a SumPrimes object, and passes.  There are commented-out tests that will fail in the manner we discussed in lecture.  Remove the leading // from those lines, and watch the test fail when you run it:

 

  (edit the TestSumPrimes.java file)

  ./build

  java TestSumPrimes g

 

Isn’t that red bar ugly? Now fix the bug(s) (Hints: its OK for a prime number to be divisible by one; there are two other bugs), and see a lovely green bar.   Finally, add a couple more tests to check other ranges.  (The sum of primes from 1 to 20 is 78)


Hints:

 

*) It might be simpler to separate development and test of the "is N prime?" code from the "check the sum over a range" code.

 

*) Its OK for a prime number to be divisible by 1.

 

*) If you try to divide a prime number by itself, you'll find that the remainder is zero.