roy osherove on unit testing good practices and horrible mistakes
TRANSCRIPT
![Page 1: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/1.jpg)
Unit Testing Good Practices & Horrible Mistakes
@RoyOsherove
![Page 2: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/2.jpg)
What I do (artOfUnitTesting.com)
Courses on TDD, BDD in JS, Ruby, Java and C# TDD (EpiServer TDD, MVC TDD…)Courses for Team Leaders (5whys.com)Consulting & coaching through Bouvet
Contact.osherove.com
Team Agile - All rights reserved
![Page 3: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/3.jpg)
Real World Agenda
Test Reviews
Making your tests TRUSTworthyCreating MAINTAINable testsREADable tests
RTFM
![Page 4: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/4.jpg)
Test review vs. code review
Understand intent of developer10 times quickerDrill in when needed
Important for learning teamsHelps drive change
![Page 5: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/5.jpg)
Maintainable
Trustworthy
Readable
![Page 6: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/6.jpg)
Make Your tests trust worthy
Make it easy to runRemoving or changing testsAssuring code coverageAvoid test logic Avoid Manual Stub\Mock LogicDon’t Repeat Production Logic
![Page 7: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/7.jpg)
Separate Unit From Integration Tests
![Page 8: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/8.jpg)
Code coverage?
Worthless without a code reviewChange production code and see what happens
Make params into constsRemove “if” checks – or make into consts
If all tests pass - something is missing or something is not needed
![Page 9: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/9.jpg)
Avoid test logic (MS Unity)
![Page 10: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/10.jpg)
Another logic example:MVCDev.HtmlHelperTest.GetHttpContext()
![Page 11: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/11.jpg)
Don’t repeat production logic
String user = “a”;String password= “b”;
String SomeResult = UnderTest.CreateMessage(user,password);
Assert.AreEqual( user + “,” + password,
SomeResult);
![Page 12: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/12.jpg)
Don't use things that keep changing
DateTime.NowRandomEnvironment.TickCountThreadsEtc..
![Page 13: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/13.jpg)
Tests that keep changing: NerdDinner
![Page 14: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/14.jpg)
Creating maintainable tests
Avoid testing private/protected membersRe-use test code (Create, Manipulate, Assert)Enforce test isolationTest One Thing
Avoid Multiple AssertsOne mock per test
Use “relaxed” or “Non strict” mocks and stubs
![Page 15: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/15.jpg)
Test only publics
“Unit” testing == “Unit Of Work Testing”Testing a private makes your test more brittleMakes you think about the design and usability of a featureTest-First leads to private members after Refactoring, but they are all tested!
But sometimes there’s not choice
![Page 16: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/16.jpg)
Reuse test code
Create objects using common methods (factories)
[make_XX]Manipulate and configure initial state using common methods
[init_XX]Run common tests in common methods
[verify_XX]
![Page 17: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/17.jpg)
Enforce test isolation
No dependency between tests!Don’t run a test from another test!Run alone or all together in any orderOtherwise – leads to nasty “what was that?” bugs
![Page 18: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/18.jpg)
Avoid Multiple Asserts On different objects
String user = “a”;String password= “b”;
String SomeResult = UnderTest.CreateMessage(user,password);
Assert.AreEqual( user + “,” + password, SomeResult);
Assert.AreEqual (1,UnderTest.MessageCount);
![Page 19: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/19.jpg)
![Page 20: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/20.jpg)
Avoid Multiple AssertsMVCDev.LinkExtensionsTest L. 334
![Page 21: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/21.jpg)
![Page 22: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/22.jpg)
Avoid Multiple Asserts
Like having multiple testsBut the first the fails – kills the othersYou won’t get the big picture (some symptoms)Hard to nameCan lead to more debugging work
![Page 23: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/23.jpg)
Avoid Multiple Mocks
![Page 24: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/24.jpg)
Creating readable tests
StructureNo Magic values
In testIn mock\stub
Naming a unit testNaming variablesSeparate Assert from action
![Page 25: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/25.jpg)
Structure
Are the tests easy to find?Can you find a test for a class, or a method?
![Page 26: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/26.jpg)
Setup Mystery
![Page 27: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/27.jpg)
Bad Naming
![Page 28: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/28.jpg)
No Magic Values
Assert.AreEqual(1003, calc.Parse(“-1”));
Int parseResult = calc.Parse(NEGATIVE_ILLEGAL_NUMBER);
Assert.AreEqual(NEGATIVE_PARSE_RETURN_CODE, parseResult)
![Page 29: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/29.jpg)
Hidden Values
![Page 30: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/30.jpg)
Magic values in Fakes
![Page 31: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/31.jpg)
![Page 32: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/32.jpg)
Magic ValuesUnity InjectionMethodFixture
![Page 33: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/33.jpg)
Call it what it is (mock\stub\fake)
Most “Mocks” are actually stubs.If it can be both, call it “Fake”
![Page 34: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/34.jpg)
Naming a unit test
Method nameState under testExpected behavior/return value
Add_LessThanZero_ThrowsException()Another angle:Add_LessThanZero_ThrowsException2()
![Page 35: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/35.jpg)
Naming
![Page 36: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/36.jpg)
Separate Assert from Action
Assert is less clutteredMore readableAllows handling the return value if neededIt’s all about readability
![Page 37: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/37.jpg)
Input Values Should Differ
X.Divide (1,1)X.Divide (1,2)
X.Check(“1,1”)X.Check(“1,2”)
![Page 38: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/38.jpg)
Maintainable
Trustworthy
Readable
![Page 39: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/39.jpg)
ArtOfUnitTesting.com
![Page 40: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/40.jpg)
Song?
![Page 41: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/41.jpg)
This is a test line
![Page 42: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/42.jpg)
Looks like you’re doing fine
![Page 43: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/43.jpg)
Time for a song of mine
![Page 44: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/44.jpg)
Hello DB My Old Friend
![Page 45: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/45.jpg)
Hellow DB my old friend
I need to work with you again
That stored procedure ain’t working wellWhoever wrote that trigger should go to jail
And that index , it is slower than a snailWhat the hell? I guess it’s time…
![Page 46: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/46.jpg)
FOR VIOLENCE
![Page 47: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/47.jpg)
Man, whoever wrote this codeThat bastard’s gonna hit the road
Now the customer is gonna sueInstead of red, my face is turning blue
And it seems like there is no way out of this.
![Page 48: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/48.jpg)
There’s just a hiss.
I guess it’s time, for violence..
![Page 49: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/49.jpg)
I’m angry and I want a name
That DBA should be ashamed
![Page 50: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/50.jpg)
What’s that you’re saying?It’s ME to blame?
That database was my own sick game?
![Page 51: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/51.jpg)
Oh that’s right.It was me who did the design
It was mine.I guess it’s time…
![Page 52: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/52.jpg)
FOR SILENCE.
![Page 53: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes](https://reader035.vdocuments.mx/reader035/viewer/2022062405/554fb526b4c9057b298b5365/html5/thumbnails/53.jpg)
Thank You
Coaching, mentoring and trainingFor team leaders, developers, architects and product owners