13. Unit Testing and Test-Driven Development - What is Unit Testing? How TDD Works?
Upcoming SlideShare
Loading in...5

13. Unit Testing and Test-Driven Development - What is Unit Testing? How TDD Works?



High-Quality Code @ Telerik Academy...

High-Quality Code @ Telerik Academy
Telerik Software Academy: http://codecourse.telerik.com/
The website and all video materials are in Bulgarian
So what’s a unit test?
Possible tests
The xUnit Frameworks
.NET unit Test Frameworks
Team System Unit Testing
The 3A Pattern
What is Test Driven Development?
Development Cycle
Why Refactor?
Pillars of good unit tests



Total Views
Slideshare-icon Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    13. Unit Testing and Test-Driven Development - What is Unit Testing? How TDD Works? 13. Unit Testing and Test-Driven Development - What is Unit Testing? How TDD Works? Presentation Transcript

    • Unit Testing and Test- Driven Development What is Unit Testing? How TDD Works?Tsvyqtko KonovSoftware engineer,Telerik Corporation,Blog: http://tsvyatkokonov.blogspot.com/Telerik Corporationwww.telerik.com
    • So what’s a unit test?
    • A Unit Test is a test of a small functional piece of codePublic bool IsLoginOK(string user, string password){//…………………………}
    • Possible testsShould return true when… Should return false when…Public bool IsLoginOK(string user, string password){//…………………………}
    • Unit Testing makes your developer lives easier Easier to find bugs Easier to maintain Easier to understand Easier to Develop source flickr.com
    • 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
    • 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
    • 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
    • .NET unit Test Frameworks NUnit XUnit MbUnit Team System Unit Testing
    • NUnit
    • Team System Unit Testing
    • 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."); }}
    • Attributes TestClass  ExpectedException TestMethod  Ignore  Description TestInitialize TestCleanup
    • 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
    • Development Cycle add a test[Pass] run the tests [Development [Fail] continues] make a little change (refactor)[Fail] run the tests [Development stops]
    • DemoTDD + Code Coverage
    • Test Driven Development TDD = Test First Development + Refactoring But what are the advantages?
    • 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 providesconcrete evidence that your software works
    • What are the advantages of TDD? (a) Traditional waterfall development process (b) Evolutionary development process (EVO).
    • What are the advantages of TDD? 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.
    • 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
    • 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
    • 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%
    • 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
    • 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
    • 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..)
    • 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
    • Pillars of good unit tests Making your tests trustworthy Creating maintainable tests Readable tests – last but not least!
    • Questions?
    • Unit Testing and Test- Driven Development курсове и уроци по програмиране, уеб дизайн – безплатно BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране – Телерик академия форум програмиране, форум уеб дизайн уроци по програмиране и уеб дизайн за ученици ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET http://academy.telerik.com програмиране за деца – безплатни курсове и уроци ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC безплатен SEO курс - оптимизация за търсачки алго академия – състезателно програмиране, състезаниякурсове и уроци по програмиране, книги – безплатно от Наков курс мобилни приложения с iPhone, Android, WP7, PhoneGap уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop Дончо Минков - сайт за програмиране free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" C# курс, програмиране, безплатно
    • Free Trainings @ Telerik Academy “High-Quality Programming Code" course @ Telerik Academy  codecourse.telerik.com Telerik Software Academy  academy.telerik.com Telerik Academy @ Facebook  facebook.com/TelerikAcademy Telerik Software Academy Forums  forums.academy.telerik.com