SlideShare a Scribd company logo
Intro to unit testing

     Steven Casey
     twitter.com/stevencasey
An intro to a test of a unit
Obligatory wikipedia quote: “unit testing is a
method by which individual units of source code,
sets of one or more computer program modules
together with associated control data, usage
procedures, and operating procedures, are
tested to determine if they are fit for use”

(emphasis mine)
http://en.wikipedia.org/wiki/Unit_testing
What to test
everything? nothing? what I feel like?

Its a tough decision to decide where to draw the
line between the obvious safety net of unit tests
and the cost of writing & maintaining those unit
tests.
• Lots of tests, will cost more to maintain over
  time and always have the potential to create
  almost as many problems as they solve.
• BUT…
• A lack of tests, doesn’t give you confidence to
  work with the glorious safety net that really
  good test coverage provides.
• and, more importantly, quality assurance &
  rapid feedback
Tightrope?
• Absolutely. One that requires discipline. Tests
  should be first-class code citizens.

• Otherwise they can become useless, ignored,
  a dead pool of code that no-one wants to
  maintain. Then you have a new kind of legacy
  codebase… sad face
So, what to test?
Use the force? Guess?
Or, you can rely on a mix of complexity analysis (get yer
cyclomatic complexity analysis ON), branches & iterations are
candidates here

Maybe, also considering edge-cases/boundary conditions for
core business functions (xunit/mbunit for iterative testing
rocks!)

Or, if you have a legacy codebase. What are the hot classes
always changing? Wouldn’t it be nice if you could wrap those
in tests and optimize that code with confidence!
What not to test?
What doesn’t add value?
- Code that never changes (or least likely to…)
  AND isn’t broken (yet)
- Code that cannot be mocked/faked. EG calling
  an API.
- Non-public code! Only test the public
  interface of your class under test, not the
  internal implementation.
What not to test
• Globals, statics, persistent data (DB, IO),
  distributed data (memcache)

• You will quickly find yourself thinking, how can I
  test this and (hopefully) deciding to avoid globals
  & statics as much as possible.

• Does your test connect to a shared db? Ok, stop!
  You cannot guarantee a repeatable isolated test
  there can you? Stay tuned for details on
  mocks/fakes/stubs!
How to structure a test?
Arrange Act Assert: a sample…

[TestMethod]
public void RegisterWithValidInputs_SendsEmailForVerification()
{
  // Arrange
  var register = _sut.RegisterNotifications;

    // Act
    var result = _sut.Index(new RegisterUserViewModel(){ Password = "password",
                 Email = "john@smith.com", FirstName = "john", LastName = "smith“ })
                 as ViewResult;

    // Assert
    register.ReceivedWithAnyArgs().SendUserActivationActionEmail(null);
}
RR Record-Replay
• Record-Replay: for me, mind bending. I much
  prefer AAA and just about every OSS project I
  see does too. So, I will not cover it here.

• For more, read:
  http://ayende.com/wiki/Rhino+Mocks+Record
  -playback+Syntax.ashx
Test structure, syntax
• Follow a naming convention for tests.
• A Suggestion is that you first name the method
  you are testing, the inputs, and the expected
  result. Be descriptive over shorthand, these
  descriptions will help identify quickly what test is
  failing when you have several hundred (or
  thousand) tests.
• Alternative conventions exist:
   – GivenWhenThen syntax (my favourite approach for
     BDD-style tests)
Test Setup
Avoid IOC. Testing your code is easier without
registration logic.
Use a mocking framework only for non-trivial
dependencies. I recommend Nsubstitute!
The testing framework usually has 2 types. One for
class level and one method level which runs
before/after every test.
Use the method setup and teardown methods for
creating and destroying the system(class) under
test, to ensure a clean SUT for each test & keep the
repetitive code out of your tests.
Some tips on writing tests…
• Change code first, then tests. If u change both
  at once, well, how do u know which is broken!
• Test’s should be independent of one another
• Test one thing (behaviour) per test. (yes, its ok
  to have multiple asserts, as long as they all
  test the same behaviour)
• No conditional logic, no loops, no exception
  catching (unless its an expected exception)
• No test logic in production code
more tips…
• Don’t create methods/props in production
  code only for use by tests (can you smell the
  bad design?)
• 1 test class = 1 class under test (don’t mix the
  classes you are testing, it gets confusing
  quickly!)
Mocks, fakes, stubs, huh?
• Stub: replaces implementation with code that returns a
  specific result. No asserts on stub.

• Mock: all about asserting that something has been called,
  perhaps with particular params

• Fake: anything that is not a ‘real’ implementation. You may
  create a fake to validate behaviours of SUT or to return
  specific results.

• http://www.martinfowler.com/articles/mocksArentStubs.ht
  ml
A test of sorts…
So finally, all your tests pass, w00t! But, someone
finds a bug. What should your next steps be?

If you said/thought…
write a test to repro the bug and then fix the test.

Well sir   (or madam)   , I think you got it! 
Fin
Initially written & presented 2010 @ communityengine. later adapted
& posted for posterity here to have a nice reference for hopefully
encouraging others to improve their approach to testing.

Steven Casey. twitter.com/stevencasey. feedback encouraged! 



References:
• http://blog.stevensanderson.com/2009/08/24/writing-great-unit-
   tests-best-and-worst-practises/
• http://www.slideshare.net/nickokiss/unit-testing-best-practices
• Art of Unit Testing by Roy Osherove (read it!)

More Related Content

PPTX
Roy Osherove TDD From Scratch
PDF
Unit Testing Done Right
PPT
Unit Testing
PDF
Unit testing (workshop)
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Unit Testing
PPTX
Testing & should i do it
PDF
Unit Testing Best Practices
Roy Osherove TDD From Scratch
Unit Testing Done Right
Unit Testing
Unit testing (workshop)
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Unit Testing
Testing & should i do it
Unit Testing Best Practices

What's hot (20)

PDF
Unit Test + Functional Programming = Love
PPTX
Automation test
PDF
Unit Testing Fundamentals
PPTX
Unit testing - the hard parts
PPTX
Mock driven development using .NET
PPTX
Advanced unit testing – real life examples and mistakes
PPTX
Real Life Unit Testing
PPTX
An Introduction to Unit Testing
PPTX
Practical unit testing tips
PDF
Clean Unit Test Patterns
PDF
Is this how you hate unit testing?
PPTX
Type mock isolator
PPTX
Moq presentation
PPTX
Understanding Unit Testing
PDF
Adding Unit Test To Legacy Code
PPTX
Unit Testing Concepts and Best Practices
PPTX
Benefit From Unit Testing In The Real World
PPTX
Unit Testing
PPTX
Unit Testing And Mocking
PDF
Unit Testing 101
Unit Test + Functional Programming = Love
Automation test
Unit Testing Fundamentals
Unit testing - the hard parts
Mock driven development using .NET
Advanced unit testing – real life examples and mistakes
Real Life Unit Testing
An Introduction to Unit Testing
Practical unit testing tips
Clean Unit Test Patterns
Is this how you hate unit testing?
Type mock isolator
Moq presentation
Understanding Unit Testing
Adding Unit Test To Legacy Code
Unit Testing Concepts and Best Practices
Benefit From Unit Testing In The Real World
Unit Testing
Unit Testing And Mocking
Unit Testing 101
Ad

Similar to An Introduction to unit testing (20)

PPTX
TDD Best Practices
PPTX
Unit testing
PPTX
Testing 101
PPTX
Unit Testing and TDD 2017
PPTX
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
PDF
iOS Test-Driven Development
PDF
TDD super mondays-june-2014
PDF
Introduzione allo Unit Testing
PDF
SELJE_Database_Unit_Testing_Slides.pdf
PPS
Unit Testing
PPT
Test Driven
KEY
Testing w-mocks
PPTX
Automated Testing but like for PowerShell (April 2012)
PPTX
Unit Testing Full@
PDF
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
PPTX
Finding a good development partner
PPTX
Building unit tests correctly with visual studio 2013
PDF
TDD Workshop UTN 2012
PPTX
Building unit tests correctly
PPTX
Creating testing tools to support development
TDD Best Practices
Unit testing
Testing 101
Unit Testing and TDD 2017
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
iOS Test-Driven Development
TDD super mondays-june-2014
Introduzione allo Unit Testing
SELJE_Database_Unit_Testing_Slides.pdf
Unit Testing
Test Driven
Testing w-mocks
Automated Testing but like for PowerShell (April 2012)
Unit Testing Full@
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Finding a good development partner
Building unit tests correctly with visual studio 2013
TDD Workshop UTN 2012
Building unit tests correctly
Creating testing tools to support development
Ad

An Introduction to unit testing

  • 1. Intro to unit testing Steven Casey twitter.com/stevencasey
  • 2. An intro to a test of a unit Obligatory wikipedia quote: “unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use” (emphasis mine) http://en.wikipedia.org/wiki/Unit_testing
  • 3. What to test everything? nothing? what I feel like? Its a tough decision to decide where to draw the line between the obvious safety net of unit tests and the cost of writing & maintaining those unit tests.
  • 4. • Lots of tests, will cost more to maintain over time and always have the potential to create almost as many problems as they solve. • BUT… • A lack of tests, doesn’t give you confidence to work with the glorious safety net that really good test coverage provides. • and, more importantly, quality assurance & rapid feedback
  • 5. Tightrope? • Absolutely. One that requires discipline. Tests should be first-class code citizens. • Otherwise they can become useless, ignored, a dead pool of code that no-one wants to maintain. Then you have a new kind of legacy codebase… sad face
  • 6. So, what to test? Use the force? Guess? Or, you can rely on a mix of complexity analysis (get yer cyclomatic complexity analysis ON), branches & iterations are candidates here Maybe, also considering edge-cases/boundary conditions for core business functions (xunit/mbunit for iterative testing rocks!) Or, if you have a legacy codebase. What are the hot classes always changing? Wouldn’t it be nice if you could wrap those in tests and optimize that code with confidence!
  • 7. What not to test? What doesn’t add value? - Code that never changes (or least likely to…) AND isn’t broken (yet) - Code that cannot be mocked/faked. EG calling an API. - Non-public code! Only test the public interface of your class under test, not the internal implementation.
  • 8. What not to test • Globals, statics, persistent data (DB, IO), distributed data (memcache) • You will quickly find yourself thinking, how can I test this and (hopefully) deciding to avoid globals & statics as much as possible. • Does your test connect to a shared db? Ok, stop! You cannot guarantee a repeatable isolated test there can you? Stay tuned for details on mocks/fakes/stubs!
  • 9. How to structure a test? Arrange Act Assert: a sample… [TestMethod] public void RegisterWithValidInputs_SendsEmailForVerification() { // Arrange var register = _sut.RegisterNotifications; // Act var result = _sut.Index(new RegisterUserViewModel(){ Password = "password", Email = "john@smith.com", FirstName = "john", LastName = "smith“ }) as ViewResult; // Assert register.ReceivedWithAnyArgs().SendUserActivationActionEmail(null); }
  • 10. RR Record-Replay • Record-Replay: for me, mind bending. I much prefer AAA and just about every OSS project I see does too. So, I will not cover it here. • For more, read: http://ayende.com/wiki/Rhino+Mocks+Record -playback+Syntax.ashx
  • 11. Test structure, syntax • Follow a naming convention for tests. • A Suggestion is that you first name the method you are testing, the inputs, and the expected result. Be descriptive over shorthand, these descriptions will help identify quickly what test is failing when you have several hundred (or thousand) tests. • Alternative conventions exist: – GivenWhenThen syntax (my favourite approach for BDD-style tests)
  • 12. Test Setup Avoid IOC. Testing your code is easier without registration logic. Use a mocking framework only for non-trivial dependencies. I recommend Nsubstitute! The testing framework usually has 2 types. One for class level and one method level which runs before/after every test. Use the method setup and teardown methods for creating and destroying the system(class) under test, to ensure a clean SUT for each test & keep the repetitive code out of your tests.
  • 13. Some tips on writing tests… • Change code first, then tests. If u change both at once, well, how do u know which is broken! • Test’s should be independent of one another • Test one thing (behaviour) per test. (yes, its ok to have multiple asserts, as long as they all test the same behaviour) • No conditional logic, no loops, no exception catching (unless its an expected exception) • No test logic in production code
  • 14. more tips… • Don’t create methods/props in production code only for use by tests (can you smell the bad design?) • 1 test class = 1 class under test (don’t mix the classes you are testing, it gets confusing quickly!)
  • 15. Mocks, fakes, stubs, huh? • Stub: replaces implementation with code that returns a specific result. No asserts on stub. • Mock: all about asserting that something has been called, perhaps with particular params • Fake: anything that is not a ‘real’ implementation. You may create a fake to validate behaviours of SUT or to return specific results. • http://www.martinfowler.com/articles/mocksArentStubs.ht ml
  • 16. A test of sorts… So finally, all your tests pass, w00t! But, someone finds a bug. What should your next steps be? If you said/thought… write a test to repro the bug and then fix the test. Well sir (or madam) , I think you got it! 
  • 17. Fin Initially written & presented 2010 @ communityengine. later adapted & posted for posterity here to have a nice reference for hopefully encouraging others to improve their approach to testing. Steven Casey. twitter.com/stevencasey. feedback encouraged!  References: • http://blog.stevensanderson.com/2009/08/24/writing-great-unit- tests-best-and-worst-practises/ • http://www.slideshare.net/nickokiss/unit-testing-best-practices • Art of Unit Testing by Roy Osherove (read it!)