Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n...

29
Chapter 8 Testing a class

Transcript of Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n...

Page 1: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Chapter 8Testing a class

Page 2: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

This chapter discusses Testing in general. Testing a single class. Test plans. Building a test system.

Page 3: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Testing

Testing is an activity whose goal is to determine if the implementation is correct.

A successful test is one that reveals some previously undiscovered error.

Page 4: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Testing Phases

Test activities are determined and test data selected.

The test is conducted and test results are compared with expected results.

Page 5: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Test design

Begins with an analysis of: The functional specifications of the

system. The ways in which the system will

be used (referred to as “use cases”).

Testing based on these considerations is referred to as “black box testing.”

Page 6: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Black Box testing The test designer ignores the internal

structure of the implementation in developing the test.

The expected external behavior of the system drives the selection of test activities and data.

The system is treated as a “black box” whose behavior can be observed, but whose internal structure cannot.

Page 7: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Test case

Test cases are defined by: A statement of case objectives. The data set for the case. The expected results.

Page 8: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Equivalency groups

Test cases are chosen from each equivalency group.

Particular attention is paid to data that lie on group “boundaries.”

Page 9: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Test plan

A test plan is a document that describes the test cases giving the purpose of each case, the data values to be used, and the expected results.

Test design should be carried out concurrently with system development.

Developing and refining test cases based on the implementation of the system is referred to as “white box testing.”

A test plan can be based on class specifications only.

Page 10: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Test system

Test systems allow us to interact with the object we want to test.

The test system will let us create the object to be tested and then act as a client of the object.

Page 11: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Testing Counter Class specifications:public class Counter

A simple integer counter.public Counter ()

Create a new Counter initialized to 0.public int count ()

The current count.ensure:

this.count() >= 0public void reset ()

Reset this Counter to 0. public void increment ()

Increment the count by 1.

public void decrement ()

Decrement the count by 1. If the count is 0, it is not decremented.

Page 12: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Possible systemEnter number denoting action to perform:Increment..............1Decrement..............2Reset..................3Create a new Counter...4Exit...................5Enter choice.1The current count is: 1Enter number denoting action to perform:Increment..............1Decrement..............2Reset..................3Create a new Counter...4Exit...................5Enter choice.1The current count is: 2 ...

Page 13: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Building a test system

The test system will be composed of two objects: A Counter to be tested. A test driver to invoke the

Counter’s methods. The test driver prompts the user

with a menu, gets input from the user, and provides output in the form of the count.

Page 14: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

CounterTUI

TUI -> “Textual User Interface.”

Page 15: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Basic input/output

Basic I/O consists of reading from standard input (keyboard) and writing to standard output (monitor).

Page 16: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Basic input/output (cont.) We will use methods provided by the authors

(OOJ.basicIO.BasicFileWriter, OOJ.basicIO.BasicFileReader).

public BasicFileWriter ()

Create a BasicFileWriter attached to standard output.

public void displayLine (String line)

Write the specified line to standard output.

public BasicFileReader ()

Create a BasicFileReader attached to standard input.

public void readInt()

Reads a new int from standard input.

public int lastInt()

Returns the int read by readInt().

public void readline ()

Read rest of line from standard input.

Page 17: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

CounterTUI specificationspublic class CounterTUI

A text-based test driver for the class Counter.

public CounterTUI ()

Create a new test driver with a Counter to test.

public void start ()

Conduct the test.

Page 18: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

CounterTUI implementationpublic class CounterTUI {

public CounterTUI () { input = new OOJ.basicIO.BasicFileReader(); output = new OOJ.basicIO.BasicFileWriter(); counter = new Counter(); …}…private OOJ.basicIO.BasicFileReader input;private OOJ.basicIO.BasicFileWriter output;private Counter counter;

}

Page 19: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

CounterTUI implementation (cont.) If we look at what happens when

the test is conducted, we see that the following sequence of actions are repeated over and over: Display menu to the user and

prompt the user for input. Get the user’s input. Perform requested action. Display results.

Page 20: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

CounterTUI implementation (cont.) We define an instance variable to hold the

user’s choice.private int choice; // the user’s most

// recent choice The local methods we will create are

specified as follows.private void displayMenuPrompt ()

Display the menu to the user.private void getChoice ()

Get the user’s choice.private void executeChoice ()

Perform the action indicated by this.choice, and display the resultsto the user.

Page 21: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

While loop

A while loop continues to perform an action as long as a condition is true.

The while statement is composed of a condition (a boolean expression) and another statement, the body.

syntax:while ( condition )

body

Page 22: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Page 23: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Page 24: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Page 25: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Implementation

main method: the top-level method that initiates execution of a system.

Although the main method is defined in a class, it is really outside the object-oriented paradigm.

It’s only purpose should be to create the top level objects and get the system started.

Page 26: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Implementation (cont.)

/*** A test system for the class Counter.*/public class CounterTest {

/*** Create the user interface, start * the system.*/public static void main (String[] argv) { CounterTUI theInterface = new

CounterTUI(); theInterface.start();}

}

Page 27: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Test planaction expected commentincrement 1 increment from initial stateincrement 2 sequence of incrementsincrement 3increment 4decrement 3 sequence of decrementsdecrement 2increment 3 increment follows decrementreset 0increment 1 increment follows resetdecrement 0decrement 0 decrement 0 countreset 0decrement 0 decrement follows resetcreate 0 initial statedecrement 0 decrement from initial state

Page 28: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

We’ve covered

The process of testing. Test systems. Text-based user interface. “read-process-write” loop. while loops. main method.

Page 29: Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.

Glossary