Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box...

43
Software Engineering I (02161) Week 5 Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2017

Transcript of Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box...

Page 1: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Software Engineering I (02161)Week 5

Assoc. Prof. Hubert Baumeister

DTU ComputeTechnical University of Denmark

Spring 2017

Page 2: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Contents

Systematic tests

Code coverage

Page 3: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Recap

I Earlier:I Requirements (Use cases, User stories), Domain model,

Acceptance TestsI Last week:

I Test-Driven DevelopmentI Refactoring

I This weekI Systematic development of testsI Code coverage

Page 4: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Systematic testing

I Tests are expensiveI Impractical to test all input valuesI Not too few because one could miss some defects→ Partition based testsI Paper by Peter Sestoft (availalbe from the course home

page)

Hubert
Page 5: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Partition based tests

I Tests test expectedbehaviour

I SUT (System under test)

Hubert
Page 6: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Partition based tests: Black box

I Expected behaviour:isEven(n)

I SUT implementation ofisEven(n)

Hubert
Page 7: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Partition based tests: White Box

I Expected behaviour: isEven(n)I SUT implementation of isEven(n)

public boolean isEven(int n) {if (n % 2 == 0) {

return true;} else {

return false;}

}

Hubert
Page 8: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Partition based tests: White Box

I Expected behaviour: isEven(n)I SUT implementation of isEven(n)

public boolean isEven(int n) {if (n == 101) return true;if (n % 2 == 0) {

return true;} else {

return false;}

}

Hubert
Page 9: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

How to find the right partitions?

1. white box test / structural test2. black box test / functional test

Page 10: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

White Box testsI Find the minimum and the maximum of a list of integers

I Path from method entry to exit → partitionI Input property = conjunction of all conditions on path

Hubert
Hubert
Hubert
Page 11: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Example of a white box test (II): Test cases

Hubert
Hubert
Page 12: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

JUnit Tests

public class WhiteBoxTest {MinMax sut = new MinMax();

@Test(expected = Error.class)public void testInputDataSetA() {int[] ar = {};sut.minmax(ar);

}

@Testpublic void testInputDataSetB() {int[] ar = {17};sut.minmax(ar);assertEquals(17,sut.getMin());assertEquals(17,sut.getMax());

}

@Testpublic void testInputDataSetC() {int[] ar = {27, 29};sut.minmax(ar);assertEquals(27,sut.getMin());assertEquals(29,sut.getMax());

}

Hubert
Hubert
Hubert
Hubert
Page 13: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

JUnit Tests (cont.)

@Testpublic void testInputDataSetD() {int[] ar = {39, 37};sut.minmax(ar);assertEquals(37,sut.getMin());assertEquals(39,sut.getMax());

}

@Testpublic void testInputDataSetE() {int[] ar = {49, 47, 48};sut.minmax(ar);assertEquals(47,sut.getMin());assertEquals(49,sut.getMax());

}

@Testpublic void testInputDataSetF() {int[] ar = {49, 52, 50};sut.minmax(ar);assertEquals(47,sut.getMin());assertEquals(49,sut.getMax());

}

}

Hubert
Hubert
Hubert
Page 14: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Example of a black box test (I): min, max computation

Problem: Find the minimum and the maximum of a list ofintegers

I What are the partitions?

Hubert
Hubert
Hubert
Hubert
Hubert
Page 15: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Example of a black box test (I): min, max computation

Problem: Find the minimum and the maximum of a list ofintegers

I Definition of the inputpartitions

I Definition of the test valuesand expected results

Hubert
Hubert
Hubert
Hubert
Page 16: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Example of a black box test (I): min, max computation

Problem: Find the minimum and the maximum of a list ofintegers

I Definition of the inputpartitions

I Definition of the test valuesand expected results

Page 17: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

White box vs. Black box testing

What do you think and why?1. If there are white box tests, no black box tests are needed2. if there are black box tests, no white box tests are needed3. Both, black box and white box tests are needed all the time

Page 18: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

White box vs. Black box testing

I White box testI finds defects in the implementationI can’t find problems with the functionality

I Ex.: Functionality: For numbers n below 100 returneven(n), for numbers 100 or above return odd(n).public boolean isEvenBelow100andOddAbove(int n) {

if (n % 2 == 0) {return true;

} else {return false;

}}

Page 19: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

White box vs. Black box testing

I White box testI finds defects in the implementationI can’t find problems with the functionality

I Ex.: Functionality: For numbers n below 100 returneven(n), for numbers 100 or above return odd(n).public boolean isEvenBelow100andOddAbove(int n) {if (n % 2 == 0) {return true;

} else {return false;

}}

Hubert
Page 20: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

White box vs. Black box testing

I Black box testI finds problems with the functionalityI can’t find defects in the implementation

I Ex.: Functionality: For a number n return even(n)public boolean isEven(int n) {if (n == 100) {

return false;}if (n % 2 == 0) {

return true;}return false;

}

Page 21: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

White box vs. Black box testing

I Black box testI finds problems with the functionalityI can’t find defects in the implementation

I Ex.: Functionality: For a number n return even(n)public boolean isEven(int n) {if (n == 100) {

return false;}if (n % 2 == 0) {return true;

}return false;

}

Hubert
Page 22: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

TDD vs. White box and Black box testing

I TDD: Black box + white box testingI TDD starts with tests for the functionality (black box)I Any production code that you want to write needs a failing

test (white box)→ Quality critera: 100% code coverage of tests (white box

test critera). Beware that all required functionality is testedtoo.

Hubert
Hubert
Page 23: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Summary

Test plan: Two tablesI Table for the input partitionsI Table for the test data (input / expected output)

Page 24: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Example Vending Machine

I ActionsI Input coinsI Press button for

bananas or applesI Press cancel

I DisplaysI current amount of

money inputI Effects

I Return moneyI Dispense banana or

apple

Page 25: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Use Case: Buy Fruit

name: Buy fruitdescription: Entering coins and buying a fruitactor: usermain scenario:

1. Input coins until the price for the fruit to be selected isreached

2. Select a fruit3. Vending machine dispenses fruit

alternative scenarios:a1. User inputs more coins than necessarya2. select a fruita3. Vending machine dispenses fruita4. Vending machine returns excessive coins

Hubert
Hubert
Page 26: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Use Case: Buy Fruit (cont.)

alternative scenarios (cont.)b1 User inputs less coins than necessaryb2 user selects a fruitb3 No fruit is dispensedb4 User adds the missing coinsb5 Fruit is dispensedc1 User selects fruitc2 User adds sufficient or more coinsc3 vending machine dispenses fruit and rest moneyd1 user enters coinsd2 user selects canceld3 money gets returned

Page 27: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Use Case: Buy Fruit (cont.)

alternative scenarios (cont.)e1 user enters correct coinse2 user selects fruit but vending machine does not have the

fruit anymoree3 nothing happense4 user selects cancele5 the money gets returnedf1 user enters correct coinsf2 user selects a fruit but vending machine does not have the

fruit anymoref3 user selects another fruitf4 if money is correct fruit with rest money is dispensed; if

money is not sufficient, the user can add more coins

Page 28: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Functional Test: for Buy Fruit Use Case: Input DataSets

Input data set Input propertyA Exact coins; enough fruits; first coins, then fruit selectionB Exact coins; enough fruits; first fruit selection, then coinsC Exact coins; not enough fruits; first coins, then fruit selection, then cancelD Exact coins; not enough fruits; first fruit selection, then coins, then cancelE More coins; enough fruits; first coins, then fruit selectionF More coins; enough fruits; first fruit selection, then coinsG More coins; not enough fruits; first coins, then fruit selection, then cancelH More coins; not enough fruits; first fruit selection, then coins, then cancelI Less coins; enough fruits; first coins, then fruit selectionJ Less coins; enough fruits; first fruit selection, then coinsK Less coins; not enough fruits; first coins, then fruit selection, then cancelL Less coins; not enough fruits; first fruit selection, then coins, then cancel

Page 29: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Functional Test for Buy Fruit Use Case: Test Cases

Input data set Contents Expected OutputA 1,2; apple apple dispensedB Apple; 1,2 apple dispensedC 1,2; apple; cancel no fruit dispensed; returned DKK 3D Apple; 1,2; cancel no fruit dispensed; returned DKK 3E 5, apple apple dispensed; returned DKK 2F Apple; 5 apple dispensed; returned DKK 2G 5, apple; cancel no fruit dispensed; returned DKK 5H Apple; 5; cancel no fruit dispensed; returned DKK 5I 5; banana no fruit dispensed; current money shows 5J Banana; 5,1 no fruit dispensed; current money shows 6K 5,1; banana; cancel no fruit dispensed; returned DKK 6L Banana; 5,1;cancel no fruit dispensed; returned DKK 6

Hubert
Hubert
Page 30: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Manual vs Automated Tests

I Manual test-plansI Table of input / expected outputI Run the applicationI Check for desired outcome

I Automatic testsa. Test the GUI directlyb. Testing ”under the GUI”

→ Layered architecture

Page 31: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Application Layer

«enumeration»Fruit

APPLEBANANA

VendingMachinedispensedItem: FruitcurrentMoney: inttotalMoney: intrestMoney: intinput(money: int)select(f: fruit)cancel()

*

Page 32: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Functional Test for Buy Fruit Use Case: JUnit Tests

public void testInputDataSetA() {VendingMachine m = new VendingMachine(10, 10);m.input(1);m.input(2);assertEquals(3, m.getCurrentMoney());m.selectFruit(Fruit.APPLE);assertEquals(Fruit.APPLE, m.getDispensedItem());

}

public void testInputDataSetB() {VendingMachine m = new VendingMachine(10, 10);m.selectFruit(Fruit.APPLE);m.input(1);m.input(2);assertEquals(0, m.getCurrentMoney());assertEquals(Fruit.APPLE, m.getDispensedItem());

}

Hubert
Hubert
Hubert
Page 33: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Functional Test: JUnit Tests (cont.)

public void testInputDataSetC() {VendingMachine m = new VendingMachine(0, 0);m.input(1);m.input(2);assertEquals(3, m.getCurrentMoney());m.selectFruit(Fruit.APPLE);assertEquals(null, m.getDispensedItem());m.cancel();assertEquals(null, m.getDispensedItem());assertEquals(3, m.getRest());

}

public void testInputDataSetD() {VendingMachine m = new VendingMachine(0, 0);m.selectFruit(Fruit.APPLE);m.input(1);m.input(2);assertEquals(3, m.getCurrentMoney());m.cancel();assertEquals(null, m.getDispensedItem());assertEquals(3, m.getRest());

}

...

Hubert
Hubert
Page 34: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Contents

Systematic tests

Code coverage

Page 35: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Code coverage

I How good are the tests?→ When the tests have covered all the codeI Code coverage

I statement coverageI decision coverageI condition coverageI path coverageI . . .

Hubert
Hubert
Hubert
Hubert
Hubert
Hubert
Hubert
Page 36: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Code coverage: statement, decision, condition

int foo (int x, int y){

int z = 0;if ((x>0) && (y>0)) {

z = x;}return z;

}

Hubert
Hubert
Hubert
Page 37: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Code coverage: path

int foo (boolean b1, boolean b2){

if (b1) {s1;

} else {s2;

}if (b2) {

s3;} else {

s4;}

}

Hubert
Page 38: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Coverage Tool

I Statement, decision, andcondition coverage

I EclEmma(http://eclemma.org)

I Goal: 100% test coverageI TDD helps to achieve the

goal

Domain Layere.g. User, Book, ...

Persistency Layer

User

Application Layere.g. LibraryApp

Thin Presentation Layer

Hubert
Page 39: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Coverage with EclEmma

Hubert
Page 40: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Coverage with EclEmma

Hubert
Page 41: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Coverage with EclEmma

Hubert
Page 42: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Next Week

I From user requirements to designI Documenting design

I Class diagramsI Sequence diagrams

I Exam project introduction

Page 43: Software Engineering I (02161)TDD vs. White box and Black box testing I TDD: Black box + white box testing I TDD starts with tests for the functionality (black box) I Any production

Exam project

I Exam projectI Week 06: Project introduction and forming of project groups

(2–4); participation mandatoryI Week 08: Submission of use cases and designI Easter Monday: Submission of systematic tests, design

contracts, etc, and first draft version of the implementationI Week 13: Demonstration of the projects (each project 10

min.) This is not an oral examination!I Group forming

I Group forming: mandantory participation in the lecturenext week

I Either you are personally present or someone can speakfor you

I If not, then there is no guarantee for participation in theexam project

Hubert
Hubert