Software Testing

416 downloads 201 Views 152KB Size Report
Goal of testing: given some code, uncover as many errors ... testing, become paranoid and malicious. ▫ Surprisingly .... Good example in Pressman, Sect. 17.4. 3 ...
Motivation ƒ

People are not perfect ƒ

ƒ

Software Testing

ƒ

Goal of testing: given some code, uncover as many errors are possible Important and expensive activity ƒ ƒ

CSE 757 - Software Testing

1

Design and coding are creative

ƒ

Testing is destructive ƒ

ƒ

ƒ

ƒ

The primary goal is to “break” the software ƒ

Need “split personality”: when you start testing, become paranoid and malicious Surprisingly hard to do: people don’t like finding out that they made mistakes CSE 757 - Software Testing

ƒ

Testing is a process of executing software with the intent of finding errors Good testing has a high probability of finding as-yet-undiscovered errors Successful testing discovers unknown errors ƒ

3

ƒ

ƒ

ƒ

ƒ

Inputs + pre-test state of the software Expected results (outputs and state)

ƒ

Black-box testing: ignores the internal logic of the software ƒ

In this course: small sample of approaches for testing White-box testing ƒ ƒ

Given this input, was the output correct?

ƒ

White-box testing: uses knowledge of the internal structure of the software ƒ

4

Testing Approaches

Test case: specifies ƒ

If did not find any errors, need to ask whether our testing approach is good CSE 757 - Software Testing

Basic Definitions ƒ

2

Testing Objectives

Very often the same person does both coding and testing ƒ

Not unusual to spend 30-40% of total project effort on testing For critical systems (e.g. flight control): cost can be several times the cost of all other activities combined CSE 757 - Software Testing

A Way of Thinking ƒ

We make errors in design and code

Control-flow-based testing Data-flow-based testing

Black-box testing ƒ

Equivalence partitioning

e.g. write tests to “cover” internal paths CSE 757 - Software Testing

5

CSE 757 - Software Testing

6

1

Control-flow-based Testing ƒ ƒ

Traditional form of white-box testing

s:=0; d:=0; while (x 5) bonus := 1; else bonus := 0.7; break; case MANAGER: bonus := 1.5; if (retiring_soon) bonus := 1.2 * bonus break; case ... endswitch CSE 757 - Software Testing

CSE 757 - Software Testing

IF-THEN, IF-THEN-ELSE, SWITCH

Three kinds of nodes:

ƒ

3

8 7

Elements of a Control Flow Graph

ƒ

2

Nodes, edges, paths CSE 757 - Software Testing

ƒ

1

Note: other loops (e.g. FOR, DO-WHILE, …) are mapped similarly Mini-assignment: figure out how this is done 11

CSE 757 - Software Testing

12

2

Statement Coverage ƒ

ƒ

Given the control flow graph, define a “coverage target” and write test cases to achieve it Traditional target: statement coverage ƒ

ƒ

Example

ƒ

Often this is the “low-probability” code

CSE 757 - Software Testing

ƒ

ƒ

Test cases that cover all nodes

Code that has not been executed during testing is more likely to contain errors ƒ

ƒ

Suppose that we write and execute two test cases

1

Test case #1: follows path 1-2-exit

2

Test case #2: 1-2-3-4-57-8-2-3-4-5-7-8-2-exit (loop twice, and both times take the true branch)

3

Problem: node 6 is never executed, so we don’t have 100% statement coverage

13

ƒ

ƒ

ƒ

ƒ

True and false branches of each IF The two branches corresponding to the condition of a loop All alternatives in a SWITCH

ƒ

CSE 757 - Software Testing

ƒ

ƒ

15

CSE 757 - Software Testing

ƒ

1

Path 1-2-exit Path 1-2-3-4-5-7-8-2-34-5-7-8-2-exit

16

Branch coverage: a necessary minimum ƒ

2 3

ƒ

T 4 F 5

6

ƒ

7 ƒ 17

Pick a set of start-to-end paths that cover all branches, and write tests cases to execute these paths

Basic strategy ƒ

8 CSE 757 - Software Testing

Plus, it subsumes statement coverage

Achieving Branch Coverage

Same example as before: two test cases

Problem: the “false” branch of 4 is never taken - don’t have 100% branch coverage

By executing only with c=true, we will achieve statement coverage, but not branch coverage

Motivation: experience shows that many errors occur in “decision making”

Example

ƒ

14

Example: if (c) then s; ƒ

In modern languages, branch coverage implies statement coverage

ƒ

8

Statement coverage does not imply branch coverage

ƒ

ƒ

7

Branch Coverage

Target: write test cases that cover all branches of predicate nodes ƒ

6

CSE 757 - Software Testing

Branch Coverage ƒ

T 4 F 5

Add a new path that covers at least one edge that is not covered by the current paths Sometimes the set of paths chosen with this strategy is called the “basis set”

Good example in Pressman, Sect. 17.4.3 CSE 757 - Software Testing

18

3

Data-flow-based Testing ƒ

ƒ

ƒ

assume y is already initialized

Test connections between variable definitions (“write”) and variable uses (“read”) Variation of the control flow graph ƒ

ƒ

Example

A node represents a single statement, not a single-entry-single-exit chain of statements

Set DEF(n) contains variables that are defined at node n (i.e., they are written) Set USE(n): variables that are read CSE 757 - Software Testing

1 s:=0; 2 x:=0; 3 while (x