unit testing - calgary .net user group - nov 26 2014 - depth consulting
TRANSCRIPT
http://www.depthconsulting.ca
DeveloperFundamentals:The Series
Calgary.NET User Group
Nov. 26, 2014
Unit TestingA Brief Explanation
Dave WhiteMicrosoft MVP – Visual Studio ALM
http://www.depthconsulting.ca
Developer Fundamentals Series
Testing – Nov. 26, 2014
SOLID – February
Dependency Injection – April
Source Control – Branching/Merging – June
Putting It All Together – July
http://www.depthconsulting.ca
Dave White Principal Consultant – Depth Consulting
http://linkd.in/giMxuw
@AgileRamblings
• In IT for over 15 years
• Microsoft MVP – Visual Studio ALM
• Kanban/Agile Evangelist, Speaker
• PRDC 2011, 2012, LKUK, Local events
• Program Director – KCP Program - LKU
• Professional Scrum Master
• Microsoft Platform Developer (C#, Web, WPF)
http://www.agileramblings.com
http://www.depthconsulting.ca
Simon TimmsSenior .NET Dev – Pacesetter Directional Drilling
http://linkd.in/15pYAWL
@stimms
• In IT for over a decade (+/− few months)
• Microsoft MVP – ASP.NET/IIS
• Prolific Speaker and Community Involvement
• PRDC (many), AzureCamp, Local events
• Calgary .NET User Group – President
• Author
• Loves Development
• Polyglot
• Web apps, .NET back ends
• Visualization and Cloud
http://blog.simontimms.com/about/
http://www.depthconsulting.ca
Why We Don’t Test• No one around me is doing it
• Too hard to learn
• No tools
• My stuff doesn’t need it
• Bugs aren’t my problem
• Our code is horrible
• No time
• Manager won’t let me (cost or time)
• Maintain my tests now too?!?!
• We have QA people
http://www.depthconsulting.ca
The truth of the matter…• Everyone is doing it
• Easier than ever
• Investment in tools• Private and OSS
• Increasing Complexity• Business and technical
• Late discovery of defects is expensive
• You can start small
• QA demand growing
• You will be a better developer
• Drives growth down maturity path
• It makes you faster
• Less time on bugs
• More time on fun
http://www.depthconsulting.ca
I got this!
By Adam Jones Adam63 (Own work) [CC-BY-SA-3.0
(http://creativecommons.org/licenses/by-sa/3.0)],
via Wikimedia Commons
http://www.depthconsulting.ca
Types of TestsUnit Test (TDD, BDD or Test After)
One class, One Method, One Scenario per Test
Not Unit Test
Integration Test
Multiple classes/methods
Feature Test
Multiple classes/method/scenarios
Database Tests
Single class/method – multiple boundaries
UI Tests (Automated or manual)
Functional test of application
Load Testing
My system sure can carry the load!
Performance Testing
My unit is very performant.
Usability Testing
That’s a good looking unit!
http://www.depthconsulting.ca
Unit Test Sub-TypesBehaviour
• Concerned with interactions between dependent objects
• Did Object A user Object B correctly
• Requires mocking framework
State
• Concerned only with object mechanics
• Typically faking data in stubs (don’t need mocking framework)
http://www.depthconsulting.ca
Anatomy of a Test
[TestClass] (or Fixture)
Used to identify classes that contain test methods
[TestInitialize] (or Setup)
Identifies the method to run before the test to allocate and configure resources needed by all of the tests
[TestMethod] (Test, Fact, or
Theory)
Used to identify test methods
[TestCleanup] (or Teardown)
Identifies the method to run after the tests to free resources allocated during the test run
[Ignore]
sut or cut = subject or class under test
http://www.depthconsulting.ca
Basic LayoutTestClass
public class
TestMethod
public void
// arrange
// act
// assert
http://www.depthconsulting.ca
Unit Test Frameworks
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
http://www.depthconsulting.ca
By Crosa (Flickr: Scream) [CC-BY-2.0
(http://creativecommons.org/licenses/by/2.0)],
via Wikimedia Commons
YOU CAN’T MAKE ME TEST FIRST!!
http://www.depthconsulting.ca
Pivot – Test Driven Development• TDD is mostly a change in perspective
• NOT ABOUT TESTING• A wonderful side effect
• Unit Tests intend to characterize or “pin down” behavior
• TDD intend to design classes
• First create a hypothesis about your class
• Create your class aligned with hypothesis
• Run experiment
• Rinse & Repeat
http://www.depthconsulting.ca
By FelipeIbazeta (Own work) [CC-BY-SA-3.0
(http://creativecommons.org/licenses/by-sa/3.0)],
via Wikimedia Commons
HUH?!?
My tests should be about business?
http://www.depthconsulting.ca
Pivot – Behaviour Driven Development• BDD is mostly about describing things different
• TestClass Per Scenario
• NOT ABOUT TESTING• A wonderful side effect
• Create a business language scenario
• Setup the world to model your situation
• Run the scenario• Probably multiple assertions in a TestClass
• Rinse & Repeat
http://www.depthconsulting.ca
QA Pros are people too!
(This guys last name is really Tester!)
By U.S. Government (http://tester.senate.gov/)
[Public domain],
via Wikimedia Commons
http://www.depthconsulting.ca
Pivot – There is No Unit in UI• UI Testing is still good!
• Just not a unit test
• Automated or Manual• Design your tests - Describe them in
Test Cases
• Execute them
• Automate them
• <INSERT DEVELOPER HERE>
• Maintain them
• <INSERT MORE DEVELOPER HERE>
http://www.depthconsulting.ca
Microsoft Developers UI Testing• Coded UI Testing
http://msdn.microsoft.com/en-us/library/dd286726.aspx
• Selenium http://www.seleniumhq.org/
• WatiNhttp://watin.org/
http://www.depthconsulting.ca
Hold on a second here Mister!
By Achim Hering (Own work)
[GFDL (http://www.gnu.org/copyleft/fdl.html) or
CC-BY-3.0 (http://creativecommons.org/licenses/by/3.0)],
via Wikimedia Commons
http://www.depthconsulting.ca
The Real Truth - It Can’t Be All Roses• Testing is hard
• Only because it is:
• Unfamiliar – we don’t have these skills yet
• Burdened by a MOUNTAIN of technical debt and bad design
• You will be slower at first
• It may force you to learn new things• SOLID, DI, new Tooling
• These are all GOOD things!
• Managers may be suspicious
• You have to go slow… Start small!
http://www.depthconsulting.ca
The Real Truth – There ARE Roses• You will get faster
• You will create higher quality product
• You will create better designs
• You will help your QA Professional teammates
• You don’t have to ask permission
• Most of the tools are free or inexpensive• You might already have them!!
• You will (eventually) have more fun! :D
http://www.depthconsulting.ca
We can do this!
By Wonderlane from Seattle, USA [CC-BY-2.0
(http://creativecommons.org/licenses/by/2.0)],
via Wikimedia Commons
http://www.depthconsulting.ca
Next Steps• Just go do it
• Do not give up
• Do not ask permission
• Do not give up
• Start small
• Do not give up
• Ask for help
• Do not give up
KEEPCALM
and
TESTON
http://www.depthconsulting.ca
Tools• Visual Studio Community
http://www.visualstudio.com/products/visual-studio-community-vs
• Visual Studio 2015 Previewhttp://www.visualstudio.com/en-us/news/vs2015-preview-vs
• Nuget
• Nunit, xUnit• http://www.nunit.org
• http://github.com/xunit/xunit
http://www.depthconsulting.ca
I’ve Skipped a LOT• Mocking
• Refactoring
• SOLID
• Dependency Injecting
• Automation
• Code Coverage
http://www.depthconsulting.ca
These are the MeetUps you’re looking for…
Testing – Nov. 26, 2014
SOLID – February
Dependency Injection – April
Source Control – Branching/Merging – June
Putting It All Together – July
http://www.depthconsulting.ca
NOW PRIZES!!
2 ReSharper licences to the loudest Unit Testers in the room!!
https://www.jetbrains.com/resharper/
http://www.depthconsulting.ca
ThanksDave White
@agileramblings
http://www.agileramblings.com
Simon Timms
@stimms
http://blog.simontimms.com