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. Urgh – Not my job
I’m a Developer.
I don’t test.
I pay testers for
testing
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. Refactoring
When is done done ?
Code may be correct
But is it healthy?
Readability
Maintainability
What about performance ?
Technical Debt
26. Code Smells
Duplicated Code
Long Method
Long Parameter List
Large Class
Comments !!
Switch statements
Dead Code
…
…
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