Introduction to TDD and Mocking

Preview:

DESCRIPTION

introduction to test driven development and mocking

Citation preview

Introduction to

Test Driven Development

And

Mocking

@saeed_shargi

The Problem

Good

FastCheap

Time taken to fix bugs

Design Implementation QA Post-Release0

250

500

750

1000

Solution

Testing

Test Driven Development

Testing

Design Implementation Test

TDD

Design Test Implementation

TDD

Design Test Implementation Test

TDD

Design

Test

Implementation

Test

TDD

Design

Test

Implementation

Test

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

What is TDD?

What is TDD?

What is TDD?

TDD = Refactoring + TFD

Two Level of TDD

1) Acceptance TDD (ATDD)

2) Developer TDD

ATDD and TDD Together

Development Style

1) KISS

2) YAGNI

Writing only the code necessary to pass test

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

Tools

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

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.

Mocking

Two way to isolated :

1) Use Interface

2) Mocking framework

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); }

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;   }}

Mocking Frameworks

Nmock Moq Rhino Mocks TypeMock EasyMock.Net

End