Download - Unit Testing and Test-Driven Development
![Page 1: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/1.jpg)
Unit Testing and Test-Driven
DevelopmentWhat is Unit Testing? How TDD Works?
Tsvyatko KonovTelerik
Corporationwww.telerik.com
![Page 2: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/2.jpg)
So what’s a unit test?
![Page 3: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/3.jpg)
A Unit Test is a testof a small functional piece of
code
Public bool IsLoginOK(string user, string password)
{
//…………………………
}
![Page 4: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/4.jpg)
Public bool IsLoginOK(string user, string password)
{
//…………………………
}
Should return true when… Should return false when…
Possible tests
![Page 5: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/5.jpg)
Unit Testing makes your developer lives easier
Easier to find bugs
Easier to maintain
Easier to understand
Easier to Develop
source flickr.com
![Page 6: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/6.jpg)
You have already done Unit testing
Not structured Not Repeatable Not on all your code Not easy to do as it should be
A framework is missing
![Page 7: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/7.jpg)
The xUnit Frameworks
Original was for SmallTalk Kent Beck and Erich Gamma
Ported to Various languages and platforms
JUnit, CppUnit, DUnit, VBUnit, RUnit, PyUnit, Sunit, HtmlUnit, …
Good list at www.xprogramming.com
Standard test architecture
![Page 8: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/8.jpg)
How we use the framework
Write Tests Make it easy to create and organize tests Reference an assembly, spread some attributes, you’re
done
Run Tests Allow running all of our tests, a group or just one. From command line or GUI
Review Results Immediate Pass/Fail feedback Details on each failure
![Page 9: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/9.jpg)
.NET unit Test Frameworks
NUnit XUnit MbUnit Team System Unit Testing
![Page 10: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/10.jpg)
NUnit
![Page 11: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/11.jpg)
Team System Unit Testing
![Page 12: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/12.jpg)
The 3A Pattern
Arrange all necessary preconditions and inputs.
Act on the object or method under test.
Assert that the expected results have occurred.[TestClass]
public class BankAccountTests() {
[TestMethod] public void WhenDeposit125And25_ThenTheBalance_ShouldBe150() {
BanckAccount account = new BanckAccount();
account.Deposit(125.0); account.Deposit(25.0);
Assert.AreEqual(150.0, account.Balance, "Balance is wrong.");
} }
![Page 13: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/13.jpg)
Attributes
TestClass TestMethod
TestInitialize TestCleanup
ExpectedException
Ignore Description
![Page 14: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/14.jpg)
What is Test Driven Development?
TDD = Test First Development + Refactoring
Make it Fail No code without a failing test
Make it Work As simply as possible
Make it Better Refactor
![Page 15: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/15.jpg)
Development Cycle
make a little change (refactor)
add a test
run the tests
run the tests
[Pass]
[Fail]
[Fail]
[Development continues]
[Development stops]
![Page 16: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/16.jpg)
DemoTDD + Code Coverage
![Page 17: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/17.jpg)
Test Driven Development
TDD = Test First Development + Refactoring
But what are the advantages?
![Page 18: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/18.jpg)
What are the advantages of TDD?
TDD shortens the programming feedback loop
TDD provides detailed specification (tests)
TDD promotes the development of high-quality code
TDD provides concrete evidence that your software works
![Page 19: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/19.jpg)
What are the advantages of TDD?
(a) Traditional waterfall development process
(b) Evolutionary development process (EVO).
![Page 20: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/20.jpg)
TDD “speaks” to programmers TDD provides very finely grained concrete feedback on the order of minutes
TDD helps to ensure that your design is clean by focusing on creation of operations that are callable and testable
TDD supports evolutionary development.
What are the advantages of TDD?
![Page 21: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/21.jpg)
Case Study: TDD Concerns
Defect Rate Long term and short term affect
Productivity Lines of code per month impact
Test Frequency Ratio of interactive to automated
Design Design robustness
Integration Smoothness of code integration
![Page 22: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/22.jpg)
Case Study: TDD Results
Defect Rate 50% improvement
Productivity Below initial estimates (<400 LOC)
Test Frequency 86% of tests were automated
Design Aided in late changes
Integration Testing made problems surface early
![Page 23: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/23.jpg)
Case Study: TDD Results(2)
Results from Microsoft Study Tested 4 products
Pre-release defect density reduces - 40% to 90%
Increase in initial development time - 15 – 35%
![Page 24: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/24.jpg)
Why write the test before the code?
Think through the requirement Think about the design and
usability of the API There’s never time to write it
afterwards
You write much less tests (if at all) otherwise
![Page 25: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/25.jpg)
Why make it fail first? Make sure the test does not have
a bug Make sure you’re testing the
right thing
Define a small problem to solve When the test passes, you are
done
If you can’t write that test, you may Not understand the requirement Have over designed things (not
testable) Not have a small enough problem
to solve
![Page 26: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/26.jpg)
Why Refactor?
Constantly improve the design of the code
Unit tests act as safety net Remove duplication, improve
readability and maintainability
You’ll need to when things change (requirements, your understanding of them, other factors..)
![Page 27: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/27.jpg)
Real World
It’s not easy to learn Dealing with legacy code Requires a lot of discipline You have to want to do it But once you do…
It gets easier and easier It’s a hard habit to let go
![Page 28: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/28.jpg)
Pillars of good unit tests
Making your tests trustworthy Creating maintainable tests Readable tests – last but not
least!
![Page 29: Unit Testing and Test-Driven Development](https://reader035.vdocuments.us/reader035/viewer/2022062323/56815c5e550346895dca64ca/html5/thumbnails/29.jpg)
Questions?