1205 S '03
Lectures
 
Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5
Lecture 6
Lecture 7
Lecture 8
Lecture 9
Lecture 10
Lecture 11
Lecture 12

From Specifications to Tests


Dijsktra: "Tests only show the presence of errors, not their
absence."

Discuss: What does this mean? Tests don't establish the
correctness of your product. They can merely find some of the
errors in it. The better the test suite, the more errors it is
likely to find.

Before we can write code, we need to have a clue of what it means
for the code to succeed. Since this grows a lot over time, we need
to build methods that automatically test classes and
methods. Without automatic testing, you have to sit there and work
out manually whether the method succeeded, or kind of
succeeded. If you change something in the classes, run the test
suite again. Make sure you didn't inadvertently affect the quality
of some other code.

Examples and Tests
----------------------------------------

When you implement the specs (the story) for a use case, your goal
is to test this unit. That's called a *unit test*. Later on, when
you link all the pieces you will write *systems* or *integration*
tests.

In some cases, the unit may consist of smaller units. Write a test
suite for each.

Before you write unit tests, you will need some basic examples of
all (most) classes in your use case's diagram. Go ahead, make
them. Proceed bottom-up, i.e., start with those structures that
don't have methods yet. Just include the examples as static fields
in the same class.

Now write a test for the use case. Exploit the examples that you
have.

What is a test? A test is the application of a statement (or an
expression) and the expected effect and result. You may also wish
to test whether a statement/expression raise the appropriate
exception.

Example 1:
----------------------------------------

Let's make a simple example by hand. Develop a function that
determines the cost for a list of books. A book is an author, a
title, and a price.

Live demo: DrScheme live-demo with test suite tool.

Example 2:
----------------------------------------

Now let's look at our ordering example. Yes, you do need to
simulate the ordering of several products by hand, without GUI
interface for picking and clicking.

You will need some methods for dealing with tests:

// print msg, if b is false; count false b's
void check(String msg, boolean b)

Test code is silent if the test is successful, i.e., if the
expected outcome and the actual outcome match.

Time to look:

draft 0: writing the classes as code

draft 1: the test class, plus the test case

draft 2: the full code

If you're using Java, look for JUnit.

What and When to test
----------------------------------------

A method for adding 1 to a number is probably easily correct.

A method for packing a jumbo jet with packages probably requires
extensive testing.

It is important to write tests that are likely to fail. Otherwise
we quickly neglect tests.

Write tests first.
Inspect tests for code that you must modify. If there are no
tests, develop a test suite, especially with tests that have to be
changed as you modify the code. Make sure that the functionality
that you're changing exists and works.



last updated on Tue Jan 11 13:12:34 EST 2005generated with PLT Scheme