overcoming the obstacles, pitfalls, and dangers of unit testing

40
Overcoming the Obstacles, Pitfalls, and Dangers of Automated Testing Stephen D. Ritchie 13-Nov-2012

Upload: stephen-ritchie

Post on 04-Jul-2015

1.123 views

Category:

Documents


3 download

DESCRIPTION

Have you ever bumped into a wall with your automated tests? Many developers bump into various roadblocks and hurdles when writing test code. Are your test methods starting to fail because the code-under-test uses DateTime.Now? Are your automated integration tests failing because the database they integrate with keeps changing? Do you have an explosion of test methods, with the ratio of test code to code-under-test way too high? Is your effort to refactor and improve code overwhelmed by the time it takes to rewrite all those failing unit tests? This presentation is about clearing away automated testing obstacles, avoiding common pitfalls, and staying away from dangerous practices.

TRANSCRIPT

Page 1: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Overcoming the Obstacles, Pitfalls, and Dangers of Automated Testing

Stephen D. Ritchie

13-Nov-2012

Page 2: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Live Tweet, Tonight’s Slides and Examples

• Twitter: @RuthlessHelp@Apress

• Slideshare: http://www.slideshare.net/ruthlesshelp

• Github: http://github.com/ruthlesshelp

Page 3: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Purpose

Automated Testing

Useful

Make Software Better

Page 4: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

2

3

1

Agenda

- 4 -

Motivation

Principles

Obstacles

Page 5: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Chrysler New Yorker

Page 6: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

First Topic: Motivation

Why Automate

Testing?

Why Write

Unit Tests?

Page 7: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

2

3

1

Agenda

- 7 -

Motivation

Principles

Obstacles

Page 8: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Problem Detection

Visibility & Insight

Advance Warning

Page 9: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Microscope: Visibility and Insight

Page 10: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Smoke Detector: Problem Detection

Page 11: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Weather Satellite: Advance Warning

Page 12: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Example

Perhaps

An Example

Would Be

Helpful

Page 13: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Software Works

Make Sure

As Intended

Automated Tests

Page 14: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

2

3

Agenda

- 14 -

Motivation

Principles

Obstacles

1

Page 15: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Principles

Fast

Zero Configuration

Clear Result

Easy To Maintain

Page 16: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Automated Testing: Vocabulary

• Test-Driven Development (TDD)

– Write a Test, Watch the Test Fail

– Write Code, Make the Test Pass

– Write the Next Test

• Behavior-Driven Development (BDD)

– Given a Desired Behavior

• Intention Checking

– The Software Works, As Intended

Page 17: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Principles

Zero Configuration

I can run your tests,You can run mine.

Page 18: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Principles

Fast

All the tests run injust a few minutes

Page 19: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Principles

Clear Results

Pass/Fail

Focused Test

Page 20: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Principles

Easy to Maintain

Conventional

Brief

Page 21: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

The Long-Term Goals

• Automated Testing

– Vigilantly Monitoring the Code

• Readability

– Have Mercy on Future Developers

• Conventional

• Short, Clear

• Maintainability

– Both a Sword and a Shield

• Code Works as Intended

• Protects Against Regression

– Reliable

– N+1 is Easy

Page 22: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

3

Agenda

- 22 -

Motivation

Principles

Obstacles

1

2

Page 23: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

One Primary Assert To Rule Them All

Obstacle 1

Over-Specifying

Page 24: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

One Primary Assert To Rule Them All

• Is your effort to refactor and improve code overwhelmed by the time it takes to maintain/update/rewrite all those failing unit tests?

– Your test-code could be over specifying things.

• Perhaps an example would be helpful …

Page 25: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

One Primary Assert To Rule Them All

• Debate: Only one assertion per test?

• Test Method Tests One and Only One Scenario

– 1 Primary Assert Verifies and Validates the Scenario

• Secondary Asserts

– Support Arrangement and Preconditions

– Support Post-Conditions

• Avoid Asserts that Over Specify

– Too Literal => Inhibited Refactoring

– Imagined Benefit => Rigidity

Page 26: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Four Ways to Fake Time

Obstacle 2

Time Crunch

Page 27: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Four Ways to Fake Time

• Are your test methods starting to fail because the code-under-test is coupled to the system clock?

– Your code is too dependent on System.DateTime.Now

• Perhaps an example would be helpful …

Page 28: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Four Ways to Fake Time

• Things to Watch For

– Thread Safety

• public static class SystemDateTime

– Making Your Privates Public

// Inject the class dependency on DateTime.Now

private DateTime? _now;

public DateTime Now

{

get { return _now ?? DateTime.Now; }

set { _now = value; }

}

Page 29: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Database Killed The Integration Test

Obstacle 3

Database

Page 30: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Database Killed The Integration Test

• Are your automated integration tests failing because of the data in the testing database; the data keeps changing?

• Perhaps an example would be helpful …

Page 31: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Database Killed The Integration Test

• Automated Testing Persistence

– NDbUnit

– SQL Server Express

– NHibernate

• Surface Testing

– Data Access Layer: API Surface

– Liberates Refactoring

Page 32: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

In Test Code, Do Repeat Yourself ... Do Repeat Yourself

Obstacle 4

ReuseRepetitionCoupling

Unhelpful …

Page 33: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

In Test Code, Do Repeat Yourself ... Do Repeat Yourself

• Do you have an explosion of test methods, with the ratio of test code to code-under-test that’s way too high?

– Your test-code is too DRY in some places and …

– Not DRY enough in all the right places

• Perhaps an example would be helpful …

Page 34: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

In Test Code, Do Repeat Yourself ... Do Repeat Yourself

• Data-Drive Test Cases

– One Test Method

– Many Test Scenarios

• Repeat Code in a “TestsContext” Class

– Sidecar Approach

• Use Helper Classes

– Extension methods

– Composition

• Keep Inheritance in Reserve

– Overall Testing Framework

Page 35: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Agenda

- 35 -

Motivation

Principles

Obstacles

1

2

3

Page 36: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Of Course It’s Safe … After You

Page 37: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Further Discussion

Any questions?

Any comments?

Page 38: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Shameless Self Promotion Time!

40% off eBook at

apress.com

Use promo code:

PR0N3T

Offer ends 15-Dec-2012

Page 39: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Contact Me

• Twitter: @ruthlesshelp

• Email: [email protected]

• Blog: http://ruthlesslyhelpful.net

• LinkedIn: http://www.linkedin.com/in/sritchie

Page 40: Overcoming the Obstacles, Pitfalls, and Dangers of Unit Testing

Excella Consulting

Slides and Examples

• Slideshare: http://www.slideshare.net/ruthlesshelp

• Github: http://github.com/ruthlesshelp