better unit tests with approvaltests: an open source library

25
T8 Test Techniques 5/2/2013 11:15:00 AM Better Unit Tests with ApprovalTests: An Open Source Library Presented by: Woody Zuill Hunter Industries Brought to you by: 340 Corporate Way, Suite 300, Orange Park, FL 32073 888-268-8770 ∙ 904-278-0524 ∙ [email protected] www.sqe.com

Upload: techwellpresentations

Post on 11-Jul-2015

76 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Better Unit Tests with ApprovalTests: An Open Source Library

T8 Test Techniques

5/2/2013 11:15:00 AM

Better Unit Tests with ApprovalTests:

An Open Source Library

Presented by:

Woody Zuill

Hunter Industries

Brought to you by:

340 Corporate Way, Suite 300, Orange Park, FL 32073

888-268-8770 ∙ 904-278-0524 ∙ [email protected] ∙ www.sqe.com

Page 2: Better Unit Tests with ApprovalTests: An Open Source Library

Woody Zuill

An agile coach for the development team at Hunter Industries, Woody Zuill creates and supports software for internal company departments using a wide range of technologies, programming languages, and platforms. For the past thirteen years Woody has worked as an agile coach and developer in both large and small environments, training more than twenty teams and more than 100 developers in agile practices. Woody has been programming computers for twenty-nine years and believes that code must be simple, clean, testable, and maintainable so we can respond to change while quickly delivering working software. He has a passion for bringing unmaintainable code back into a manageable, healthy state.

Page 3: Better Unit Tests with ApprovalTests: An Open Source Library

An open source library

Presented by Woody Zuill

Slides by Woody Zuill and Lynn Langit

StarEast 2013 – 11:15 am, May 2, 2013

Better Unit Testing With Approval Tests

Copyright 2012-2013 Woody Zuill, Lyn Langit

Page 4: Better Unit Tests with ApprovalTests: An Open Source Library

Dan Gilkerson

Llewellyn Falco

Who created ApprovalTests?

Page 5: Better Unit Tests with ApprovalTests: An Open Source Library

DEMO GETTING STARTED

Page 6: Better Unit Tests with ApprovalTests: An Open Source Library

Building your Testing Toolkit

• Test Runner • Java: Junit

• Ruby: Test::Unit

• .NET: NUnit, MSTest

• PHP: PHPUnit

• Approval Test Libraries

• Your Test Code

Page 7: Better Unit Tests with ApprovalTests: An Open Source Library

TDD - Red, Green, Refactor

Approval Tests enhance existing TDD frameworks Approval Tests are an alternative to Asserts

RED Write a

failing test

GREEN

Make it pass

REFACTOR

Improve the code

Page 8: Better Unit Tests with ApprovalTests: An Open Source Library

Approval Tests make it EASY…

• … for human evaluation of test results

• … to set up Tests

• … to modify test output for readability

• … to maintain tests

• … in multiple languages

• … with visual results

• … to make characterization tests

Page 9: Better Unit Tests with ApprovalTests: An Open Source Library

DEMO EXPRESSIVENESS

Page 10: Better Unit Tests with ApprovalTests: An Open Source Library

Approving the Reported Result

• Contents are serialized to a file as binary (to compare) • Rename *.received to *.approved • Right click ‘Use whole file’ • Copy ‘Move’ statement from Test Results to command

window.

Page 11: Better Unit Tests with ApprovalTests: An Open Source Library

More about

reporters

Image from http://askville.amazon.com/loves-Ferrell-favorite-character-played/AnswerViewer.do?requestId=7446578

Page 12: Better Unit Tests with ApprovalTests: An Open Source Library

A Little About Reporters

• Reporters provide a rich, expressive, human-friendly view of a failed test.

• Reporters usually hook into DiffTools (Text, Image, Sound…)

• Support for many diff tools included in Reporters • Can add hooks to your favorite diff tool

Page 13: Better Unit Tests with ApprovalTests: An Open Source Library
Page 14: Better Unit Tests with ApprovalTests: An Open Source Library

Best Unit Test Output Provides:

• Granularity

• What is the result of a break

• What is the specific cause and location of a break

• Feedback

• Frequent (or even constant) feedback

• Specification

• What is this code supposed to do?

• How do we use it?

• Regression-Proofing

• Does EVERYTHING still work?

Page 15: Better Unit Tests with ApprovalTests: An Open Source Library

DEMO ARRAYS

Page 16: Better Unit Tests with ApprovalTests: An Open Source Library

Approval Tests in Java…

Page 17: Better Unit Tests with ApprovalTests: An Open Source Library

DEMO GUIS

Page 18: Better Unit Tests with ApprovalTests: An Open Source Library

Code must

be Testable

Page 19: Better Unit Tests with ApprovalTests: An Open Source Library

Code must be Testable

• Code must include Tests

• Code should be written so that it is easy to write it’s required tests

• “Reduce to Functional”

• Unit Tests must be kept runnable

Page 20: Better Unit Tests with ApprovalTests: An Open Source Library

Unit Test Frameworks are Multi-Purpose

• Application code (components) New Code

• Functionality

• Edge cases Testing APIs

• Locking tests

• Characterization tests Legacy Code

• Scope

• Functionality

Learning new APIs

Page 21: Better Unit Tests with ApprovalTests: An Open Source Library

Legacy Code

How much of your code base is dead code?

Image credits – http://dilbert.com/strips/comic/2006-12-08

Page 22: Better Unit Tests with ApprovalTests: An Open Source Library

Locking Tests (Characterization Tests)

Test to enable

refactoring

Ensures system still works the

same

Most often used with

Legacy Code

Quickly results in high test coverage

Page 23: Better Unit Tests with ApprovalTests: An Open Source Library

DEMO Legacy Code – Chart Smart

Page 24: Better Unit Tests with ApprovalTests: An Open Source Library

For More Information

www.ApprovalTests.com

Pick your language & download

Pick your diff tool

Reference, Approve & Enjoy

Learn more – You Tube videos: http://bit.ly/YZQXCc

Podcast – HerdingCode - http://bit.ly/10puZnJ

Page 25: Better Unit Tests with ApprovalTests: An Open Source Library

Q & A – [email protected]

@WoodyZuill