better unit tests with approvaltests: an open source library
TRANSCRIPT
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
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.
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
Dan Gilkerson
Llewellyn Falco
Who created ApprovalTests?
DEMO GETTING STARTED
Building your Testing Toolkit
• Test Runner • Java: Junit
• Ruby: Test::Unit
• .NET: NUnit, MSTest
• PHP: PHPUnit
• Approval Test Libraries
• Your Test Code
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
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
DEMO EXPRESSIVENESS
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.
More about
reporters
Image from http://askville.amazon.com/loves-Ferrell-favorite-character-played/AnswerViewer.do?requestId=7446578
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
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?
DEMO ARRAYS
Approval Tests in Java…
DEMO GUIS
Code must
be Testable
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
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
Legacy Code
How much of your code base is dead code?
Image credits – http://dilbert.com/strips/comic/2006-12-08
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
DEMO Legacy Code – Chart Smart
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
Q & A – [email protected]
@WoodyZuill