Unit Testing Abhinav Gujjar @abhinavgujjar
Unit Testing
Abhinav Gujjar
@abhinavgujjar
RULES!
Mobiles on Silent modeAskAskAsk again
Agenda
What is Unit TestingWhy unit test ?Unit Testing Frameworks for .NetBest Practices for unit testingTest Driven Development (TDD)Unit testing in Agile
Urgh – Not my job
I’m a Developer. I don’t test.
I pay testers for testing
In The Real World
In The Real World
Different Types of Testing
Development Testing Deployment
Unit Testing Functional Testing
Integration Testing
Acceptance Testing
DEMO
Simple Interest Calculation
What?
You are already doing it… But not correctly
Prove the correctness of a small isolated piece of functionality in a larger system
Why?
Higher QualityBetter CodeDocumentation
How?
Unit Testing Frameworks.Net
nUnit xUnitmsTest
Demo
Creating unit tests
Anatomy
Unit Test Framework
Unit Tests
Arrange
Act
Assert
Red-Green Refactor Cycle
Red
GreenRefactor
One Assert per test
One logical assertion per testMay need more than one Assert statement
Qualities of a Unit Test
RepeatableIndependent
Exercise
Writing Tests
Testing the Sad Path
Assertions !
Simple Assertions Collection AssertsString Asserts
Test Setup and Tear Downs
AssemblyClassTest
Isolating Code
Isolation Techniques
DummiesStubsFake – Maintain StateSpy – Maintains state and is available for assertionMock – Complex behavior
Mocking Frameworks
MoqRhinoMocksnMock
Silverlight Unit Testing
Tests are run on the browserMoq available! Yay !But - Only if you use the MVVM pattern
View Model First Approach
Refactoring
When is done done ?Code may be correct
But is it healthy?
Readability
Maintainability
What about performance ?
Technical Debt
Code Smells
Duplicated Code
Long Method
Long Parameter List
Large Class
Comments !!
Switch statements
Dead Code
…
…
Testing Legacy Code
Why?When?How?
Happy Unit Testing!
Blog: www.rubiksquare.com
Discussion: http://www.rubiksquare.com/p/discuss.html
LinkedIn : http://www.linkedin.com/in/abhinavgujjar
mail: [email protected]