lessons learned in programmer testinggotocon.com/dl/jaoo-sydney-2009/slides/james... · lesson #1...

44
1 Lessons Learned in Programmer Testing James Newkirk, xUnit.net and NUnit V2 Lead CodePlex Product Unit Manager

Upload: others

Post on 20-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

1

Lessons Learned in Programmer Testing

James Newkirk, xUnit.net and NUnit V2 LeadCodePlex Product Unit Manager

Page 2: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

The “T” in

Test-Driven Development

is Wrong

Lesson #1

Page 3: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

3

Words Matter

Unit Test

• Intent and purpose are wrong

• Boundary Conditions

• Pre/Post Conditions

• White Box vs. Black Box

Programmer Test

• Nobody knows what it means

• Picked the wrong word to change

Exampler

• A pattern

• I had to look it up in the dictionary

Design By Example

• It might be too late to change

Page 4: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

4

Lessons Learned in Design By Example

James Newkirk, xUnit.net and NUnit V2 LeadCodePlex Product Unit Manager

Page 5: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Why Design with Examples

“There is no such thing as done. Much more investment will be spent modifying programs them developing them initially” [Beck]

“Programs are read more often than they are written” [Beck]

“Readers need to understand programs in detail and concept” [Beck]

Page 6: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Total Development Cost

Extend/MaintainDevelop

Page 7: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Understand

Change

Test

Deploy

Extend/Maintain Cost [Beck]

Page 8: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

8

Code is our Blueprint

Page 9: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Write facts using the 3A pattern

Lesson #2

Page 10: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

3A Pattern

Attributed to Bill Wake (http://xp123.com) Arrange – Setup the test harness

Act – Run the test

Assert – Check the results

Let’s look at an example!

Page 11: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

11

A Typical Fact

Page 12: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

12

3A Pattern

Arrange

Page 13: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

13

3A Pattern

Act

Page 14: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

14

3A Pattern

Assert

Page 15: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

15

3A Summary

BenefitsReadability

Consistency

LiabilitiesMore Verbose

Might need to introduce local variables

Related IssuesOne Assert per Test?

Page 16: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Work Backwards

Lesson #3

Page 17: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

17

Example

When calling the property Top on a stack it does not change the state of the Stack

Let’s write it backwards

Page 18: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

18

Fact Example - Assert

Page 19: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

19

Fact Example - Act

Page 20: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

20

Fact Example - Arrange

Page 21: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

21

Fact Example – Name the Method

Page 22: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

22

Work Backwards Summary

BenefitsKnown – to – unknown work flow

Improves method and variable naming

LiabilitiesCan often lead to Assert/Act merging

Page 23: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

ExpectedException leads to uncertainty

Lesson #4

Page 24: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

24

ExpectedException Violates 3A

Page 25: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

25

Record the Exception instead

Page 26: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

26

Use Assert.Throws - .NET 2.0

Page 27: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

27

More ExpectedException Problems

Page 28: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

28

Use Assert.Throws

Page 29: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

29

Improved Control Flow

Page 30: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

30

Use Alternatives to ExpectedException

BenefitsReadability (these tests look like all the rest)

Identify and isolate the code where you are expecting the exception

Improved control flow

LiabilitiesAct and Assert are together in Assert.Throws

Anonymous delegate syntax in .NET Framework 2.0 is not great for readability

Page 31: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Be Self-Contained

Lesson #5

Page 32: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

32

Self-Contained

Page 33: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

33

NUnit Test

Page 34: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

34

What’s wrong with SetUp / Teardown

Often requires class member variables

Code is located in up to 3 methods

SetUp/Teardown are often used for more than one test

Page 35: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

35

What’s wrong with base classes?

Inheriting from a class is about identity, not about implementation inheritance

Always test each class in isolation from its environment, including parent class(es)

Consider moving utility functions to utility classes

Page 36: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

36

Be Self-Contained

BenefitsReadability

Test isolation

LiabilitiesDuplicated initialization code

Related TopicsSmall Fixtures

Testable Object Pattern - http://shrinkster.com/wk2

Page 37: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Take the Next Step

Story Test-Driven Development

Lesson#6

Page 38: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

38

Story Test-Driven Development

xUnit.net acceptance test framework

Page 39: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

39

Acceptance Test Driven Development

BenefitsEnd-to-End Testing

Suite evolves over time to match actual customer usage

LiabilitiesPoor tooling support

Can require architectural modifications to your application

Page 40: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Summary

Lesson #1 – The “T” in Test-Driven Development is Wrong

Lesson #2 – Write Tests using the 3A Pattern

Lesson #3 – Work Backwards

Lesson #4 – Use Alternatives to ExpectedException

Lesson #5 – Be Self-Contained

Lesson #6 – Take The Next Step – Acceptance Test-Driven Development

Page 41: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Blogs

Brian Button http://www.agileprogrammer.com/oneagilecoder

Brian Marick http://www.exampler.com

Peter Provost http://peterprovost.org

Brad Wilson http://bradwilson.typepad.com

Page 42: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Books

[Beck] Implementation Patterns by Kent Beck, Addison-Wesley, 2008

xUnit Test Patterns by Gerard Meszaros, Addison-Wesley, 2007

Refactoring to Patterns by Joshua Kerievsky, Addison-Wesley, 2005

Page 43: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

Contact Information

James NewkirkEmail: [email protected]

Blog: http://jamesnewkirk.typepad.com

Page 44: Lessons Learned in Programmer Testinggotocon.com/dl/jaoo-sydney-2009/slides/James... · Lesson #1 –The “T” in Test-Driven Development is Wrong Lesson #2 –Write Tests using

44