5. Sounds Familiar?
20:00 – One bug left before release
20:01 - Done!
20:02 - Send version to QA, call wife
20:05 - On the way to the elevator.
QA: It doesn’t work!
22:50 - What have I done?!
Typemock™
6. Unit testing for Fun & Profit
Short Overview: Unit Test, TDD
Why unit test?
Things you thought you knew
Tools you need to know about
Typemock™
7. Overview
What is Unit Test?
Verifies an atomic piece of code
Tests one specific behavior
Each Test is autonomous
[TestMethod]
public void CheckPassword_ValidUser_ReturnTrue()
{
bool result = CheckPassword(“user”, “pass”);
Assert.IsTrue(result);
}
Typemock™
9. Why Unit Test?
Why not use Unit Test
Testing is for QA
Time = Cost
Integration Testing find more bugs
Manual Testing find more bugs
It’s like writing the code twice
Typemock™
10. Why Unit Test?
Is TDD a Waste of Time?
Time taken to code a feature
140% 135%
120% 125%
120% 115%
100%
80%
60%
40%
20%
0%
IBM: Drivers MS: Windows MS: MSN MS: VS
WithoutTDD Using TDD
Typemock™
11. Why Unit Test?
Is TDD a Waste of Time?
Using Test Driven Design
140%
120%
100%
80%
61%
60%
38%
40%
24%
20% 9%
0%
IBM: Drivers MS: Windows MS: MSN MS: VS
Major quality improvement for minor time investment
Time To Code Feature Defect density of team
Typemock™
12. Why Unit Test?
So Why Unit Test?
Where does it hurt?
100% 10
The pain is here! This is too late… 9
80% 8
% defects created
7
Thousand $s
60% 6
5
40% 4
3
20% 2
1
0% 0
Requirements Coding Integration Testing
Typemock™
Support
% of Defects Introduced Cost to Fix a Defect
13. Misconceptions
TDD is not about testing
TDD is about design and development
By testing first you design your code
Typemock™
14. Misconceptions
Unit Tests are written by
developers!
Increase confidence in code
Fearlessly change your code
Document requirements
Discover usability issues early
Typemock™
15. Misconceptions
Unit Tests are Not
Integration
Unit tests should be:
Small
Atomic
Test a single functional unit
Isolated!
Integration tests are used to test system
wide functionality
Typemock™
16. Tools
Unit Testing Frameworks
Consensus – NUnit
Integrated – MSTest
New kid on the block – XUnit
Performance oriented – Zenebug
Meta framework- Gallio (MBUnit)
Typemock™
17. Tools
Mocking / Isolation
Each unit test MUST be isolated
How do we test business logic without
Database calls?
Typemock™
20. Tools
Continuous Integration Build Server
(TeamCity)
What’s new?
Commit
There you go
Source Control
(SVN)
We automatically got
•Error reports & logs
•New version installer
•Help files
•More…
Build Agents Typemock™
(FinalBuilder)
21. Tools
Continuous Integration
Computer
Says NO!
Build Automation Tools
NAnt
MSBuild
FinalBuilder
CI Servers
CCnet
TeamCity
Team System / Team Foundation Server
FinalBuilder Server
Typemock™
28. Our Tools
The Tools We Use
Unit testing – MSTest/NUnit
Build automation – FinalBuilder
Continuous Integration – TeamCity
Integrated tools – R#, TestDriven.NET
Isolation Framework – Isolator (surprise!)
Build Bunny!
Typemock™
29. Write Code
Writing Some Code
We begin with a clean slate
Write
new test
Run
Refactor
tests
Run All Write
tests code
Typemock™
30. Write Code
Writing Some Code
An exercise in futility…
Write
new test
Run
Refactor
tests
Run all Write
tests code
Typemock™
31. Write Code
Writing Some Code
Now we get our hands dirty
Write
new
Test
Run
Refactor
tests
Run all Write
tests code
Typemock™
32. Write Code
Writing Some Code
Make sure everything’s fine…
Write
new
Test
Run
Refactor
tests
Run all Write
tests code
Typemock™
33. Write Code
Writing Some Code
… and make it perform/look better
Write
new test
Run
Refactor
tests
Run all Write
tests code
Typemock™
34. Write Code
Writing Some Code
Lets take her out for another spin…
Write
new
Test
Run
Refactor
tests
Run all Write
tests code
Typemock™
35. Broken Build
OK, Who Broke the Build?
Something went horribly wrong!
And it’s easy to find who to blame
But it’s easy to find out what happened
Why is this important?
The project heartbeat
Healthy build == easy to release
Typemock™
36. Spare Time
Too Much Spare Time
The foosball table
The build bunny
The Shooting of The Zombies
Helping children with computer skills
Typemock™
38. Examples from the real
world
Hard to unit test scenarios
How to solve this problem
Examples
Typemock™
39. Difficult scenarios to Unit
Test
Client – server architecture
Databases
Legacy Code
Closed Object Models
(Sharepoint, Silverlight)
GUI interaction
Typemock™
40. Possible solutions
First - better understand the problem
Design for Testability
Refactoring for Testability
Reflection
Mocks / Isolation
Last resort - Integration tests instead
Typemock™
41. Example Unit Testing WCF
Client
Class: WCF Client
Method: string GetSourceList(string source);
Problem: Server connection is not possible
Typemock™
45. Thank you
Want to learn more?
Read about Test Driven Development &
Isolation at the Typemock Insiders blog
Follow up on twitter.com/Typemock
Take a card and drop us an email!
Typemock™
Notes de l'éditeur
The lecture today will be divided into four parts:1 Overview of Unit tests and TDD and how it could save money for companies and improve software development process2 Typical day (but nor all day) of a software developer that uses TDD3 Real examples of Unit testing in non trivial cases4 Answer questions from the audience about the lecture, TDD & Unit testing in their companies
How a software company can benefit from unit testing Question to audience:Who knows what are unit tests?Who uses unit tests?Who practices TDD?
Why unit test:Define unit testing, define isolationMotivationWhy not do unit testing?Benefits: regression, confidence in changeStatistics Examples of companies that unit testCommon misconceptions:Not all tests are unit tests (integration vs. unit testing)Unit tests are for developers, not QAIntroducing TDDTools:Unit testing frameworksR#Mocking frameworksTeamCity/Team SystemFinalBuilder/msbuild/ccnet
Show unit test parts:Declaration (attribute)Run code under testAssertion – did satisfy condition Usually we have an additional part that arrange the needed componentsShould this be a new slide? What does unit test test?Functional requirememts
First explain - Difference between unit testing – how and tdd - methodologyTest Driven Development (or test driven design) is a methodology.Write a test that failsRun tests – why should we – to make sure the test actually checks the right thingWrite codeRefactorRepeat if nessesary
No that we understand what is unit testing and TDD I’d like
Taken from http://research.microsoft.com/en-us/projects/esm/nagappan_tdd.pdfRealizing quality improvement through test driven development: results and experiences of four industrial teamsPublished online: 27 February 2008
- One known fact is that most of the bugs are introduced during the development stage.Another known fact is that the cost to fix a defect increases exponentially over stages.When bug is found at Testing phase the application needs to go back to development and it actually takes more time!It would be better to find/fix/not create bugs during development afterward it’s too late.We get a costly fix and frustrated clients.Show bugs in testing/supprt – where did they come from? Every few defects fixed a new defect is introducedOur pain in at the dev stage however many companies invest most of their money on QA & support
Give example about massive refactoring of new code.Function/code afraid to touch…
Examples DB, WCF
Has Teardown/Setup functionalityRun Unit tests
How to test part of a whole system
The backbone of TDD (and development)Show how CI & script runner run the test suite – perhaps publish results to web
Continuous integration is the backbone of TDD. Without test runner that runs each time code changes it is impossible to make sure that nothing was broken.