TDD – Test Design Driven on visual studio 2013

TDD is usefull to be sure of what your code is supposed to do and guaranty that your test strategy is started as soon as you start to develop.

The hard work is more on correctly scoping your test on TDD ( scope at component level or integration level) because for a high level, on a similar way, we could use BDD (Behavior Design Driven at requirement/ user stories or acceptance level).

A quick start with a quick sample (famous calculator):

3 steps called

-Red because at the first iteration, test should fail (no software exist to do the job)

-Green because we create simple class in order to pass on build

-Refactor for many iterations to implement the business logic and after make code in better way

First create a test project in visual studio :

unitTestProject

Rename unit test class to calculatorTest:

02-renamecalculatortest

First task to implement: application  is able to add 2 integers:

1 – Red => create a test which check that application is able to add 2 integers:

03-Red-Failetocompile

We have created a unit test method called TestAdd2int in three step

Arrange => to create the context for the test

We choose to declare a Calculator class (not yet created) to do the work

Act=>do the work we want to test (here call the tested method add 2 integers)

Assert=>to check that the result is correct

The red step is done => it doesn’t compile but we have design a Calculator Class and an Add method to do the task

2/Green=>create the class with only the needed items to pass the build (not the assertion):

We only need to create a Calculator class and an Add method which return an integer:

Create a class library called Calculator:

04-createclasslibrary

Implement Calculator class and an add method (without the business logic)

05-classcreated

Add a project reference of the created library class in unit test project :

06-referenceinunitest

Add the namespace calculatornamespace in the unit test project

07-solvereference

now our code build successfully:

08-buildsucced

The green step is done => our code compile

3/The final step is to implement and refactor:

We launch mstest runner to check that our test fails because implement of business logic doesn’t exist.

09-runtest

Test fails as expected:

10-testfails

Now, we implement the task in Add method of our Calculator class:

11-addimplementation

And run test again:

12-testpass

Congratulations => you have done your first TDD

After that when implementing other methods, as substract… for different type (double …) we will make refactoring – (using generic and dynamic step which could be as follow:

Class view: (Calculator become calculator<T>

13-calculatorcode

14-calculatortest

Publicités
  1. Poster un commentaire

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

Microsoft System Center 2012

All System Center 2012 products, Service Manager, App-Controller, Orchestrator, Virtual Machine Manager, Configuration Manager, Data Protection Manager, Operation Manager -Boutougha Hassan

Microsoft Windows Server 2012

IT - Windows server 2012 - Hassan Boutougha

Microsoft Hyper-V

IT - Hyper-V - Hassan Boutougha

Microsoft SQL Server 2012

IT - SQL Server 2012 - Hassan Boutougha

%d blogueurs aiment cette page :