Unit testing


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • IntroductionsAbhinav Gujjar from
  • Only bad questions are the ones that were not asked
  • Unit Testing frameworksnUnitxUnitmsTestBest PractisesIsolation Refactoring
  • The job is to deliver a software product, not focus on roles.Especially in Agile developmentCosts 500% more to fix bugs in production than it does before leaving the developers code“Inspection at the point of creation”
  • Functional testing is done by the QA teamThey are trying to cover user related functionalities (Similar to switching on the light)They may be looking at negative test scenarios. But they can never replicate all the circumstancesAcceptance testing is usually done by the clients.Most of the time is spent in developmentThe quicker the bugs are caught, the cheaper it is to fix themSometimes the testing phase is already too late for bugsFunctional testing
  • How will you send the input ? Change the visual studio startup optionsGo to command line and pass in the parametersHow will you check the output ?Debug modeOR console.writelineWhat about multiple tests ?All the test scenario information is lost and thus not repeatable
  • Unit testing frameworks are available for almost every popular language.nUnit – ported from javas junit. GrandfatherxUnit – Newcomer. Different implementation. Open SourcemsTest – integrated into visual studioMost of these will have a command line runner.Demonstrate with diagram how this might work
  • Should be a separate project – to ensure that test are not deployed Also – more importantly to ensure that we are testing only the public interfaces of the targetTest Runner – will pass or failCan debug into the testsOne test per project, or a single test project for Always require the reference to a domain project + reference to the unit testing framework
  • Separate test project because you may not want to ship the testing code along with
  • Keep things simpleOnly add functionality that is required to make the test passCreating a failing testMake the test passRefactor – Improve the internal implementationRefactoring is made a lot more reliable. You will find a way to write code in a manner that makes it testable
  • Repeatable – for instance based on a time factorRun them in any order
  • Catching exceptions
  • Dummies are objects that the SUT won’t be using them in a meaningful wayStubs are empty classes or objectsFakes may contain a little more complex implementationSpies
  • Unit testing

    1. 1. Unit Testing Abhinav Gujjar @abhinavgujjar
    2. 2. RULES! Mobiles on Silent mode Ask Ask Ask again
    3. 3. Agenda What is Unit Testing Why unit test ? Unit Testing Frameworks for .Net Best Practices for unit testing Test Driven Development (TDD) Unit testing in Agile
    4. 4. Urgh – Not my job I’m a Developer. I don’t test. I pay testers for testing
    5. 5. In The Real World
    6. 6. In The Real World
    7. 7. Different Types of Testing Unit Testing Development Functional Testing Testing Acceptance Testing Deployment Integration Testing
    8. 8. DEMO Simple Interest Calculation
    9. 9. What? You are already doing it … But not correctly Prove the correctness of a small isolated piece of functionality in a larger system
    10. 10. Why? Higher Quality Better Code Documentation
    11. 11. How? Unit Testing Frameworks .Net nUnit xUnit msTest
    12. 12. Demo Creating unit tests
    13. 13. Anatomy Arrange Unit Test Framework Unit Tests Act Assert
    14. 14. Red-Green Refactor Cycle Red Refactor Green
    15. 15. One Assert per test One logical assertion per test May need more than one Assert statement
    16. 16. Qualities of a Unit Test Repeatable Independent
    17. 17. Exercise Writing Tests
    18. 18. Testing the Sad Path
    19. 19. Assertions ! Simple Assertions Collection Asserts String Asserts
    20. 20. Test Setup and Tear Downs Assembly Class Test
    21. 21. Isolating Code
    22. 22. Isolation Techniques Dummies Stubs Fake – Maintain State Spy – Maintains state and is available for assertion Mock – Complex behavior
    23. 23. Mocking Frameworks Moq RhinoMocks nMock
    24. 24. Silverlight Unit Testing Tests are run on the browser Moq available! Yay ! But - Only if you use the MVVM pattern View Model First Approach
    25. 25. Refactoring When is done done ? Code may be correct But is it healthy?  Readability  Maintainability  What about performance ?  Technical Debt
    26. 26. Code Smells  Duplicated Code  Long Method  Long Parameter List  Large Class  Comments !!  Switch statements  Dead Code … …
    27. 27. Testing Legacy Code Why? When? How?
    28. 28. Happy Unit Testing! Blog: www.rubiksquare.com Discussion: http://www.rubiksquare.com/p/discuss.html LinkedIn : http://www.linkedin.com/in/abhinavgujjar mail: Abhinavgujjar@gmail.com