Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven...

35
Test Driven Development Seminar on Languages for Scientific Computing Albert J. Helligrath December 13, 2012

Transcript of Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven...

Page 1: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Test Driven DevelopmentSeminar on Languages for Scientific Computing

Albert J. Helligrath

December 13, 2012

Page 2: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

1 Motivation

2 Test Driven Development

3 Analysis

4 Conclusion

TDD 2 A. Helligrath

Page 3: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Costs of Errors

Maiden Flight of Ariane 5 (1996)

unhandled exception duringfloat-int conversion (overflow)

error message misinterpreted ascontrol commands

purposeful blowing-up

costs: 370 Million $

TDD 3 A. Helligrath

Page 4: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Software Quality Assurance

increase quality,reduce errors

avoid errors (constructive)orfind and eliminate errors

verification to expensive,practical solution:minimum effort,maximum damage reduction

Quality

The degree to which a system,component, or process meetsspecified requirements.ISO/IEC/IEEE 24765:2010

Error

The difference between acomputed, observed, or measuredvalue or condition and the true,specified, or theoretically correctvalue or condition.ISO/IEC/IEEE 24765:2010

TDD 4 A. Helligrath

Page 5: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Software Quality Assurance

increase quality,reduce errors

avoid errors (constructive)orfind and eliminate errors

verification to expensive,practical solution:minimum effort,maximum damage reduction

Quality

The degree to which a system,component, or process meetsspecified requirements.ISO/IEC/IEEE 24765:2010

Error

The difference between acomputed, observed, or measuredvalue or condition and the true,specified, or theoretically correctvalue or condition.ISO/IEC/IEEE 24765:2010

TDD 4 A. Helligrath

Page 6: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Test

goal: find an error

effective testing:test frameworktest process

but: tests skipped first,programmers don’t like testing

Testing

The process of operating a system orcomponent under specified conditions,observing or recording the results, andmaking an evaluation of some aspectof the system or component.IEEE-Definition

TDD 5 A. Helligrath

Page 7: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Test

goal: find an error

effective testing:test frameworktest process

but: tests skipped first,programmers don’t like testing

Testing

The process of operating a system orcomponent under specified conditions,observing or recording the results, andmaking an evaluation of some aspectof the system or component.IEEE-Definition

TDD 5 A. Helligrath

Page 8: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

1 Motivation

2 Test Driven Development

3 Analysis

4 Conclusion

TDD 6 A. Helligrath

Page 9: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Test Driven Development

goal: assure testing, avoid defects earlyapproach: ”test first”

TDD introduced as extreme programming practice

iterative steps,short cycle

TDD 7 A. Helligrath

Page 10: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Test Driven Development

goal: assure testing, avoid defects earlyapproach: ”test first”

TDD introduced as extreme programming practice

iterative steps,short cycle

TDD 7 A. Helligrath

Page 11: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Steps of TDD

1 Identify missing functionalityrequires analysis of specificationsplits the problem into smaller pieces

2 Write a failing unit testguarantees code to be testedgood unit test:fast, runs in isolation,easy to read, uses real datafailing tests the test

3 Write just enough code to pass itfake implementation, inelegant wayonly pass the test, refactor later

4 Refactor and Test againcode smell?make the bar green

TDD 8 A. Helligrath

Page 12: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Steps of TDD

1 Identify missing functionalityrequires analysis of specificationsplits the problem into smaller pieces

2 Write a failing unit testguarantees code to be testedgood unit test:fast, runs in isolation,easy to read, uses real datafailing tests the test

3 Write just enough code to pass itfake implementation, inelegant wayonly pass the test, refactor later

4 Refactor and Test againcode smell?make the bar green

TDD 8 A. Helligrath

Page 13: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Steps of TDD

1 Identify missing functionalityrequires analysis of specificationsplits the problem into smaller pieces

2 Write a failing unit testguarantees code to be testedgood unit test:fast, runs in isolation,easy to read, uses real datafailing tests the test

3 Write just enough code to pass itfake implementation, inelegant wayonly pass the test, refactor later

4 Refactor and Test againcode smell?make the bar green

TDD 8 A. Helligrath

Page 14: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Steps of TDD

1 Identify missing functionalityrequires analysis of specificationsplits the problem into smaller pieces

2 Write a failing unit testguarantees code to be testedgood unit test:fast, runs in isolation,easy to read, uses real datafailing tests the test

3 Write just enough code to pass itfake implementation, inelegant wayonly pass the test, refactor later

4 Refactor and Test againcode smell?make the bar green

TDD 8 A. Helligrath

Page 15: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Steps of TDD

1 Identify missing functionalityrequires analysis of specificationsplits the problem into smaller pieces

2 Write a failing unit testguarantees code to be testedgood unit test:fast, runs in isolation,easy to read, uses real datafailing tests the test

3 Write just enough code to pass itfake implementation, inelegant wayonly pass the test, refactor later

4 Refactor and Test againcode smell?make the bar green

TDD 8 A. Helligrath

Page 16: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Coding Example

Steps of TDD

1 Identify missing functionality

2 Write a failing unit test

3 Write just enough code to pass it

4 Refactor andTest again

Settlers of Aachen

The class SoA simulates aboard game. Each playercan register his name.

Task

Implement the functionstart().It returns a String namingthe player and telling him,that his turn has begun.

TDD 9 A. Helligrath

Page 17: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Code Smells

duplicated code

long methods, classes

complex branches

obscure code

design patternapplicable?

public class q{

static String s="public class q{1}static String s=

{3}{0}{3}; public static void main(String[] a){1}

System.out.println(java.text.MessageFormat.format(

s,s,’’{1}’’,’’{2}’’,’’{3}’’));{2}{2}";

public static void main(String[] a){

System.out.println(java.text.MessageFormat.format(

s,s,’{’,’}’,’"’));

}

}

TDD 10 A. Helligrath

Page 18: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Factory

problem of testing:initialize too many classes

solution: introduce factory

public class SoA_Factory{

SoA getNewSoA() {

SoA soa = new SoA();

Player p1 = new Player();

soa.add_player(p1.name);

...

ResourceFields rf = new ResourceFields();

rf.add(new Wool());

..

soa.add_fields(rf);

return soa;

}

}

TDD 11 A. Helligrath

Page 19: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Factory

problem of testing:initialize too many classes

solution: introduce factory

public class SoA_Factory{

SoA getNewSoA() {

SoA soa = new SoA();

Player p1 = new Player();

soa.add_player(p1.name);

...

ResourceFields rf = new ResourceFields();

rf.add(new Wool());

..

soa.add_fields(rf);

return soa;

}

}

TDD 11 A. Helligrath

Page 20: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Strategy Pattern

test may interact with complexobjects

non-deterministic behavior(camera data, dice)

slow access (database)

critical execution(paypal.sendAllMyMoney(),nuclearMissile.launch())

solution: Strategy Pattern

extract interface

real object implements theinterface

fake object implements theinterface

public interface IDice {

int roll();

}

public class Dice implements IDice {

public int roll() {

return new Random().nextInt(6) + 1;

}

}

public class CheatDice implements IDice {

public int roll() {

return 6; // chosen by fair dice roll.

// guaranteed to be random.

}

}

TDD 12 A. Helligrath

Page 21: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Strategy Pattern

test may interact with complexobjects

non-deterministic behavior(camera data, dice)

slow access (database)

critical execution(paypal.sendAllMyMoney(),nuclearMissile.launch())

solution: Strategy Pattern

extract interface

real object implements theinterface

fake object implements theinterface

public interface IDice {

int roll();

}

public class Dice implements IDice {

public int roll() {

return new Random().nextInt(6) + 1;

}

}

public class CheatDice implements IDice {

public int roll() {

return 6; // chosen by fair dice roll.

// guaranteed to be random.

}

}

TDD 12 A. Helligrath

Page 22: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Strategy Pattern

test may interact with complexobjects

non-deterministic behavior(camera data, dice)

slow access (database)

critical execution(paypal.sendAllMyMoney(),nuclearMissile.launch())

solution: Strategy Pattern

extract interface

real object implements theinterface

fake object implements theinterface

public interface IDice {

int roll();

}

public class Dice implements IDice {

public int roll() {

return new Random().nextInt(6) + 1;

}

}

public class CheatDice implements IDice {

public int roll() {

return 6; // chosen by fair dice roll.

// guaranteed to be random.

}

}

TDD 12 A. Helligrath

Page 23: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Mock Object

Problem: too many different fake implementations

solution: simulate Interface by Mock Objectpublic void test_rollForResources() {

IDice mockDice = EasyMock.createMock(IDice.class);

EasyMock.expect(mockDice.roll()).andReturn(4);

EasyMock.expect(mockDice.roll()).andReturn(3);

EasyMock.replay(mockDice);

SoA soa = new SoA();

soa.dice = mockDice;

assert (soa.rollForResources() == 7); // this function sums two dice rolls

}

TDD 13 A. Helligrath

Page 24: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Mock Object

Problem: too many different fake implementations

solution: simulate Interface by Mock Objectpublic void test_rollForResources() {

IDice mockDice = EasyMock.createMock(IDice.class);

EasyMock.expect(mockDice.roll()).andReturn(4);

EasyMock.expect(mockDice.roll()).andReturn(3);

EasyMock.replay(mockDice);

SoA soa = new SoA();

soa.dice = mockDice;

assert (soa.rollForResources() == 7); // this function sums two dice rolls

}

TDD 13 A. Helligrath

Page 25: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Steps of TDD - Summary

TDD 14 A. Helligrath

Page 26: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

1 Motivation

2 Test Driven Development

3 Analysis

4 Conclusion

TDD 15 A. Helligrath

Page 27: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Better Code Testing

high coverage: cover every line of code

defects early found (every effort worth it)

more confidence, buttests cannot prove the absence of errors,false sense of security

tester = programmer, same blind spotindependent testing still necessary

TDD 16 A. Helligrath

Page 28: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Requirements and Documentation

tests define requirements:early requirement analysis(increases confidence)concern early about interface

tests = documentationTDD is specification, not validation

more tests than code

TDD 17 A. Helligrath

Page 29: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Clean Code

test guides the implementation

modularized, highly cohesive, loosely coupled code

write functions only if needed (YAGNI)

encourages simple designs, inspires confidence

TDD 18 A. Helligrath

Page 30: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Scalability

test-runs take time

split into test-suite hierarchy

maintenance overhead, more code

problem if many tests fail simultaneouslye.g. due to elementary design change

TDD 19 A. Helligrath

Page 31: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

1 Motivation

2 Test Driven Development

3 Analysis

4 Conclusion

TDD 20 A. Helligrath

Page 32: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Studies

how to measure software quality?defect density?

TDD 21 A. Helligrath

Page 33: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Literature

David S. Janzen and Hossein Saiedian.Test-Driven Development: Concepts, Taxonomy,andFuture Direction.http://works.bepress.com/djanzen/6

Kent Beck.Test-Driven Development by Example(Addison-Wesley Signature)

Dan Pilone, Russ Miles.Head First Software Development

http://www.agiledata.org/essays/tdd.html

Prof. Lichter.lecture Software Quality Assurance,2012 RWTH

Mark Dowson.The Ariane 5 Software Failure.SIGSOFT Software Engineering Notes,March 1997

TDD 22 A. Helligrath

Page 34: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Images

Ariane 5

www.capcomespace.net/dossiers/espace_europeen/ariane/ariane5/AR501/V88_AR501.htm

Money, Time, Quality

www.brucesallan.com/wp-content/uploads/2012/05/ROIdice1.jpg

Kiss

dryinkdesigns.files.wordpress.com/2012/06/062112_kiss.png

Settlers of Catan

www.catan.com

SmellfishProf. Lichter. lecture Software Quality Assurance, 2012 RWTH

Drawing Hands,by M. C. Escher, 1948, Lithograph

Factory

www.clipartheaven.com

Cheat Dice

http://www.slidetoplay.com/game/cheat-dice

Documentation

www.picospin.com/wp-content/uploads/2011/12/documentation1.jpg

TDD 23 A. Helligrath

Page 35: Test Driven Developmenthpac.rwth-aachen.de/teaching/sem-lsc-12/TDD.pdfMotivation Test Driven Development Analysis Conclusion Test Driven Development goal: assure testing, avoid defects

Motivation Test Driven Development Analysis Conclusion

Summary

TDD 24 A. Helligrath