SlideShare uma empresa Scribd logo
1 de 17
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!)

Mais conteúdo relacionado

Mais procurados

Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
Automation test
Automation testAutomation test
Automation testyuyijq
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard partsShaun Abram
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NETPuneet Ghanshani
 
Advanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakesAdvanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakesMilan Vukoje
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit TestingDror Helper
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Practical unit testing tips
Practical unit testing tipsPractical unit testing tips
Practical unit testing tipsTypemock
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?Steven Mak
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
Moq presentation
Moq presentationMoq presentation
Moq presentationLynxStar
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeTerry Yin
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 

Mais procurados (20)

Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Automation test
Automation testAutomation test
Automation test
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
 
Advanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakesAdvanced unit testing – real life examples and mistakes
Advanced unit testing – real life examples and mistakes
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Practical unit testing tips
Practical unit testing tipsPractical unit testing tips
Practical unit testing tips
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
 

Semelhante a An Introduction to unit testing

Unit testing
Unit testingUnit testing
Unit testingPiXeL16
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
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
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014Alex Kavanagh
 
Introduzione allo Unit Testing
Introduzione allo Unit TestingIntroduzione allo Unit Testing
Introduzione allo Unit TestingStefano Ottaviani
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfEric Selje
 
Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Rob Reynolds
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...AgileNetwork
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partnerKevin Poorman
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Dror Helper
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
 
Creating testing tools to support development
Creating testing tools to support developmentCreating testing tools to support development
Creating testing tools to support developmentChema del Barco
 

Semelhante a An Introduction to unit testing (20)

TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Unit testing
Unit testingUnit testing
Unit testing
 
Testing 101
Testing 101Testing 101
Testing 101
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
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
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
TDD super mondays-june-2014
TDD super mondays-june-2014TDD super mondays-june-2014
TDD super mondays-june-2014
 
Introduzione allo Unit Testing
Introduzione allo Unit TestingIntroduzione allo Unit Testing
Introduzione allo Unit Testing
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdf
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Test Driven
Test DrivenTest Driven
Test Driven
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)Automated Testing but like for PowerShell (April 2012)
Automated Testing but like for PowerShell (April 2012)
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
Finding a good development partner
Finding a good development partnerFinding a good development partner
Finding a good development partner
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Creating testing tools to support development
Creating testing tools to support developmentCreating testing tools to support development
Creating testing tools to support development
 

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!)