SlideShare a Scribd company logo
@RoyOsherove
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
Real World Agenda
 Test Reviews

 Making your tests TRUSTworthy
 Creating MAINTAINable tests
 READable tests
                                 RTFM
Test review vs. code review
 Understand intent of developer
 10 times quicker
 Drill in when needed

 Important for learning teams
 Helps drive change
Readable             Maintainable




           Trustworthy
Make Your tests trust worthy
 Make it easy to run
 Removing or changing tests
 Assuring code coverage
 Avoid test logic
 Avoid Manual StubMock Logic
 Don’t Repeat Production Logic
Separate Unit From Integration Tests
Code coverage?
 Worthless without a code review
 Change production code and see what happens

 Make params into consts
 Remove “if” checks – or make into consts

 If all tests pass - something is missing or something is
 not needed
Avoid test logic (MS Unity)
Another logic example:
MVCDev.HtmlHelperTest.GetHttpContext()
Don’t repeat production logic
  String user = “a”;
  String password= “b”;

 String SomeResult =
 UnderTest.CreateMessage(user,password);

  Assert.AreEqual( user + “,” + password,
                  SomeResult);
Don't use things that keep changing
 DateTime.Now
 Random
 Environment.TickCount
 Threads
 Etc..
Tests that keep changing:
NerdDinner
Creating maintainable tests
 Avoid testing private/protected members
 Re-use test code (Create, Manipulate, Assert)
 Enforce test isolation
 Test One Thing
    Avoid Multiple Asserts
    One mock per test
 Use “relaxed” or “Non strict” mocks and stubs
Test only publics
 “Unit” testing == “Unit Of Work Testing”
 Testing a private makes your test more brittle
 Makes you think about the design and usability
 of a feature
 Test-First leads to private members after
 Refactoring, but they are all tested!

 But sometimes there’s not choice
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]
Enforce test isolation
 No dependency between tests!
 Don’t run a test from another test!
 Run alone
 or all together
 in any order
 Otherwise – leads to nasty “what was that?”
 bugs
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);
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Avoid Multiple Asserts
MVCDev.LinkExtensionsTest L. 334
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Avoid Multiple Asserts
 Like having multiple tests
 But the first the fails – kills the others
 You won’t get the big picture (some symptoms)
 Hard to name
 Can lead to more debugging work
Avoid Multiple Mocks
Creating readable tests
 Structure
 No Magic values
    In test
    In mockstub
 Naming a unit test
 Naming variables
 Separate Assert from action
Structure
 Are the tests easy to find?
 Can you find a test for a
 class, or a method?
Setup Mystery
Bad Naming
No Magic Values
 Assert.AreEqual(1003, calc.Parse(“-1”));


 Int parseResult =
 calc.Parse(NEGATIVE_ILLEGAL_NUMBER);

 Assert.AreEqual(
   NEGATIVE_PARSE_RETURN_CODE,
   parseResult)
Hidden Values
Magic values in Fakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Magic Values
Unity InjectionMethodFixture
Call it what it is (mockstubfake)
 Most “Mocks” are actually stubs.
 If it can be both, call it “Fake”
Naming a unit test
 Method name
 State under test
 Expected behavior/return value

 Add_LessThanZero_ThrowsException()
 Another angle:
 Add_LessThanZero_ThrowsException2()
Naming
Separate Assert from Action


 Assert is less cluttered
 More readable
 Allows handling the return value if needed
 It’s all about readability
Input Values Should Differ
 X.Divide (1,1)
 X.Divide (1,2)

 X.Check(“1,1”)
 X.Check(“1,2”)
Readable             Maintainable




           Trustworthy
ArtOfUnitTesting.com
Song?
This is a test line
Looks like you’re doing fine
Time for a song of mine
Hello DB My Old Friend
Hellow DB my old friend
 I need to work with you again

 That stored procedure ain’t working well
 Whoever wrote that trigger should go to jail

 And that index , it is slower than a snail
 What the hell?
 I guess it’s time…
FOR VIOLENCE
Man, whoever wrote this code
That bastard’s gonna hit the road

Now the customer is gonna sue
Instead of red, my face is turning blue

And it seems like there is no way out of this.
There’s just a hiss.

I guess it’s time, for violence..
I’m angry and I want a name

That DBA should be ashamed
What’s that you’re saying?
It’s ME to blame?

That database was my own sick game?
Oh that’s right.
It was me who did the design

It was mine.
I guess it’s time…
FOR SILENCE.
Thank You

 royo@bouvet.no

 Coaching, mentoring and training
 For team leaders, developers, architects and
 product owners

More Related Content

What's hot (20)

PPT
Cucumber presentation
Akhila B
 
PDF
Effective testing with pytest
Hector Canto
 
PPTX
Karate for Complex Web-Service API Testing by Peter Thomas
intuit_india
 
PPTX
BDD Approach with Karate Framework in Service Tests
kloia
 
PDF
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
PPTX
Unit Testing
Sergey Podolsky
 
PDF
Unit testing best practices
nickokiss
 
PPTX
Unit Testing Using N Unit
Gaurav Arora
 
PDF
Gradle Introduction
Dmitry Buzdin
 
PPTX
Dependency injection presentation
Ahasanul Kalam Akib
 
PPTX
An Introduction to Unit Testing
Joe Tremblay
 
PDF
Test Driven Development (TDD)
David Ehringer
 
PPTX
Automation test framework with cucumber – BDD
123abcda
 
PPT
Hybrid framework
Sudhakar Mangi
 
PPTX
Test automation with Cucumber-JVM
Alan Parkinson
 
PPTX
Introduce Katalon tool
재연 김
 
PDF
Karate - Web-Service API Testing Made Simple
VodqaBLR
 
PDF
Clean Architecture Essentials - Stockholm Software Craftsmanship
Ivan Paulovich
 
PDF
How We Test MongoDB: Evergreen
MongoDB
 
PPTX
Cucumber presenation
Oussama BEN WAFI
 
Cucumber presentation
Akhila B
 
Effective testing with pytest
Hector Canto
 
Karate for Complex Web-Service API Testing by Peter Thomas
intuit_india
 
BDD Approach with Karate Framework in Service Tests
kloia
 
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
Unit Testing
Sergey Podolsky
 
Unit testing best practices
nickokiss
 
Unit Testing Using N Unit
Gaurav Arora
 
Gradle Introduction
Dmitry Buzdin
 
Dependency injection presentation
Ahasanul Kalam Akib
 
An Introduction to Unit Testing
Joe Tremblay
 
Test Driven Development (TDD)
David Ehringer
 
Automation test framework with cucumber – BDD
123abcda
 
Hybrid framework
Sudhakar Mangi
 
Test automation with Cucumber-JVM
Alan Parkinson
 
Introduce Katalon tool
재연 김
 
Karate - Web-Service API Testing Made Simple
VodqaBLR
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Ivan Paulovich
 
How We Test MongoDB: Evergreen
MongoDB
 
Cucumber presenation
Oussama BEN WAFI
 

Viewers also liked (7)

PPTX
A second look at Unit Testing with Roy Osherove at Microsoft Swit
Roy Osherove
 
PPTX
Organizational influence-hacks-45min
Roy Osherove
 
PPTX
Vim - for newbies, by Roy Osherove
Roy Osherove
 
PPTX
Roy Osherove TDD From Scratch
Roy Osherove
 
PPTX
Team leadership in the age of Agile - Roy Osherove
Roy Osherove
 
PPTX
Building out a Microservices Architecture with WebSphere Liberty Profile and ...
David Currie
 
PDF
Robot Framework Dos And Don'ts
Pekka Klärck
 
A second look at Unit Testing with Roy Osherove at Microsoft Swit
Roy Osherove
 
Organizational influence-hacks-45min
Roy Osherove
 
Vim - for newbies, by Roy Osherove
Roy Osherove
 
Roy Osherove TDD From Scratch
Roy Osherove
 
Team leadership in the age of Agile - Roy Osherove
Roy Osherove
 
Building out a Microservices Architecture with WebSphere Liberty Profile and ...
David Currie
 
Robot Framework Dos And Don'ts
Pekka Klärck
 
Ad

Similar to Roy Osherove on Unit Testing Good Practices and Horrible Mistakes (20)

PPTX
Think Agile - Practice TDD
vijay sylvester
 
PPTX
Dev204 Osherove
vijay sylvester
 
PPTX
TDD Best Practices
Attila Bertók
 
PPTX
Unit testing
PiXeL16
 
PPTX
Building unit tests correctly with visual studio 2013
Dror Helper
 
PDF
Writing Tests Effectively
Paul Boocock
 
PDF
Workshop unit test
Francesco Garavaglia
 
PPTX
Building unit tests correctly
Dror Helper
 
PPTX
Rc2010 tdd
JasonOffutt
 
PPTX
An Introduction to unit testing
Steven Casey
 
PDF
SELJE_Database_Unit_Testing.pdf
Eric Selje
 
PPTX
Unit testing
Panos Pnevmatikatos
 
PPT
Automated Unit Testing and TDD
Greg Sohl
 
PDF
Unit Testing Guidelines
Joel Hooks
 
PDF
Unit testing (workshop)
Foyzul Karim
 
PDF
Unit testing - An introduction
Alejandro Claro Mosqueda
 
PDF
An Introduction to Unit Test Using NUnit
weili_at_slideshare
 
PDF
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
PDF
Unit Testing Best Practices
Tomaš Maconko
 
PPT
Unit testing
Murugesan Nataraj
 
Think Agile - Practice TDD
vijay sylvester
 
Dev204 Osherove
vijay sylvester
 
TDD Best Practices
Attila Bertók
 
Unit testing
PiXeL16
 
Building unit tests correctly with visual studio 2013
Dror Helper
 
Writing Tests Effectively
Paul Boocock
 
Workshop unit test
Francesco Garavaglia
 
Building unit tests correctly
Dror Helper
 
Rc2010 tdd
JasonOffutt
 
An Introduction to unit testing
Steven Casey
 
SELJE_Database_Unit_Testing.pdf
Eric Selje
 
Unit testing
Panos Pnevmatikatos
 
Automated Unit Testing and TDD
Greg Sohl
 
Unit Testing Guidelines
Joel Hooks
 
Unit testing (workshop)
Foyzul Karim
 
Unit testing - An introduction
Alejandro Claro Mosqueda
 
An Introduction to Unit Test Using NUnit
weili_at_slideshare
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
Unit Testing Best Practices
Tomaš Maconko
 
Unit testing
Murugesan Nataraj
 
Ad

Recently uploaded (20)

PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 

Roy Osherove on Unit Testing Good Practices and Horrible Mistakes

  • 2. 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
  • 3. Real World Agenda Test Reviews Making your tests TRUSTworthy Creating MAINTAINable tests READable tests RTFM
  • 4. Test review vs. code review Understand intent of developer 10 times quicker Drill in when needed Important for learning teams Helps drive change
  • 5. Readable Maintainable Trustworthy
  • 6. Make Your tests trust worthy Make it easy to run Removing or changing tests Assuring code coverage Avoid test logic Avoid Manual StubMock Logic Don’t Repeat Production Logic
  • 7. Separate Unit From Integration Tests
  • 8. Code coverage? Worthless without a code review Change production code and see what happens Make params into consts Remove “if” checks – or make into consts If all tests pass - something is missing or something is not needed
  • 9. Avoid test logic (MS Unity)
  • 11. Don’t repeat production logic String user = “a”; String password= “b”; String SomeResult = UnderTest.CreateMessage(user,password); Assert.AreEqual( user + “,” + password, SomeResult);
  • 12. Don't use things that keep changing DateTime.Now Random Environment.TickCount Threads Etc..
  • 13. Tests that keep changing: NerdDinner
  • 14. Creating maintainable tests Avoid testing private/protected members Re-use test code (Create, Manipulate, Assert) Enforce test isolation Test One Thing Avoid Multiple Asserts One mock per test Use “relaxed” or “Non strict” mocks and stubs
  • 15. Test only publics “Unit” testing == “Unit Of Work Testing” Testing a private makes your test more brittle Makes you think about the design and usability of a feature Test-First leads to private members after Refactoring, but they are all tested! But sometimes there’s not choice
  • 16. 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]
  • 17. Enforce test isolation No dependency between tests! Don’t run a test from another test! Run alone or all together in any order Otherwise – leads to nasty “what was that?” bugs
  • 18. 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);
  • 22. Avoid Multiple Asserts Like having multiple tests But the first the fails – kills the others You won’t get the big picture (some symptoms) Hard to name Can lead to more debugging work
  • 24. Creating readable tests Structure No Magic values In test In mockstub Naming a unit test Naming variables Separate Assert from action
  • 25. Structure Are the tests easy to find? Can you find a test for a class, or a method?
  • 28. No Magic Values Assert.AreEqual(1003, calc.Parse(“-1”)); Int parseResult = calc.Parse(NEGATIVE_ILLEGAL_NUMBER); Assert.AreEqual( NEGATIVE_PARSE_RETURN_CODE, parseResult)
  • 33. Call it what it is (mockstubfake) Most “Mocks” are actually stubs. If it can be both, call it “Fake”
  • 34. Naming a unit test Method name State under test Expected behavior/return value Add_LessThanZero_ThrowsException() Another angle: Add_LessThanZero_ThrowsException2()
  • 36. Separate Assert from Action Assert is less cluttered More readable Allows handling the return value if needed It’s all about readability
  • 37. Input Values Should Differ X.Divide (1,1) X.Divide (1,2) X.Check(“1,1”) X.Check(“1,2”)
  • 38. Readable Maintainable Trustworthy
  • 40. Song?
  • 41. This is a test line
  • 42. Looks like you’re doing fine
  • 43. Time for a song of mine
  • 44. Hello DB My Old Friend
  • 45. Hellow DB my old friend I need to work with you again That stored procedure ain’t working well Whoever wrote that trigger should go to jail And that index , it is slower than a snail What the hell? I guess it’s time…
  • 47. Man, whoever wrote this code That bastard’s gonna hit the road Now the customer is gonna sue Instead of red, my face is turning blue And it seems like there is no way out of this.
  • 48. There’s just a hiss. I guess it’s time, for violence..
  • 49. I’m angry and I want a name That DBA should be ashamed
  • 50. What’s that you’re saying? It’s ME to blame? That database was my own sick game?
  • 51. Oh that’s right. It was me who did the design It was mine. I guess it’s time…
  • 53. Thank You [email protected] Coaching, mentoring and training For team leaders, developers, architects and product owners