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 :


Rename unit test class to calculatorTest:


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:


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:


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


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


Add the namespace calculatornamespace in the unit test project


now our code build successfully:


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.


Test fails as expected:


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


And run test again:


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>




Poster un commentaire

Code UI Test in Visual studio 2010 ultimate – Acceptance test in Microsoft

with Ultimate or premium visual studio, you are able to automate your acceptance test or make some User interface test with a recorder provided by

Code UI Test project. Morever, you can customize those test by adding some code, so let’s start with a simple winform project and after create the acceptance test associated:

Create a winform as follows to make an addition:

On designer set textbox and label:


The code behind on button click will display the addition result :


Now, let’s try to test our GUI by first creating a code UI Test :

1/add a test project to our solution (in ultimate version please or premium) and call it as you want 😉


2/Notice the test project on solution explorer and delete the UnitTest.cs file:


3/add a new test:


4/select a code UI Test


5/ a dialog appears, let the record action checked in order to use a User Interface recorder

Which allows to record every User Interface action and click OK:


6/waw on the left side at the bottom, a tool appears : it is the UI recorder


7/create a shortcut on your desktop to launch our addition application and press start recording to record the events to start the application (only click on shortcut to launch application):


8/Press pause to stop recording, click on the right button to notice the event recorded (here only the application launched)

You are able to delete some record steps if there are noise on test.


9/Finally, click on the button at the right side to generate code


10/in code ui test the method is generated and allow an automatic launch of our application


11/record and put some addition data here 5 + 5 = 10 :


As you can see all click event and numbers put on application are registred, let’s stop record and  create a method as before:


The second is correctly generated in code UI test:


Now, let’s check that the result is 10 by put the target button by drag and drop on the control label we are interesting:


As you can see a blue square wrap the control we are interesting and a panel property appears to let us decide on which property we will do an assertion: for sure it is  the display text so select the display text property and click on add assertion button :


It is inserted in code test:


Now run the test by selecting it in test view and enjoy the test is automated:


Waw the test is passed ! we have now to integrate it in a tfs nightly build and the delivery continuous will come faster 😉

, , , , ,

1 commentaire

DataFlow – TPL ( Task Parallel Library) – Asynchronous / Parallel programming using Microsoft

DataFlow allows you to make asynchronous process with different step of the process (called Block).First, download the TPL library using Nuget (and visual studio 2012 if you have it ;-). Create first an application console and after use nuget as follows « Manage Nuget Package »:


Write “dataflow” on the search textbox you will obtain the nuget package as follows:


In Solution Explorer, you will notice the TPL library as follow


The simple block called action block allow you to define a function asynchronously.

To write it is very easy, you have only to instantiate an ActionBlock first :


here we use linq to make a loop ten time on our process which waits one minute and write the string parameter in a console.After declare it, you have only to call it using Post method and pass a string parameter in our case:



the whole code to run your DataFlow in a console application is:


You will notice that the dataflow will occur in the same time as the while loop where we write dot in the output.Run it and enjoys :



waw, this a parallel program 😉

Now we will add another block to make our dataflow process.We create a TransformBlock which take a string as input and return the string in UpperCase.


We link the TransformBlock to the previous ActionBlock to write in console ouput the string in upper case:



Finally, we post a string using the transform block (which transfer the string to the ActionBlock because of  the link we ‘ve made:



the whole code :


Run it :


congratulation, you have made your first dataFlow (using two blocks) in a parallel programming 😉

With TPL, you can associate as many block you want  in asynchronous way

, , , ,

Poster un commentaire

Using architecture Diagram with visual studio ultimate 2010, architecture check

We create a simple 3 Layered architecture application with presentation layer for GUI, business Layer to do our business process and Data Access Layer to get our data from different source. The purpose is to check that each layer call the layer below and if not a tool (here architecture diagram ) can detect that someone doesn’t follow architecture rules.

The solution explorer show three project (one console project as presentation layer and the two others are library project:


The DataLayer project contains Data.cs file where data access is implemented:


the business layer in a process call data access layer to make some work:


with a reference on Data access layer assembly


finally, the presentation layer call the business process and show result in a console output:



with a reference on layer below that is business layer



use Architecture ->Generate Dependencices diagram by Assembly :



you obtain a dependency graph:


we see that Presentation Layer calls business which calls DataLayer.

We want to set architecture rules, so go to Architecture -> new diagram and select Diagram Layer:


click ok and set a name to your model project which appears in our solution explorer:



in toolbox on the left panel select layer and drag and drop on diagram three times as follows:



rename each layers to have presentation, business and data layer:


Drag and drop each project on its layer (Presentation is dragged and dropped on Presentation layer and so on…) a number at the right side of the layer appears to show that the layer is associated to a project 😉


now generate the dependencies we want to keep :


Visual studio create dependencies at layer level for you 😉


arrows show all dependencies (Presentation calls Business which calls Data layer)



Suppose now our software developper friend who call from presentation directly Data by setting a reference on data assembly and call our data method which is prohibited:


You are the architect (waw you have a good job ;-), and you want to quickly check that the architecture is followed and if not you want to have an alert. Go to the Layer diagram, then call Validate from the context menu


Now we have three errors showing where the architecture is not followed as expected:


Correct it by retrieve the modification of your bad friend (code and Data reference if not your bad friend will loose his job and will ask you to let him sleep at your home ;-( ), and run Validate Architecture again:



all is good so enjoy and show this article to your manager to get the ultimate version of visual studio 2010 😉

, , , , ,

1 commentaire

Code Contract, a way to control authorized values input output method or in interface at building time ! – Microsoft Static Check

From Microsoft research, with code contract, it is allowed to make verification on input, ouput arguments on methods and make other check on Interface.

First check in object explorer if you have Microsoft.Contracts.

1/ if you have an old Visual studio download Code contract assembly on download panel:


run MSI installer as follows:


Add a reference on Microsoft.Contracts assembly (which implements Data Contract you have just installed before 😉 :


in object explorer, (double click on Microsoft.Contracts to run it), you will see the 2 namespaces and classes contained:


2/Now you have Code Contract Assembly

in BizProcess method, use Contract class and call Requires method to check input value (here we check that b is different of 3 ;-):


Go to properties solution and in Code Contracts panel (left side), Check Perform Static Contract Checking in order to make checks on build time 😉


when you build solution, you will see a warning as below because a business rule is violated (b different of 3 in our sample)


Code contract is very interesting to make some check at build time because at runtime it is too late… 😉

You can also make check on Interface by using attributes ContractClass and ContractClassFor

For sure when data are knowing at runtime, you will make your check in code.

, , ,

1 commentaire

Entity Framework 5.0 – Microsoft ORM ( Object Relational Mapping)

In this article, we will create a simple application using Entity Framework 5.0 with Code First approach. This approach is used when you want to create a database model from your application model.

Entity Framework is the ORM (Object Relationnal Mapping) in Microsoft Technologies.

First create a business object in a library class (the file is named Poco_entity.cs), client:

this is a POCO class because it doesn’t have an ORM depedency.

Secondly, create a clientContext class which will wrap the client class an is used by Entity Framework to create table

ClientContext inherits from DbContext and we use DbSet to wrap our POCO class.

It allows us to not have a dependency between class and ORM technologies. So ClientContext wrap a client collection.

Now we can create a database from our code application by initializing the database and the context for example in a console application:


Here, we first initialize the target database and indicate that each time the framework will drop and create database (only in development cycle;-). Secondly, we instantiate a ClientContext which allows us to manage our POCO class in clients collection (our DbSet property) to persist it in the target database. And finally, we call SaveChanges in order to create the database and record the client created in a table (client table).

Because of not specifying a sql connection, Entity Framework by default use SQL express:


Connect to your sql server express instance (for me Hassan-Tosh\Sqlexpress)

You will see that a database has been created for you (databaseContext.clientContext)

And in the client table, a new row is inserted 😉

you have also other approach: Database first ( you create entities code from an existing database ) and model first (you create class diagram in visual studio and Entity create database for you).

For more information:



, , , ,

Poster un commentaire

Multithreading with ThreadPool How many thread to use in a computer ?

In this article, I give a sample console application with a task contained a Sleep of ten second ( like  a process that take ten second 😉 to simulate an application with  200 task of ten second.

ThreadPool is a class that allows you to create task which are queuing and executing by the ThreadPool. It is better to use it than creating yourself all the plumbing of the management of thread ( I believe that Microsoft engineer work well ;-).

the rule is to take one thread by CPU to make real multithread but in many cases you can use as in my sample an example that demonstrate that it is not true every  time

I use a 8 cpu computer so in my ThreadPool when I call GetMinThreads it return 8 thread (one by CPU). The application launch 200 task that wait 10 second.

when running the application, you see that it took only 11 seconds instead of 200×11 second :

launch task manager to see the number of thread: first select view->Select Columns


and select Thread column:

there are 203 thread with 3 thread for console and main application:

if I set the minimum thread at 8, ThreadPool will use it so it execute the 200 task 8  by 8 . To do it modify the code by using SetMinThread :

Now the application spend more time although we respect the rule : 1 thread by CPU, we have 8 CPU so we use 8 threads 😉 Now it take 67 seconds (67011ms)!

you can observe that the ThreadPool has created new thread (more than 8) step by step which explains that application run on 67seconds and not on 200/8 x 10 seconds that is 250seconds. Don’t forget that you can set min and max thread so the ThreadPool can add or remove thread to execute all tasks.

If you want to fix the number of thread you can set the same number in SetMinThread method and SetMaxThread method

So if you have to make a lot of task try different number of threads using the SetMinThreads


, , ,

Poster un commentaire

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