Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

53
Unit Testing Good Practices & Horrible Mistakes @RoyOsherove

Transcript of Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Page 1: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Unit Testing Good Practices & Horrible Mistakes

@RoyOsherove

Page 2: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

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

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

Maintainable

Trustworthy

Readable

Page 6: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

Separate Unit From Integration Tests

Page 8: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

Avoid test logic (MS Unity)

Page 10: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Another logic example:MVCDev.HtmlHelperTest.GetHttpContext()

Page 11: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

Don't use things that keep changing

DateTime.NowRandomEnvironment.TickCountThreadsEtc..

Page 13: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Tests that keep changing: NerdDinner

Page 14: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

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

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

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

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
Page 20: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Avoid Multiple AssertsMVCDev.LinkExtensionsTest L. 334

Page 21: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Page 22: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

Avoid Multiple Mocks

Page 24: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

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

Setup Mystery

Page 27: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Bad Naming

Page 28: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

Hidden Values

Page 30: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Magic values in Fakes

Page 31: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Page 32: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Magic ValuesUnity InjectionMethodFixture

Page 33: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

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

Naming

Page 36: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

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

Maintainable

Trustworthy

Readable

Page 39: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

ArtOfUnitTesting.com

Page 40: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Song?

Page 41: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

This is a test line

Page 42: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Looks like you’re doing fine

Page 43: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Time for a song of mine

Page 44: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Hello DB My Old Friend

Page 45: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

FOR VIOLENCE

Page 47: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

There’s just a hiss.

I guess it’s time, for violence..

Page 49: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

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

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

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

FOR SILENCE.

Page 53: Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

Thank You

[email protected]

Coaching, mentoring and trainingFor team leaders, developers, architects and product owners