90
Using UML, Patterns, and Java Object-Oriented Software Engineering Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software Engineering Technische Universität München http://wwwbruegge.in.tum.de

Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

  • Upload
    lyanh

  • View
    221

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

Usi

ng U

ML

, P

atte

rns,

and J

ava!

Ob

ject

-Ori

ente

d S

oft

ware

En

gin

eeri

ng!

Software Engineering II Testing

5 May 2009

Bernd Bruegge Applied Software Engineering

Technische Universität München http://wwwbruegge.in.tum.de

Page 2: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 2!

Outline of Testing

Today:

•! Terminology

•! Testing Activities

•! Unit testing

•! Integration testing

•! Testing strategy

•! Design patterns & testing

•! System testing

•! Function testing

•! Acceptance testing.

Next Lecture:

•! Model-based Testing

•! U2TP Testing Profile

Page 3: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

Famous Problems •! A F-16 fighter plane crossed the equator using autopilot

•! Result: plane flipped over

•! Reason?

•! Reuse of autopilot software from a rocket

•! The Therac-25 was a machine used for radiation therapy

•! Result: Patients were given massive overdoses of radiation, approximately 100 times the intended dose (6 deaths)"

•! Reason: Failure only occurred when a particular sequence of keys was entered on the VT-100 controlling a PDP-11: X, !, E, Enter

•! The "X” erroneously selected 25MV photon mode. The “Cursor up” was an attempt to correct the mistake. The "E” followed by "Enter” then attempted to correctly select 25 MeV Electron mode.

•! The machine responded with an error message “MALFUNCTION 54”, which was not explained in the user manual. Machine shut down briefly and then showed under dosage of radiation.

•! Because of this the operator hit “P” to continue treatment.

Page 4: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 4!

Terminology

•! Failure: Any deviation of the observed behavior from the specified behavior

•! Erroneous state (error): The system is in a state such that further processing by the system can lead to a failure

•! Fault: The mechanical or algorithmic cause of an error (“bug”)

•! Validation: Activity of checking for deviations between the observed behavior of a system and its specification.

Page 5: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 5!

What is this?

A failure?

An error?

A fault?

We need to describe specified and desired behavior first!

Page 6: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 6!

Erroneous State (“Error”)

Page 7: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 7!

Algorithmic Fault

Page 8: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 8!

Mechanical Fault Superuser!

Page 9: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 9!

The F-16 Problem

•! Where is the failure?

•! Where is the error?

•! Where is the fault? •! Bad use of implementation

inheritance

•! A Plane is not a rocket.

Rocket!

Plane!

Page 10: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 10!

Examples of Faults and Errors

•! Faults in the Interface specification

•! Mismatch between what the client needs and what the server offers

•! Mismatch between requirements and implementation

•! Algorithmic Faults

•! Missing initialization

•! Incorrect branching condition

•! Missing test for null

•! Mechanical Faults (very hard to find)

•! Operating temperature outside of equipment specification

•! Errors •! Null reference errors

•! Concurrency errors

•! Exceptions.

Page 11: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 11!

How do we deal with Errors, Failures and Faults?

Page 12: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 12!

Modular Redundancy

Page 13: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 13!

Declaring the Bug as a Feature

Page 14: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 14!

Patching

Page 15: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 15!

Testing

Page 16: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 16!

Another View on How to Deal with Faults

•! Fault avoidance •! Use a good methodology to reduce complexity

•! Use configuration management to prevent inconsistency

•! Apply verification to prevent algorithmic faults

•! Use reviews

•! Fault detection •! Testing: Activity to provoke failures in a planned way

•! Debugging: Find and remove the cause (Faults) of an observed failure

•! Monitoring: Deliver information about state => Used during debugging

•! Fault tolerance •! Enable exception handling

•! Provide modular redundancy.

Page 17: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 17!

Taxonomy for Fault Handling Techniques 5 05 2009

Fault Handling!

Fault "

Avoidance!

Fault "

Detection!

Fault "

Tolerance!

Verification!

Configuration"

Management!Methodoloy!

Atomic"

Transactions!

Modular"

Redundancy!

System "

Testing!

Integration"

Testing!

Unit!

Testing!

Testing! Debugging!

Page 18: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 18!

Observations

•! It is impossible to completely test any nontrivial module or system

•! Practical limitations: Complete testing is prohibitive in time and cost

•! Theoretical limitations: e.g. Halting problem

•! “Testing can only show the presence of bugs, not their absence” (Dijkstra).

•! Testing is not for free

=> Define your goals and priorities

Edsger W. Dijkstra (1930-2002)"" " " "!

- First Algol 60 Compiler!

"- 1968:!

- T.H.E. !

- Go To considered Harmful, CACM!

"- Since 1970 Focus on Verification!

and Foundations of Computer Science !

"-"1972 A. M. Turing Award!

Page 19: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 19!

Testing takes creativity

•! To develop an effective test, one must have:

•! Detailed understanding of the system

•! Application and solution domain knowledge

•! Knowledge of the testing techniques

•! Skill to apply these techniques

•! Testing is done best by independent testers

•! We often develop a certain mental attitude that the program should in a certain way when in fact it does not

•! Programmers often stick to the data set that makes the program work

•! A program often does not work when tried by somebody else.

behave!

Page 20: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 20!

Testing Activities

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Requirements"

Analysis!

Document!

Client!

Expectation!

System"

Design!

Document!

Object"

Design!

Document!

Developer! Client!

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 21: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 21!

Types of Testing

•! Unit Testing

•! Individual component (class or subsystem)

•! Carried out by developers

•! Goal: Confirm that the component or subsystem is correctly coded and carries out the intended functionality

•! Integration Testing •! Groups of subsystems (collection of subsystems) and

eventually the entire system

•! Carried out by developers

•! Goal: Test the interfaces among the subsystems.

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 22: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 22!

Types of Testing continued...

•! System Testing

•! The entire system

•! Carried out by developers

•! Goal: Determine if the system meets the requirements (functional and nonfunctional)

•! Acceptance Testing •! Evaluates the system delivered by developers

•! Carried out by the client. May involve executing typical transactions on site on a trial basis

•! Goal: Demonstrate that the system meets the requirements and is ready to use.

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 23: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 23!

When should you write a test?

•! Traditionally after the source code to be tested

•! In XP before the source code to be tested

•! Test-Driven Development Cycle

•! Add a test

•! Run the automated tests

=> see the new one fail

•! Write some code

•! Run the automated tests

=> see them succeed

•! Refactor code.

Page 24: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 24!

Unit Testing

•! Static Testing (at compile time)

•! Static Analysis

•! Review

•! Walk-through (informal)

•! Code inspection (formal)

•! Dynamic Testing (at run time) •! Black-box testing

•! White-box testing.

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 25: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 25!

Static Analysis with Eclipse

•! Compiler Warnings and Errors •! Possibly uninitialized Variable

•! Undocumented empty block

•! Assignment has no effect

•! Checkstyle •! Check for code guideline violations

•! http://checkstyle.sourceforge.net

•! FindBugs •! Check for code anomalies

•! http://findbugs.sourceforge.net

•! Metrics •! Check for structural anomalies

•! http://metrics.sourceforge.net

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 26: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 26!

Black-box testing

•! Focus: I/O behavior

•! If for any given input, we can predict the output, then the component passes the test

•! Requires test oracle

•! Goal: Reduce number of test cases by equivalence partitioning:

•! Divide input conditions into equivalence classes

•! Choose test cases for each equivalence class.

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 27: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 27!

Black-box testing: Test case selection

a) Input is valid across range of values •! Developer selects test cases from 3 equivalence classes:

•! Below the range

•! Within the range

•! Above the range

b) Input is only valid, if it is a member of a discrete set

•! Developer selects test cases from 2 equivalence classes:

•! Valid discrete values

•! Invalid discrete values

•! No rules, only guidelines.

Page 28: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 28!

Lecture Check: Where are we?

•! Terminology

•! Testing Activities

•! Unit testing

•! Static Testing

•! Dynamic Testing

•! Blackbox Testing

!!Example

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 29: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 29!

Black box testing: An example

public class MyCalendar {

public int getNumDaysInMonth(int month, int year)

throws InvalidMonthException

{ … }

}

Representation for month:!1: January, 2: February, …., 12: December!

Representation for year:!1904, … 1999, 2000,…, 2006, …!

How many test cases do we need for the black box testing of

getNumDaysInMonth()?!

Page 30: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 30!

White-box testing overview

•! Code coverage

•! Branch coverage

•! Condition coverage

•! Path coverage

=> Details in the exercise session about testing

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 31: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 31!

Unit Testing Heuristics

1. Create unit tests when object design is completed

•! Black-box test: Test the functional model

•! White-box test: Test the dynamic model

2. Develop the test cases

•! Goal: Find effective num- ber of test cases

3. Cross-check the test cases to eliminate duplicates

•! Don't waste your time!

4. Desk check your source code

•! Sometimes reduces testing time

5. Create a test harness

•! Test drivers and test stubs are needed for integration testing

6. Describe the test oracle

•! Often the result of the first successfully executed test

7. Execute the test cases

•! Re-execute test whenever a change is made (“regression testing”)

8. Compare the results of the test with the test oracle

•! Automate this if possible.

Page 32: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 32!

Excursion J_Unit: A Java Framework for unit testing

•! A Java framework for writing and running unit tests

•! Test cases and fixtures

•! Test suites

•! Test runner

•! Written by Kent Beck and Erich Gamma

•! Written with “test first” and pattern-based development in mind

•! Tests written before code

•! Allows for regression testing

•! Facilitates refactoring

•! JUnit is Open Source

•! www.junit.org

•! JUnit Version 4.6, released April 2009

Unit"

Testing!

Acceptance"

Testing!

Integration"

Testing!

System"

Testing!

Page 33: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 33!

*

JUnit Classes

Test

run(TestResult)

ConcreteTestCase

setUp() tearDown() runTest()

TestResult

TestCase

run(TestResult) setUp() tearDown()

testName:String

runTest()

TestSuite

run(TestResult) addTest()

UnitToBeTested

Methods under Test!

Skip!

J_Unit!

Slides!

Page 34: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 34!

An example: Testing MyList

•! Unit to be tested

•! MyList

•! Methods under test •! add()

•! remove()

•! contains()

•! size()

•! Concrete Test case

•! MyListTestCase

Page 35: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

Test

run(TestResult)

MyListTestCase

setUp() tearDown() runTest() testAdd() testRemove()!

TestResult

TestCase

run(TestResult) setUp() tearDown()

testName:String

runTest()

TestSuite

run(TestResult) addTest()

MyList

add() remove() contains() size()

*

Page 36: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 36!

Writing TestCases in JUnit public class MyListTestCase extends TestCase {

public MyListTestCase(String name) {

super(name);

}

public void testAdd() {

// Set up the test

List aList = new MyList();

String anElement = “a string”;

// Perform the test

aList.add(anElement);

// Check if test succeeded

assertTrue(aList.size() == 1);

assertTrue(aList.contains(anElement));

}

protected void runTest() {

testAdd();

}

}

Test

run(TestResult)

MyListTestCase

setUp() tearDown() runTest() testAdd() testRemove()!

TestResult

TestCase

run(TestResult) setUp() tearDown()

testName:String

runTest()

TestSuite

run(TestResult) addTest()

MyList

add() remove() contains() size()

*

Page 37: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 37!

Writing Fixtures and Test Cases

public class MyListTestCase extends TestCase {

// …

private MyList aList;

private String anElement;

public void setUp() {

aList = new MyList();

anElement = “a string”;

}

public void testAdd() {

aList.add(anElement);

assertTrue(aList.size() == 1);

assertTrue(aList.contains(anElement));

}

public void testRemove() {

aList.add(anElement);

aList.remove(anElement);

assertTrue(aList.size() == 0);

assertFalse(aList.contains(anElement));

}

Test Fixture

Test Case

Test Case

Page 38: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 38!

Collecting TestCases into TestSuites

public static Test suite() {

TestSuite suite = new TestSuite();

suite.addTest(new MyListTest(“testAdd”));

suite.addTest(new MyListTest(“testRemove”));

return suite;

}

Test

run(TestResult)

TestCase

run(TestResult) setUp() tearDown()

testName:String

runTest()

TestSuite

run(TestResult) addTest()

Composite Pattern!

*

Page 39: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 39!

Design patterns in JUnit

Test

run(TestResult)

ConcreteTestCase

setUp() tearDown() runTest()

TestResult

TestCase

run(TestResult) setUp() tearDown()

testName:String

runTest()

TestSuite

run(TestResult) addTest()

Command Pattern

Composite

Pattern

Adapter

Pattern

Template Method

Pattern

TestedUnit

*

Page 40: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 40!

Design patterns in JUnit

Test

run(TestResult)

ConcreteTestCase

setUp() tearDown() runTest()

TestResult

TestCase

run(TestResult) setUp() tearDown()

testName:String

runTest()

TestSuite

run(TestResult) addTest()

Command Pattern

Composite

Pattern

Adapter

Pattern

Template Method

Pattern

TestedUnit

*

Page 41: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 41!

Other JUnit features

•! Textual and GUI interface •! Displays status of tests

•! Displays stack trace when tests fail

•! Integrated with Maven and Continuous Integration •! http://maven.apache.org

•! Build and Release Management Tool

•! http://Maven.apache.org/continuum

•! Continous integration server for Java programs

•! All tests are run before release (regression tests)

•! Test results are advertised as a project report

•! Many specialized variants •! Unit testing of web applications

•! J2EE applications

Page 42: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 42!

Additional Readings

•! [Herman Bruyninckx] Embedded Control Systems Design, WikiBook, Learning from Failure: http://en.wikibooks.org/wiki/Embedded_Control_Systems_Design/Learning_from_failure

•! [Dijkstra 68] •! E. W. Dijkstra, "Letters to the editor: go to statement

considered harmful“, Communications of the ACM 11 (3): 147–148, March 1968

•! [Meszaros 2007]

•! G. Meszaros, xUnit Test Patterns, Refactoring Test Code, Addison Wesley, 2007

•! http://www.junit.org/

•! Resources for Test Driven Development

Page 43: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

Usi

ng U

ML

, P

atte

rns,

and J

ava!

Ob

ject

-Ori

ente

d S

oft

ware

En

gin

eeri

ng!

Testing Part 2: Integration & System Testing

Page 44: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 44!

Overview

•! Integration testing

•! Big bang

•! Bottom up

•! Top down

•! Sandwich

•! Continuous

•! System testing •! Functional

•! Performance

•! Acceptance testing

•! Summary

Page 45: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 45!

Integration Testing

•! The entire system is viewed as a collection of subsystems (sets of classes) determined during the system and object design

•! Goal: Test all interfaces between subsystems and the interaction of subsystems

•! The Integration testing strategy determines the order in which the subsystems are selected for testing and integration.

Page 46: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 46!

Why do we do integration testing?

•! Unit tests only test the unit in isolation

•! Many failures result from faults in the interaction of subsystems

•! Often many Off-the-shelf components are used that cannot be unit tested

•! Without integration testing the system test will be very time consuming

•! Failures that are not discovered in integration testing will be discovered after the system is deployed and can be very expensive.

Page 47: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 47!

Stubs and drivers

•! Driver: •! A component, that calls the TestedUnit

•! Controls the test cases

•! Stub: •! A component, the TestedUnit

depends on

•! Partial implementation

•! Returns fake values.

Driver!

Tested!

Unit!

Stub!

Page 48: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 48!

Example: A 3-Layer-Design

Layer I!

Layer II!

Layer III!

Spread"

SheetView!

A!

Calculator!

C!

BinaryFile "

Storage!

E!

XMLFile"

Storage!

F!

Currency"

DataBase!

G!

Currency!

Converter!

D!

Data"

Model!

B!

A!

C!

E! F! G!

D!B!

Spread"

SheetView!

BinaryFile "

Storage!

Entity!

Model!

A!

E! F!

Currency"

DataBase!

G!

Currency!

Converter!

D!B!

Calculator!

C!

XMLFile"

Storage!

(Spreadsheet)

Page 49: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 49!

A!

C!

E! F! G!

D!B!

Big-Bang Approach

Test A!

Test B!

Test G!

Test F!

Test E!

Test C!

Test D!Test !

A, B, C, D,!

E, F, G!

Page 50: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 50!

Bottom-up Testing Strategy

•! The subsystems in the lowest layer of the call hierarchy are tested individually

•! Then the next subsystems are tested that call the previously tested subsystems

•! This is repeated until all subsystems are included

•! Drivers are needed.

Page 51: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 51!

A!

C!

E! F! G!

D!B!

Bottom-up Integration A!

Test !

A, B, C, D,!

E, F, G!

E!Test E!

F!

Test F!

B!

Test B, E, F!

C!

Test C!

D!

Test D,G!

G!

Test G!

Page 52: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 52!

Pros and Cons of Bottom-Up Integration Testing

•! Con:

•! Tests the most important subsystem (user interface) last

•! Drivers needed

•! Pro

•! No stubs needed

•! Useful for integration testing of the following systems

•! Object-oriented systems

•! Real-time systems

•! Systems with strict performance requirements.

Page 53: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 53!

Top-down Testing Strategy

•! Test the top layer or the controlling subsystem first

•! Then combine all the subsystems that are called by the tested subsystems and test the resulting collection of subsystems

•! Do this until all subsystems are incorporated into the test

•! Stubs are needed to do the testing.

Page 54: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 54!

Top-down Integration

Test !

A, B, C, D,!

E, F, G!

All Layers!Layer I + II!

Test A, B, C, D!

Layer I!

Test A!

A!

E! F!

B! C! D!

G!

Page 55: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 55!

Pros and Cons of Top-down Integration Testing

Pro

•! Test cases can be defined in terms of the functionality of the system (functional requirements)

•! No drivers needed

Cons

•! Writing stubs is difficult: Stubs must allow all possible conditions to be tested.

•! Large number of stubs may be required, especially if the lowest level of the system contains many methods.

•! Some interfaces are not tested separately.

Page 56: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 56!

Sandwich Testing Strategy

•! Combines top-down strategy with bottom-up strategy

•! The system is viewed as having three layers •! A target layer in the middle

•! A layer above the target

•! A layer below the target

•! Testing converges at the target layer.

Page 57: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 57!

Sandwich Testing Strategy

Test !

A, B, C, D,!

E, F, G!

Test B, E, F!

Test D,G!

Test A!

Test E!

Test F!

Test G!

Test A,B,C, D!

A!

E! F!

B! C! D!

G!

Page 58: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 58!

Pros and Cons of Sandwich Testing

•! Top and Bottom Layer Tests can be done in parallel

•! Problem: Does not test the individual subsystems and their interfaces thoroughly before integration

•! Solution: Modified sandwich testing strategy

Page 59: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 59!

Modified Sandwich Testing Strategy

•! Test in parallel:

•! Middle layer with drivers and stubs

•! Top layer with stubs

•! Bottom layer with drivers

•! Test in parallel:

•! Top layer accessing middle layer (top layer replaces drivers)

•! Bottom accessed by middle layer (bottom layer replaces stubs).

Page 60: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 60!

Modified Sandwich Testing

Test F!

Test E!

Test B!

Test G!

Test D!

Test A!

Test C!

Test B, E, F!

Test D,G!

Test A,C!

Test !

A, B, C, D,!

E, F, G!

A!

E! F!

B! C! D!

G!

Page 61: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 61!

Continuous Testing

•! Continuous build:

•! Build from day one

•! Test from day one

•! Integrate from day one

!! System is always runnable

•! Requires integrated tool support: •! Continuous build server

•! Automated tests with high coverage

•! Tool supported refactoring

•! Software configuration management

•! Issue tracking.

Page 62: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 62!

Spread"

SheetView!

BinaryFile "

Storage!

Data"

Model!

Continuous Testing Strategy

Layer I!

Layer II!

Layer III!

A!

E! F!

Currency"

DataBase!

G!

Currency!

Converter!

D!B!

Calculator!

C!

XMLFile"

Storage!

Sheet View!+ Cells!

+ Addition!+ File Storage!

Page 63: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 63!

Steps in Integration Testing

.!

1. Based on the integration strategy, select a component to be tested. Unit test all the classes in the component.

2. Put selected component together; do any preliminary fix-up necessary to make the integration test operational (drivers, stubs)

3. Test functional requirements: Define test cases that exercise all uses cases with the selected component

4. Test subsystem decomposition: Define test cases that exercise all dependencies

5. Test non-functional requirements: Execute performance tests

6. Keep records of the test cases and testing activities.

7. Repeat steps 1 to 7 until the full system is tested.

The primary goal of integration testing is to identify failures with the (current) component configuration.

Page 64: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 64!

System Testing

•! Functional Testing •! Validates functional requirements

•! Performance Testing

•! Validates non-functional requirements

•! Acceptance Testing •! Validates clients expectations

Page 65: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 65!

. !

Functional Testing

Goal: Test functionality of system

•! Test cases are designed from the requirements analysis document (better: user manual) and centered around requirements and key functions (use cases)

•! The system is treated as black box

•! Unit test cases can be reused, but new test cases have to be developed as well.

Page 66: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 66!

Performance Testing

Goal: Try to violate non-functional requirements

•! Test how the system behaves when overloaded. •! Can bottlenecks be identified? (First candidates for

redesign in the next iteration)

•! Try unusual orders of execution •! Call a receive() before send()

•! Check the system’s response to large volumes of data

•! If the system is supposed to handle 1000 items, try it with 1001 items.

•! What is the amount of time spent in different use cases?

•! Are typical cases executed in a timely fashion?

Page 67: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 67!

Types of Performance Testing

•! Stress Testing

•! Stress limits of system

•! Volume testing

•! Test what happens if large amounts of data are handled

•! Configuration testing

•! Test the various software and hardware configurations

•! Compatibility test

•! Test backward compatibility with existing systems

•! Timing testing

•! Evaluate response times and time to perform a function

•! Security testing

•! Try to violate security requirements

•! Environmental test

•! Test tolerances for heat, humidity, motion

•! Quality testing

•! Test reliability, maintain- ability & availability

•! Recovery testing

•! Test system’s response to presence of errors or loss of data

•! Human factors testing

•! Test with end users.

Page 68: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 68!

Acceptance Testing

•! Goal: Demonstrate system is ready for operational use

•! Choice of tests is made by client

•! Many tests can be taken from integration testing

•! Acceptance test is performed by the client, not by the developer.

•! Alpha test:

•! Client uses the software at the developer’s environment.

•! Software used in a controlled setting, with the developer always ready to fix bugs.

•! Beta test:

•! Conducted at client’s environment (developer is not present)

•! Software gets a realistic workout in target environ- ment

Page 69: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 69!

Testing has many activities

Establish the test objectives!

Design the test cases!

Write the test cases!

Test the test cases!

Execute the tests!

Evaluate the test results!

Change the system!

Do regression testing!

Page 70: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 70!

Test Team

Test!

Analyst!

Team!User!

Programmer!

too familiar!with code!

Professional!

Tester!

Configuration !

Management!

Specialist!

System !

Designer!

Page 71: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 71!

The 4 Testing Steps

1. Select what has to be tested

•! Analysis: Completeness of requirements

•! Design: Cohesion

•! Implementation: Source code

2. Decide how the testing is done

•! Review or code inspection

•! Proofs (Design by Contract)

•! Black-box, white box,

•! Select integration testing strategy (big bang, bottom up, top down, sandwich)

3. Develop test cases

•! A test case is a set of test data or situations that will be used to exercise the unit (class, subsystem, system) being tested or about the attribute being measured

4. Create the test oracle

•! An oracle contains the predicted results for a set of test cases

•! The test oracle has to be written down before the actual testing takes place.

Page 72: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 72!

Guidance for Test Case Selection

•! Use analysis knowledge about functional requirements (black-box testing):

•! Use cases

•! Expected input data

•! Invalid input data

•! Use design knowledge about system structure, algorithms, data structures (white-box testing):

•! Control structures

•! Test branches, loops, ...

•! Data structures

•! Test records fields, arrays, ...

•! Use implementation knowledge about algorithms and datastructures:

•! Force a division by zero

•! If the upper bound of an array is 10, then use 11 as index.

Page 73: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 73!

Summary

•! Testing is still a black art, but many rules and heuristics are available

•! Testing consists of •! Unit testing

•! Integration testing

•! System testing

•! Acceptance testing

•! Design patterns can be used for integration testing

•! Testing has its own lifecycle

Page 74: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 74!

Additional Reading

•! Lecture: Prozesse und Methoden beim Testen von Software, Dr. Stefan Kriebel

•! Andreas Spillner, Tilo Linz, Basiswissen Softwaretest, dpunkt.verlag, 2004

•! JUnit Website www.junit.org/index.htm

•! J. Thomas, M. Young, K. Brown, A. Glover, Java Testing Patterns, Wiley, 2004

•! D. Saff and M. D. Ernst, An experimental evaluation of continuous testing during development Int. Symposium on Software Testing and Analysis, Boston July 12-14, 2004, pp. 76-85"

•! A controlled experiment shows that developers using continuous testing were three times more likely to complete the task before the deadline than those without.

Page 75: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

Usi

ng U

ML

, P

atte

rns,

and J

ava!

Ob

ject

-Ori

ente

d S

oft

ware

En

gin

eeri

ng!

Testing, Part 3 Model-based Testing

Page 76: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 76!

Outline of the Lectures on Testing

"!Terminology

"!Types of errors

"!Approaches for dealing with errors

"!Testing activities

"!Unit testing

"!Integration testing

"!System testing

•! Model-based Testing

Page 77: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 77!

Generating Tests from a System Model

•! There are many different ways to "derive" tests from a system model"

•! Manual generation"

•! Automatic generation"

•! Because testing is usually experimental and based on heuristics, there is no one best way to do this. "

•! It is common to consolidate all test related decisions into a package that is often known as "test requirements” or "test package”."

•! This test package can contain e.g. information about the part of the system model that should be the focus for testing, or about the conditions where it is correct to stop testing (test stopping criteria)."

Page 78: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 78!

Model Driven Architecture (MDA)

•! Recall: MDA focuses on forward engineering, i.e. producing code from a system model

•! Note: The term “architecture” in MDA does not mean the architecture of the system being modeled, but refers the architecture of the various standards and models that serve as the technology basis for MDA

•! UML: Unified Modeling Language

•! MOF: Meta-Object Facility

•! XMI: Metadata Interchange

•! EDOC: Enterprise Distributed Object Computing

•! SPEM: Software Process Engineering Metamodel

•! CWM: Common Warehouse Metamodel

An important objective of MDA is executable UML.

Page 79: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 79!

Executable UML

•! Executable UML means that given a UML system model the following steps can be performed automatically:

•! Code generation

•! Simulation Validation

•! Test generation (“Model based Testing”)

Page 80: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 80!

Model Based Testing

•! Definition Model Based Testing: All testing is based on a system model which is used to (manually) generate test cases and oracles

•! Advantage:

•! Increased effectiveness of testing

•! Decreased costs

•! Reuse of artifacts such as analysis and design models

•! Requirement:

•! The system model is formalized (specification model) and described in UML 2.0

Page 81: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 81!

Model Driven Testing

•! Model Driven Testing is a special case of model based testing:

•! All the test cases are automatically generated from the system model

•! Advantage:

•! Automatic generation of tests suites

•! Better test thoroughness

•! Reduces the cost of test execution.

Page 82: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 82!

Observations on Model Based Testing

•! A system model is an abstract representation of the system’s desired behavior

•! The test cases derived from this model are functional tests on the same level of abstraction as the model

•! These test cases are known as the abstract test suite

•! Because test suites are derived from models - not from source code - model-based testing is a form of black-box testing"

•! The abstract test suite cannot be executed

•! An executable test suite must be derived from the abstract test suite which communicates with the system under test (SUT)

•! This is done by mapping the abstract test suite to concrete test cases suitable for execution.

•! Mapping is not part of this lecture.

Page 83: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 83!

U2TP

•! UML-2 Testing Profile (U2TP):

•! A UML extension that makes UML applicable to software testing.

•! U2TP allows the collection of all information required for the model-based testing process

•! Deriving a test model from the system model.

•! Test Model: •! A set of test cases derived from the system model.

Page 84: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 84!

General Definitions used in U2TP

•! Test: An attempt to create a difference between the observed behavior and the specified behavior of a system a planned way

•! Test data: The structures and meaning of values to be processed in a test

•! System under test (SUT)

•! A system, subsystem or component

•! The SUT is treated as black-box. This means:

•! The SUT can be accessed only via a public interface

•! No information on the internal structure of the SUT is available for use in the specification of test cases.

•! Test case: Describes a specified behavior, that is, a behavior specified in the system model (normative behavior) of the SUT.

Page 85: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 85!

Test Objective

•! Test objective

•! Find any difference between the observed behavior and the specified behavior, that is, the behavior specified in the system model.

•! UML Example

•! The test objective in the diagram below specifies that the objective of the ValidWithdrawal test case is to validate the WithdrawMoney use case.

<<testcase>>!

ValidWithdrawal!<<usecase>>!

WithdrawMoney!<<objective>>!

Page 86: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 86!

Test Architecture Definitions

•! Test component: An object within a test system

•! Test architecture: The test components and their relationships in a test system

•! Testing configuration: The combination of a test system with a SUT

•! Test control: A ordering of the execution of the tests performed on the SUT

•! Test context: A description of

•! A set of test cases

•! A test configuration

•! A test control.

Page 87: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 87!

Example: Test System for a Vending Machine •! Vending Machine with 2 use cases:

•! The user inserts money

•! A bottle is returned, when the inserted money equals the price of the bottle.

Page 88: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 88!

Model-based Testing of MoneyBox

The test system MoneyUnitTest is developed with the purpose of unit testing the Moneybox, which is part of the system under test

MoneyBox must be imported by MoneyUnitTest to be accessible.

Test System! SUT!

Page 89: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 89!

UML Model of the MoneyBox

Now we need to test that the machine correctly counts the money

inserted by the user into the Moneybox -> MoneyUnitTest

Page 90: Software Engineering II e Engineering Testing ·  · 2015-03-25Using UML, Patterns, and Java e Engineering!! Software Engineering II Testing 5 May 2009 Bernd Bruegge Applied Software

2009 Bernd Bruegge! ! ! ! Software Engineering II, Lecture 3: Testing ! ! 90!

Model-based Testing of MoneyBox

MoneyBox is the system under test

MoneyUnitTest is the test system

MoneyTest is the test context

addMoneyTest is the test case

Test Context!

Test

Case!

Test System!SUT!