Unit Testing

  Prashant
Before we start…
• Unit tests are “Code”
• Not efficient to test the UI
• Unit testing is not a testing activity.
Why unit testing?
• Acts as specifications for your code / business
  requirements
• Helps to produce better code (Readable /
  Maintainable)
• Make you think before you write any code
It’s good unit test, if…
•   One Unit Test Class per “Class”
•   Each test should run in isolation
•   Test should be clear
•   Single outcome per test
•   Name Tests from Users Perspective
    – Long test method names.
    – Conventions
Demo
Recommendations
•   Use Mocking frameworks
•   Mark classes and Interfaces as internal
•   When refactoring think of architecture
•   Consider splitting class into multiple files
•   Avoid using Static classes/Methods
•   UI Containing non-user logic
Continued…
• Inject dependencies and state via constructor
• Avoid classes coupled to many other classes
• Avoid classes coupled to concrete classes
  rather than interfaces
References
• Rhino Mocks
  – http://www.ayende.com/Wiki/Rhino+Mocks+3.5.
    ashx#WhatsNewinRhinoMocksDF
• Nunit
  – http://www.nunit.org
• Mocks Aren't Stubs (Martin Fowler)
  – http://martinfowler.com/articles/mocksArentStub
    s.html
Thanks…




          questions ?

Unit Testing (C#)

  • 1.
    Unit Testing Prashant
  • 2.
    Before we start… •Unit tests are “Code” • Not efficient to test the UI • Unit testing is not a testing activity.
  • 3.
    Why unit testing? •Acts as specifications for your code / business requirements • Helps to produce better code (Readable / Maintainable) • Make you think before you write any code
  • 4.
    It’s good unittest, if… • One Unit Test Class per “Class” • Each test should run in isolation • Test should be clear • Single outcome per test • Name Tests from Users Perspective – Long test method names. – Conventions
  • 5.
  • 6.
    Recommendations • Use Mocking frameworks • Mark classes and Interfaces as internal • When refactoring think of architecture • Consider splitting class into multiple files • Avoid using Static classes/Methods • UI Containing non-user logic
  • 7.
    Continued… • Inject dependenciesand state via constructor • Avoid classes coupled to many other classes • Avoid classes coupled to concrete classes rather than interfaces
  • 8.
    References • Rhino Mocks – http://www.ayende.com/Wiki/Rhino+Mocks+3.5. ashx#WhatsNewinRhinoMocksDF • Nunit – http://www.nunit.org • Mocks Aren't Stubs (Martin Fowler) – http://martinfowler.com/articles/mocksArentStub s.html
  • 9.
    Thanks… questions ?