Building unit tests correctly with visual studio 2013

Dror Helper
Dror HelperConsultant & software architect @ Practical Software at CodeValue
Building Unit Tests correctly with VS 2013
About.ME
• Senior Consultant @CodeValue
• Developing software (Professionally) since 2002
• Mocking code since 2008
• Test driven professional
• Blogger: http://blog.drorhelper.com
/*
* You may think you know what the following code does.
* But you dont. Trust me.
* Fiddle with it, and youll spend many a sleepless
* night cursing the moment you thought youd be clever
* enough to "optimize" the code below.
* Now close this file and go play with something else.
*/
//
// Dear maintainer:
//
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
//
// total_hours_wasted_here = 42
//
//This code sucks, you know it and I know it.
//Move on and call me an idiot later.
http://stackoverflow.com/questions/184618/what-is-the-
best-comment-in-source-code-you-have-ever-encountered
We fear our code!
//Abandon all hope ye who enter beyond this point
//When I wrote this, only God and I understood what I was doing
//Now, God only knows
// I dedicate all this code, all my work, to my wife, Darlene, who will
// have to support me and our three children and the dog once it gets
// released into the public.
//The following 1056 lines of code in this next method
//is a line by line port from VB.NET to C#.
//I ported this code but did not write the original code.
//It remains to me a mystery as to what
//the business logic is trying to accomplish here other than to serve as
//some sort of a compensation shell game invented by a den of thieves.
//Oh well, everyone wants this stuff to work the same as before.
//I guess the devil you know is better than the devil you don't.
“If we’re afraid to change
the very thing we’ve
created,
we failed as professionals”
Robert C. Martin
Legacy code
“Code without tests is bad code...
With tests, we can change the
behavior of our code quickly and
verifiably...”
Michael Feathers - “Working effectively with legacy code”
This is a unit test
[Test]
public void CheckPassword_ValidUser_ReturnTrue()
{
bool result = CheckPassword(“user”, “pass”);
Assert.That(result, Is.True);
}
D
This is also a unit test
[TestMethod]
public void CheckPassword_ValidUser_ReturnTrue()
{
bool result = CheckPassword(“user”, “pass”);
Assert.IsTrue(result);
}
D
A unit test is…
1. Tests specific functionality
2. Clear pass/fail criteria
3. Good unit test runs in isolation
Unit testing is an iterative effort
Unit testing is an iterative effort
There’s more to unit tests then just “tests”
Written by the developer who wrote the code
Quick feedback
Avoid stupid bugs
Immune to regression
Change your code without fear
In code documentation
13
copyright 2008 trainologic LTD
13
One last reason
You’re already Testing your code – manually
So why not save the time?
The cost of unit testing
120%
135%
115%
125%
0%
20%
40%
60%
80%
100%
120%
140%
IBM: Drivers MS: Windows MS: MSN MS: VS
Time taken to code a feature
WithoutTDD Using TDD
The value of unit testing
61%
38%
24%
9%
0%
20%
40%
60%
80%
100%
120%
140%
IBM: Drivers MS: Windows MS: MSN MS: VS
Using Test Driven Design
Time To Code Feature Defect density of team
Major quality improvement for minor time investment
The cost of bugs
0
1
2
3
4
5
6
7
8
9
10
0%
20%
40%
60%
80%
100%
Thousand$s
%defectscreated
Where does it hurt?
% of Defects Introduced Cost to Fix a Defect
The pain is here! This is too late…
Supporting environment
• The team
• Development environment
The Team
• The team commitment is important
• Learn from test reviews
• Track results
Tools of the trade
Server Dev Machine
Source
Control
Build Server
Test Runner
Code
CoverageBuild Script
Unit Testing
Framework
Isolation
Framework
Development environment
• Make it easy to write and run tests
– Unit test framework
– Test Runner
– Isolation framework
• Know where you stand
– Code coverage
Unit testing frameworks
• Create test fixtures
• Assert results
• Additional utilities
• Usually provide command-line/GUI runner
http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
[Test]
public void AddTest()
{
var cut = new Calculator();
var result = cut.Add(2, 3);
Assert.AreEqual(5, result);
}
This is not a real unit test
Real code has dependencies
Unit test
Code under test
Dependency Dependency
The solution - Mocking
Fake object(s)
Unit test
Code under test
Dependency
Isolation
• Replace production logic with custom logic
• We do this in order to
– Focus the test on one class only
– Test Interaction
– Simplify Unit test writing
What Mocking framework can do for you?
• Create Fake objects
• Set behavior on fake objects
• Verify method was called
• And more...
[Test]
public void IsLoginOK_LoggerThrowsException_CallsWebService()
{
var fakeLogger = Isolate.Fake.Instance<ILogger>();
Isolate
.WhenCalled(() => fakeLogger.Write(""))
.WillThrow(new LoggerException());
var fakeWebService = Isolate.Fake.Instance<IWebService>();
var lm = new LoginManagerWithMockAndStub(fakeLogger,fakeWebService);
lm.IsLoginOK("", "");
Isolate.Verify.WasCalledWithAnyArguments(() => fakeWebService.Write(""));
}
Open source
• FakeItEasy
• Moq
• NMock3
• nSubtitute
• Rhino
Mocks
Free
• MS Fakes
Commercial
• Isolator
• JustMock
Moq
45%
Rhino Mocks
23%
None
9%
FakeItEasy
6%
Nsubstitute
6%
Isolator
4%
Moles
2%
MS Fakes
2%
JustMocks
2%
Other
1%
http://osherove.com/blog/2012/5/4/annual-poll-which-isolation-framework-do-
you-use-if-any.html
Code Coverage
So, What is a good code coverage?
Source Control
Build Server
Commit
There you go
What’s new?
Build Agents
We automatically get
•Error reports & logs
•New version installer
•Help files
•More…
Build run at a Glance
How I failed unit testing my code
Unit testing is great!
Everything should be tested
I can test “almost” everything
Tests break all the time
Unit testing is a waste of time 
A good unit test should be:
• Easy to understand
• Trustworthy
• Robust
Trust Your Tests!
Trustworthy means deterministic
Problem
• I cannot re-run the exact same test if a test fails
Solution
• Don’t use Random in tests
– If you care about the values set them
– If you don’t care about the values put defaults
– Do not use Sleep & time related logic – fake it
• Use fake objects to “force” determinism into the test
• When possible avoid threading in tests.
Trustworthy also means not fragile
Ideally
A test would only fail if a bug was introduced
OR
Requirements changed
How to avoid fragile tests
• Don’t test private/internal (most of the time)
• Fake as little as necessary
• Test only one thing (most of the time)
Readable unit tests
Unit test intent should be clear!
1.What is being tested?
2.What is the desired outcome?
3.Why did test fail?
Learn to write “clean tests”
[Test]
public void CalculatorSimpleTest()
{
calc.ValidOperation = Calculator.Operation.Multiply;
calc.ValidType = typeof (int);
result = calc.Multiply(1, 3);
Assert.IsTrue(result == 3);
if (calc.ValidOperation == Calculator.Operation.Invalid)
{
throw new Exception("Operation should be valid");
}
}
Or suffer the consequences!
[Test]
public void CalculatorSimpleTest()
{
var calc = new Calculator();
calc.ValidOperation = Calculator.Operation.Multiply;
calc.ValidType = typeof (int);
var result = calc.Multiply(-1, 3);
Assert.AreEqual(result, -3);
calc.ValidOperation = Calculator.Operation.Multiply;
calc.ValidType = typeof (int);
result = calc.Multiply(1, 3);
Assert.IsTrue(result == 3);
if (calc.ValidOperation == Calculator.Operation.Invalid)
{
throw new Exception("Operation should be valid");
}
calc.ValidOperation = Calculator.Operation.Multiply;
calc.ValidType = typeof (int);
result = calc.Multiply(10, 3);
Assert.AreEqual(result, 30);
}
Writing a good unit test
[Test]
public void
Multiply_PassTwoPositiveNumbers_ReturnCorrectResult()
{
var calculator = CreateMultiplyCalculator();
var result = calculator.Multiply(1, 3);
Assert.AreEqual(result, 3);
}
So what about code reuse
Readability is more important than code reuse
• Create objects using factories
• Put common and operations in helper
methods
• Use inheritance – sparsely
Avoid logic in the test (if, switch etc.)
Problem
• Test is not readable
• Has several possible paths
• High maintain cost
Tests should be deterministic
Solution
• Split test to several tests – one for each
path
– If logic change it’s easier to update some of the
tests (or delete them)
• One Assert per test rule
How to start with unit tests
1. Test what you’re working on – right now!
2. Write tests to reproduce reported bug
3. When refactoring existing code – use unit tests to
make sure it still works.
4. Delete obsolete tests (and code)
5. All tests must run as part of CI build
Building unit tests correctly with visual studio 2013
1 of 46

Recommended

Battle of The Mocking Frameworks by
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking FrameworksDror Helper
3.2K views26 slides
Working Effectively with Legacy Code: Lessons in Practice by
Working Effectively with Legacy Code: Lessons in PracticeWorking Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeAmar Shah
644 views33 slides
Working with Legacy Code by
Working with Legacy CodeWorking with Legacy Code
Working with Legacy CodeEyal Golan
5.8K views48 slides
Improving the Quality of Existing Software - DevIntersection April 2016 by
Improving the Quality of Existing Software - DevIntersection April 2016Improving the Quality of Existing Software - DevIntersection April 2016
Improving the Quality of Existing Software - DevIntersection April 2016Steven Smith
1.3K views78 slides
Getting Unstuck: Working with Legacy Code and Data by
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataCory Foy
1.9K views49 slides
Unit Testing 101 by
Unit Testing 101Unit Testing 101
Unit Testing 101Dave Bouwman
13.5K views89 slides

More Related Content

What's hot

Working Effectively with Legacy Code by
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy CodeOrbit One - We create coherence
3.4K views36 slides
Improving the Quality of Existing Software by
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing SoftwareSteven Smith
1.7K views75 slides
Writing clean code in C# and .NET by
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NETDror Helper
13.9K views49 slides
Working With Legacy Code by
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
3.7K views33 slides
Devday2016 real unittestingwithmockframework-phatvu by
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuPhat VU
449 views56 slides
Unit Test + Functional Programming = Love by
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
16.9K views68 slides

What's hot(20)

Improving the Quality of Existing Software by Steven Smith
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith1.7K views
Writing clean code in C# and .NET by Dror Helper
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
Dror Helper13.9K views
Working With Legacy Code by Andrea Polci
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
Andrea Polci3.7K views
Devday2016 real unittestingwithmockframework-phatvu by Phat VU
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
Phat VU449 views
Unit Test + Functional Programming = Love by Alvaro Videla
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
Alvaro Videla16.9K views
Principles and patterns for test driven development by Stephen Fuqua
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
Stephen Fuqua4.2K views
Breaking Dependencies to Allow Unit Testing by Steven Smith
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith1.4K views
TDD and the Legacy Code Black Hole by Noam Kfir
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
Noam Kfir514 views
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes by Roy Osherove
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove25.1K views
VT.NET 20160411: An Intro to Test Driven Development (TDD) by Rob Hale
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Rob Hale445 views
iOS Test-Driven Development by Pablo Villar
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar1.5K views
Adding Unit Test To Legacy Code by Terry Yin
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
Terry Yin2.3K views
Unit Testing Done Right by Brian Fenton
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
Brian Fenton1.2K views
Unit testing (workshop) by Foyzul Karim
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
Foyzul Karim1.7K views
Practical Test Automation Deep Dive by Alan Richardson
Practical Test Automation Deep DivePractical Test Automation Deep Dive
Practical Test Automation Deep Dive
Alan Richardson1.7K views
Embrace Unit Testing by alessiopace
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
alessiopace4K views
Improving the Quality of Existing Software by Steven Smith
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith1.3K views
Unit Testing Fundamentals by Richard Paul
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul4K views

Similar to Building unit tests correctly with visual studio 2013

Building unit tests correctly by
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
1.4K views40 slides
Unit testing - A&BP CC by
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CCJWORKS powered by Ordina
722 views113 slides
Getting Started With Testing by
Getting Started With TestingGetting Started With Testing
Getting Started With TestingGiovanni Scerra ☃
740 views39 slides
Test driven development by
Test driven developmentTest driven development
Test driven developmentchristoforosnalmpantis
786 views27 slides
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex by
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
839 views32 slides
Developer Tests - Things to Know (Vilnius JUG) by
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)vilniusjug
376 views103 slides

Similar to Building unit tests correctly with visual studio 2013(20)

Building unit tests correctly by Dror Helper
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
Dror Helper1.4K views
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex by michael.labriola
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
michael.labriola839 views
Developer Tests - Things to Know (Vilnius JUG) by vilniusjug
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
vilniusjug376 views
Unit tests & TDD by Dror Helper
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper3.3K views
Qt test framework by ICS
Qt test frameworkQt test framework
Qt test framework
ICS10.1K views
Mastering PowerShell Testing with Pester by Mark Wragg
Mastering PowerShell Testing with PesterMastering PowerShell Testing with Pester
Mastering PowerShell Testing with Pester
Mark Wragg268 views
Grails unit testing by pleeps
Grails unit testingGrails unit testing
Grails unit testing
pleeps4.1K views
Code igniter unittest-part1 by Albert Rosa
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
Albert Rosa7.2K views
We Are All Testers Now: The Testing Pyramid and Front-End Development by All Things Open
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open375 views
Unit Testing - The Whys, Whens and Hows by atesgoral
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Hows
atesgoral684 views
Software Testing by AdroitLogic
Software TestingSoftware Testing
Software Testing
AdroitLogic405 views
An Introduction to unit testing by Steven Casey
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
Steven Casey2.7K views

More from Dror Helper

Unit testing patterns for concurrent code by
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
429 views44 slides
The secret unit testing tools no one ever told you about by
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutDror Helper
273 views40 slides
Debugging with visual studio beyond 'F5' by
Debugging with visual studio beyond 'F5'Debugging with visual studio beyond 'F5'
Debugging with visual studio beyond 'F5'Dror Helper
306 views29 slides
From clever code to better code by
From clever code to better codeFrom clever code to better code
From clever code to better codeDror Helper
198 views38 slides
From clever code to better code by
From clever code to better codeFrom clever code to better code
From clever code to better codeDror Helper
236 views26 slides
A software developer guide to working with aws by
A software developer guide to working with awsA software developer guide to working with aws
A software developer guide to working with awsDror Helper
1.3K views23 slides

More from Dror Helper(20)

Unit testing patterns for concurrent code by Dror Helper
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
Dror Helper429 views
The secret unit testing tools no one ever told you about by Dror Helper
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you about
Dror Helper273 views
Debugging with visual studio beyond 'F5' by Dror Helper
Debugging with visual studio beyond 'F5'Debugging with visual studio beyond 'F5'
Debugging with visual studio beyond 'F5'
Dror Helper306 views
From clever code to better code by Dror Helper
From clever code to better codeFrom clever code to better code
From clever code to better code
Dror Helper198 views
From clever code to better code by Dror Helper
From clever code to better codeFrom clever code to better code
From clever code to better code
Dror Helper236 views
A software developer guide to working with aws by Dror Helper
A software developer guide to working with awsA software developer guide to working with aws
A software developer guide to working with aws
Dror Helper1.3K views
The secret unit testing tools no one has ever told you about by Dror Helper
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you about
Dror Helper340 views
The role of the architect in agile by Dror Helper
The role of the architect in agileThe role of the architect in agile
The role of the architect in agile
Dror Helper269 views
Harnessing the power of aws using dot net core by Dror Helper
Harnessing the power of aws using dot net coreHarnessing the power of aws using dot net core
Harnessing the power of aws using dot net core
Dror Helper281 views
Developing multi-platform microservices using .NET core by Dror Helper
 Developing multi-platform microservices using .NET core Developing multi-platform microservices using .NET core
Developing multi-platform microservices using .NET core
Dror Helper408 views
Harnessing the power of aws using dot net by Dror Helper
Harnessing the power of aws using dot netHarnessing the power of aws using dot net
Harnessing the power of aws using dot net
Dror Helper217 views
Secret unit testing tools no one ever told you about by Dror Helper
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you about
Dror Helper458 views
C++ Unit testing - the good, the bad & the ugly by Dror Helper
C++ Unit testing - the good, the bad & the uglyC++ Unit testing - the good, the bad & the ugly
C++ Unit testing - the good, the bad & the ugly
Dror Helper1.9K views
Working with c++ legacy code by Dror Helper
Working with c++ legacy codeWorking with c++ legacy code
Working with c++ legacy code
Dror Helper749 views
Visual Studio tricks every dot net developer should know by Dror Helper
Visual Studio tricks every dot net developer should knowVisual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should know
Dror Helper525 views
Secret unit testing tools by Dror Helper
Secret unit testing toolsSecret unit testing tools
Secret unit testing tools
Dror Helper460 views
Electronics 101 for software developers by Dror Helper
Electronics 101 for software developersElectronics 101 for software developers
Electronics 101 for software developers
Dror Helper1.2K views
Navigating the xDD Alphabet Soup by Dror Helper
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
Dror Helper968 views
Who’s afraid of WinDbg by Dror Helper
Who’s afraid of WinDbgWho’s afraid of WinDbg
Who’s afraid of WinDbg
Dror Helper1.9K views
Unit testing patterns for concurrent code by Dror Helper
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
Dror Helper3K views

Recently uploaded

DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... by
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...Deltares
14 views23 slides
WebAssembly by
WebAssemblyWebAssembly
WebAssemblyJens Siebert
48 views18 slides
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t... by
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...Deltares
9 views26 slides
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...Deltares
17 views12 slides
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptxNeo4j
7 views26 slides
Generic or specific? Making sensible software design decisions by
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
6 views60 slides

Recently uploaded(20)

DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... by Deltares
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
Deltares14 views
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t... by Deltares
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
Deltares9 views
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares17 views
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j7 views
Generic or specific? Making sensible software design decisions by Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 views
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... by Marc Müller
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Marc Müller37 views
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... by Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 views
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... by TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin96 views
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok5 views
Headless JS UG Presentation.pptx by Jack Spektor
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptx
Jack Spektor7 views
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... by Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri825 views
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs by Deltares
DSD-INT 2023 The Danube Hazardous Substances Model - KovacsDSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
Deltares8 views
Airline Booking Software by SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta6 views
Sprint 226 by ManageIQ
Sprint 226Sprint 226
Sprint 226
ManageIQ5 views
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... by Deltares
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
Deltares5 views
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ... by Deltares
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
Deltares11 views

Building unit tests correctly with visual studio 2013

  • 1. Building Unit Tests correctly with VS 2013
  • 2. About.ME • Senior Consultant @CodeValue • Developing software (Professionally) since 2002 • Mocking code since 2008 • Test driven professional • Blogger: http://blog.drorhelper.com
  • 3. /* * You may think you know what the following code does. * But you dont. Trust me. * Fiddle with it, and youll spend many a sleepless * night cursing the moment you thought youd be clever * enough to "optimize" the code below. * Now close this file and go play with something else. */ // // Dear maintainer: // // Once you are done trying to 'optimize' this routine, // and have realized what a terrible mistake that was, // please increment the following counter as a warning // to the next guy: // // total_hours_wasted_here = 42 //
  • 4. //This code sucks, you know it and I know it. //Move on and call me an idiot later. http://stackoverflow.com/questions/184618/what-is-the- best-comment-in-source-code-you-have-ever-encountered We fear our code! //Abandon all hope ye who enter beyond this point //When I wrote this, only God and I understood what I was doing //Now, God only knows // I dedicate all this code, all my work, to my wife, Darlene, who will // have to support me and our three children and the dog once it gets // released into the public. //The following 1056 lines of code in this next method //is a line by line port from VB.NET to C#. //I ported this code but did not write the original code. //It remains to me a mystery as to what //the business logic is trying to accomplish here other than to serve as //some sort of a compensation shell game invented by a den of thieves. //Oh well, everyone wants this stuff to work the same as before. //I guess the devil you know is better than the devil you don't.
  • 5. “If we’re afraid to change the very thing we’ve created, we failed as professionals” Robert C. Martin
  • 7. “Code without tests is bad code... With tests, we can change the behavior of our code quickly and verifiably...” Michael Feathers - “Working effectively with legacy code”
  • 8. This is a unit test [Test] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.That(result, Is.True); } D
  • 9. This is also a unit test [TestMethod] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.IsTrue(result); } D
  • 10. A unit test is… 1. Tests specific functionality 2. Clear pass/fail criteria 3. Good unit test runs in isolation
  • 11. Unit testing is an iterative effort Unit testing is an iterative effort
  • 12. There’s more to unit tests then just “tests” Written by the developer who wrote the code Quick feedback Avoid stupid bugs Immune to regression Change your code without fear In code documentation
  • 13. 13 copyright 2008 trainologic LTD 13 One last reason You’re already Testing your code – manually So why not save the time?
  • 14. The cost of unit testing 120% 135% 115% 125% 0% 20% 40% 60% 80% 100% 120% 140% IBM: Drivers MS: Windows MS: MSN MS: VS Time taken to code a feature WithoutTDD Using TDD
  • 15. The value of unit testing 61% 38% 24% 9% 0% 20% 40% 60% 80% 100% 120% 140% IBM: Drivers MS: Windows MS: MSN MS: VS Using Test Driven Design Time To Code Feature Defect density of team Major quality improvement for minor time investment
  • 16. The cost of bugs 0 1 2 3 4 5 6 7 8 9 10 0% 20% 40% 60% 80% 100% Thousand$s %defectscreated Where does it hurt? % of Defects Introduced Cost to Fix a Defect The pain is here! This is too late…
  • 17. Supporting environment • The team • Development environment
  • 18. The Team • The team commitment is important • Learn from test reviews • Track results
  • 19. Tools of the trade Server Dev Machine Source Control Build Server Test Runner Code CoverageBuild Script Unit Testing Framework Isolation Framework
  • 20. Development environment • Make it easy to write and run tests – Unit test framework – Test Runner – Isolation framework • Know where you stand – Code coverage
  • 21. Unit testing frameworks • Create test fixtures • Assert results • Additional utilities • Usually provide command-line/GUI runner http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
  • 22. [Test] public void AddTest() { var cut = new Calculator(); var result = cut.Add(2, 3); Assert.AreEqual(5, result); } This is not a real unit test
  • 23. Real code has dependencies Unit test Code under test Dependency Dependency
  • 24. The solution - Mocking Fake object(s) Unit test Code under test Dependency
  • 25. Isolation • Replace production logic with custom logic • We do this in order to – Focus the test on one class only – Test Interaction – Simplify Unit test writing
  • 26. What Mocking framework can do for you? • Create Fake objects • Set behavior on fake objects • Verify method was called • And more...
  • 27. [Test] public void IsLoginOK_LoggerThrowsException_CallsWebService() { var fakeLogger = Isolate.Fake.Instance<ILogger>(); Isolate .WhenCalled(() => fakeLogger.Write("")) .WillThrow(new LoggerException()); var fakeWebService = Isolate.Fake.Instance<IWebService>(); var lm = new LoginManagerWithMockAndStub(fakeLogger,fakeWebService); lm.IsLoginOK("", ""); Isolate.Verify.WasCalledWithAnyArguments(() => fakeWebService.Write("")); }
  • 28. Open source • FakeItEasy • Moq • NMock3 • nSubtitute • Rhino Mocks Free • MS Fakes Commercial • Isolator • JustMock
  • 30. Code Coverage So, What is a good code coverage?
  • 31. Source Control Build Server Commit There you go What’s new? Build Agents We automatically get •Error reports & logs •New version installer •Help files •More…
  • 32. Build run at a Glance
  • 33. How I failed unit testing my code Unit testing is great! Everything should be tested I can test “almost” everything Tests break all the time Unit testing is a waste of time 
  • 34. A good unit test should be: • Easy to understand • Trustworthy • Robust Trust Your Tests!
  • 35. Trustworthy means deterministic Problem • I cannot re-run the exact same test if a test fails Solution • Don’t use Random in tests – If you care about the values set them – If you don’t care about the values put defaults – Do not use Sleep & time related logic – fake it • Use fake objects to “force” determinism into the test • When possible avoid threading in tests.
  • 36. Trustworthy also means not fragile Ideally A test would only fail if a bug was introduced OR Requirements changed
  • 37. How to avoid fragile tests • Don’t test private/internal (most of the time) • Fake as little as necessary • Test only one thing (most of the time)
  • 38. Readable unit tests Unit test intent should be clear! 1.What is being tested? 2.What is the desired outcome? 3.Why did test fail?
  • 39. Learn to write “clean tests” [Test] public void CalculatorSimpleTest() { calc.ValidOperation = Calculator.Operation.Multiply; calc.ValidType = typeof (int); result = calc.Multiply(1, 3); Assert.IsTrue(result == 3); if (calc.ValidOperation == Calculator.Operation.Invalid) { throw new Exception("Operation should be valid"); } }
  • 40. Or suffer the consequences! [Test] public void CalculatorSimpleTest() { var calc = new Calculator(); calc.ValidOperation = Calculator.Operation.Multiply; calc.ValidType = typeof (int); var result = calc.Multiply(-1, 3); Assert.AreEqual(result, -3); calc.ValidOperation = Calculator.Operation.Multiply; calc.ValidType = typeof (int); result = calc.Multiply(1, 3); Assert.IsTrue(result == 3); if (calc.ValidOperation == Calculator.Operation.Invalid) { throw new Exception("Operation should be valid"); } calc.ValidOperation = Calculator.Operation.Multiply; calc.ValidType = typeof (int); result = calc.Multiply(10, 3); Assert.AreEqual(result, 30); }
  • 41. Writing a good unit test [Test] public void Multiply_PassTwoPositiveNumbers_ReturnCorrectResult() { var calculator = CreateMultiplyCalculator(); var result = calculator.Multiply(1, 3); Assert.AreEqual(result, 3); }
  • 42. So what about code reuse Readability is more important than code reuse • Create objects using factories • Put common and operations in helper methods • Use inheritance – sparsely
  • 43. Avoid logic in the test (if, switch etc.) Problem • Test is not readable • Has several possible paths • High maintain cost
  • 44. Tests should be deterministic Solution • Split test to several tests – one for each path – If logic change it’s easier to update some of the tests (or delete them) • One Assert per test rule
  • 45. How to start with unit tests 1. Test what you’re working on – right now! 2. Write tests to reproduce reported bug 3. When refactoring existing code – use unit tests to make sure it still works. 4. Delete obsolete tests (and code) 5. All tests must run as part of CI build

Editor's Notes

  1. Taken from http://research.microsoft.com/en-us/projects/esm/nagappan_tdd.pdf Realizing quality improvement through test driven development: results and experiences of four industrial teams Published online: 27 February 2008
  2. Explain about good places to start using unit tests