automated unit testing for mere mortals
DESCRIPTION
TRANSCRIPT
![Page 1: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/1.jpg)
Automated Unit Testing for Mere Mortals
Jess ChadwickArchitect / RIA Specialist
Dow Jones, Inc
blog.jesschadwick.com@jchadwick
![Page 2: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/2.jpg)
![Page 3: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/3.jpg)
![Page 4: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/4.jpg)
What is an “automated unit test”?
What is a “unit”?
![Page 5: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/5.jpg)
Automated Tests…• Ensure components work as designed• Guard against regressions• Exercise the system• Expose how the system works
– Documentation
![Page 6: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/6.jpg)
Automated Unit Tests are…• Fast• Deterministic• Independent• Repeatable• Focused
![Page 7: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/7.jpg)
Unit tests…• Do not cross application / process boundaries
![Page 8: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/8.jpg)
INTEGRATION TESTS
![Page 9: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/9.jpg)
Integration Tests…• Ensure two or more system components work
together• Cross application / process boundaries
• Are unreliable (non-deterministic)– Encourage ignoring failing tests– This is very bad!
![Page 10: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/10.jpg)
DEMO: A SAMPLE UNIT TEST
![Page 11: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/11.jpg)
TEST DOUBLESMocks and Stubs and Fakes… oh my!
![Page 12: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/12.jpg)
Test Doubles• Stand in for production components• Provide deterministic behavior• Provide control over testing environment
![Page 13: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/13.jpg)
Source: Unit Testing: Exploring The Continuum Of Test Doubles MSDN Magazine, September, 2007
![Page 14: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/14.jpg)
CODE COVERAGE
![Page 15: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/15.jpg)
Code Coverage is…
• The degree to which source code is tested
![Page 16: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/16.jpg)
RETROFITTING EXISTING CODE WITH TESTS
![Page 17: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/17.jpg)
![Page 18: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/18.jpg)
UNIT TEST ANTI-PATTERNS
![Page 19: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/19.jpg)
!! Warning !!
![Page 20: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/20.jpg)
Unit Test Anti-Patterns and Smells
• Test multiple components at once
• Slow– Cross application / process boundaries
• Depend on other tests or external interaction (non-deterministic)
![Page 21: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/21.jpg)
Unit Test Anti-Patterns and Smells
• Multiple assertions
• No assertions
• Unclear / vague assertions
• Logging / Console.WriteLine()
![Page 22: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/22.jpg)
TEST-DRIVEN DEVELOPMENT
![Page 23: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/23.jpg)
A different way of thinking• Part of Agile / Extreme Programming beliefs• Tests drive system design
– TDD = “Test-Driven Design”– “How am I going to test this?”
• Helps create loosely-coupled components
![Page 24: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/24.jpg)
Test-First Development• No code can be written unless driven by a
failing test
• Red – Green - Refactor
![Page 25: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/25.jpg)
TDD Pair Programming Game• Player 1: Write failing test• Player 2: Make failing test pass• Player 1: Refactor• … repeat!
![Page 26: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/26.jpg)
IDEAS TO TAKE HOME
![Page 27: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/27.jpg)
![Page 28: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/28.jpg)
![Page 29: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/29.jpg)
Resources• My Blog: http://blog.jesschadwick.com • Unit Testing Frameworks
– Team System / MS Test– NUnit: http://www.nunit.org
• Visual Studio Add-ins– Test-Driven.Net: http://www.testdriven.net – ReSharper: http://www.jetbrains.com
• Mock Frameworks– Moq, Rhino Mocks, NMock, TypeMock, NUnit
Mocks, DotNetMock, EasyMock.NET
![Page 30: Automated Unit Testing for Mere Mortals](https://reader036.vdocuments.net/reader036/viewer/2022081414/54c41f844a795968638b4569/html5/thumbnails/30.jpg)