Testing a class - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Testing a class

Description:

Testing is an activity whose goal is to determine if the implementation ... public static void main (String[] argv) { CounterTUI theInterface = new. CounterTUI ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 30
Provided by: hyperspace
Category:
Tags: argv | class | testing

less

Transcript and Presenter's Notes

Title: Testing a class


1
Chapter 8
  • Testing a class

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

3
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.

4
Testing Phases
  • Test activities are determined and test data
    selected.
  • The test is conducted and test results are
    compared with expected results.

5
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.

6
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.

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

8
Equivalency groups
  • Test cases are chosen from each equivalency
    group.
  • Particular attention is paid to data that lie on
    group boundaries.

9
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.

10
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.

11
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() gt 0
  • public 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.

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

13
Building a test system
  • The test system will be composed of two objects
  • A Counter to be tested.
  • A test driver to invoke the Counters methods.
  • The test driver prompts the user with a menu,
    gets input from the user, and provides output in
    the form of the count.

14
CounterTUI
  • TUI -gt Textual User Interface.

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

16
Basic input/output (cont.)
  • We will use methods provided by the authors
    (OOJ.basicIO.BasicFileWriter, OOJ.basicIO.BasicFil
    eReader).
  • 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.

17
CounterTUI specifications
  • public 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.

18
CounterTUI implementation
  • public class CounterTUI
  • public CounterTUI ()
  • input new
  • OOJ.basicIO.BasicFileReader()
  • output new OOJ.basicIO.BasicFileWr
    iter()
  • counter new Counter()
  • private OOJ.basicIO.BasicFileReader
  • input
  • private OOJ.basicIO.BasicFileWriter
  • output
  • private Counter counter

19
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 users input.
  • Perform requested action.
  • Display results.

20
CounterTUI implementation (cont.)
  • We define an instance variable to hold the users
    choice.
  • private int choice // the users 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 users choice.
  • private void executeChoice ()
  • Perform the action indicated by this.choice, and
    display the results
  • to the user.

21
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

22
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
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.
  • Its only purpose should be to create the top
    level objects and get the system started.

26
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()

27
Test plan
  • action expected comment
  • increment 1 increment from initial
    state
  • increment 2 sequence of increments
  • increment 3
  • increment 4
  • decrement 3 sequence of
    decrements
  • decrement 2
  • increment 3 increment follows
    decrement
  • reset 0
  • increment 1 increment follows reset
  • decrement 0
  • decrement 0 decrement 0 count
  • reset 0
  • decrement 0 decrement follows reset
  • create 0 initial state
  • decrement 0 decrement from initial
    state

28
Weve covered
  • The process of testing.
  • Test systems.
  • Text-based user interface.
  • read-process-write loop.
  • while loops.
  • main method.

29
Glossary
Write a Comment
User Comments (0)
About PowerShow.com