Top Banner
Introduction to Test Driven Development And Mocking @saeed_shargi
24

Introduction to TDD and Mocking

May 10, 2015

Download

Technology

introduction to test driven development and mocking
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to TDD and Mocking

Introduction to

Test Driven Development

And

Mocking

@saeed_shargi

Page 2: Introduction to TDD and Mocking

The Problem

Good

FastCheap

Page 3: Introduction to TDD and Mocking

Time taken to fix bugs

Design Implementation QA Post-Release0

250

500

750

1000

Page 4: Introduction to TDD and Mocking

Solution

Testing

Test Driven Development

Page 5: Introduction to TDD and Mocking

Testing

Design Implementation Test

Page 6: Introduction to TDD and Mocking

TDD

Design Test Implementation

Page 7: Introduction to TDD and Mocking

TDD

Design Test Implementation Test

Page 8: Introduction to TDD and Mocking

TDD

Design

Test

Implementation

Test

Page 9: Introduction to TDD and Mocking

TDD

Design

Test

Implementation

Test

Page 10: Introduction to TDD and Mocking

What is TDD?

Write a test before writing a code. Specification not validation Think through requirements or design

before write code. Programming technique Ron Jefferies -> write clean code

Page 11: Introduction to TDD and Mocking

What is TDD?

Page 12: Introduction to TDD and Mocking

What is TDD?

Page 13: Introduction to TDD and Mocking

What is TDD?

TDD = Refactoring + TFD

Page 14: Introduction to TDD and Mocking

Two Level of TDD

1) Acceptance TDD (ATDD)

2) Developer TDD

Page 15: Introduction to TDD and Mocking

ATDD and TDD Together

Page 16: Introduction to TDD and Mocking

Development Style

1) KISS

2) YAGNI

Writing only the code necessary to pass test

Page 17: Introduction to TDD and Mocking

Benefits

1) Suit unit test provides that components working.

2) Clear code

3) Forces critical analysis and design

4) Better design , loosely coupled , easily maintainable

5) Reduced debugging time

Page 18: Introduction to TDD and Mocking

Tools

Cpputest csUnit (.Net) Cunit Dunit (Delphi) DBUnit JUnit NUnit PHPUnit xUnit.net

Page 19: Introduction to TDD and Mocking

Mocking

Simple classes dose not have dependencies.

In Action classes maybe have external dependencies like connect to database , connect to web services.

Good Test should be isolated. Integration test. Test should be fast.

Page 20: Introduction to TDD and Mocking

Mocking

Two way to isolated :

1) Use Interface

2) Mocking framework

Page 21: Introduction to TDD and Mocking

Example of using Interface

Interfaces to isolated database and web services :

public interface IEmailSource{    IEnumerable<string> GetEmailAddresses();} public interface IEmailDataStore{    void SaveEmailAddresses(IEnumerable<string> emailAddresses); }

Page 22: Introduction to TDD and Mocking

Example of using Interface

Mock classes :public class MockEmailSource : IEmailSource{   public IEnumerable<string> EmailAddressesToReturn { get; set; }   public IEnumerable<string> GetEmailAddresses()   {       return EmailAddressesToReturn;   }} public class MockEmailDataStore : IEmailDataStore{   public IEnumerable<string> SavedEmailAddresses { get; set; }   public void SaveEmailAddresses(IEnumerable<string> emailAddresses)   {       SavedEmailAddresses = emailAddresses;   }}

Page 23: Introduction to TDD and Mocking

Mocking Frameworks

Nmock Moq Rhino Mocks TypeMock EasyMock.Net

Page 24: Introduction to TDD and Mocking

End