SlideShare a Scribd company logo
1 of 11
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 1 28/07/2015
TUTORIAL: TEST DRIVEN DEVELOPMENT
WITH VISUAL STUDIO 2012
This blog post is going to talk about Test Driven Development (TDD) in an Agile environment using
the latest Visual Studio 2012 features and how that is going to help you to be more efficient and
productive. The goal of this post will be to show you some of the new features within Visual Studio
2012, that are going to help you when trying to apply the TDD approach. The first parts are more
theoretical, while as the last part will be fully practical, since we are going to implement some example
code using TDD.
Contrary to traditional software development methodologies and processes, where writing unit test is
often done as last step after code implementation, TDD consists of writing unit test upfront as first step
in the software development cycle. No development can be started without having implemented the
corresponding unit tests. You understand that TDD has large impacts on the organization of your teams.
The following are the different steps when applying Test Driven Development:
 Write a unit test for a new functionality, then validate that the test fails, since its
implementation has not been realized
 Implement the minimum code necessary to pass the test, then validate that the test passes
successfully, meaning that the expected behavior is now provided by the implementation
 Refactor and optimize your code, the unit tests ensure the coherence of the functionalities
Using TDD helps in obtaining a code source that is very reliable, predictable, robust - and after
refactoring - highly optimized. Unit tests ensure a correct behavior, independently from where it is
going to be used, resulting in a code source that will work as expected under any circumstances.
For being able to create good unit tests, you have at first to think about the conception and software
design of your application. You must not hurry into doing your implementations, before being clear
about your objectives (which should be true for any development, but sadly often it is not).
Conceptual errors can be detected and resolved much quicker and in a much more efficient way. As
explained the implementation is started only after the conceptual phase has been validated and tested
via thorough unit tests. In this case, your unit tests become much than just tests. They become some
sort of general specification, describing “units of functionality” for your application.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 2 28/07/2015
When refactoring and optimizing your code, you may restructure your code without any risks, since all
your modifications are now verifiable. Unit tests ensure that there are no technical or functional
regressions and furthermore the coherence of behavior. They validate that your application will always
behave in the same way, if they are executed with success.
Additionally if you combine TDD with Extreme Programming (XP) and pair programming, you obtain
a code with a very high degree of quality.
Visual Studio 2012 externaltest framework support
The previous version Visual Studio 2010 already provides the possibility of using external test
frameworks. But unfortunately with multiple limitations such as:
 Dependency on third party applications for being able to execute unit tests. For example
Gallio needs Icarus Runner if you want to run your unit tests.
 Code coverage analysis does not work natively, only by using third party products (such as
NCover for example).
 Execution of unit tests is specific to certain plugins: for example there is a difference if you
run your MSTest unit tests from within Visual Studio 2008 or by using Resharper, which may
result in problems when trying to apply continuous integration processes.
As you can see, you have to multiply plugins and tools, that might not always be compatible with each
other, when using Visual Studio 2010 with external test frameworks. Really not an optimal situation,
when you want to benefit from specialized external test frameworks and their advanced functionalities!
One of the major updates of Visual Studio 2012 is the support of several external unit test frameworks
for multiple languages without any of the limitations mentioned above.
Here are some of the unit test frameworks that you can now use easily:
For .NET:
 NUnit (http://nunit.org)
 xUnit.net (http://xunit.codeplex.com)
 MBUnit (https://github.com/Gallio/Gallio-VS2011-Integration)
For Javascript/HTML:
 QUnit & Jasmine (http://chutzpah.codeplex.com)
For C++:
 MSTest Native (http://aka.ms/mstest-native-vs11)
How to use NUnit as external testunit framework
Lets see how to use NUnit as external test unit framework. First you have to install NUnit (currently as
Prerelease) via NuGet.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 3 28/07/2015
Then you have to install the test adapter plugin for NUnit (currently as Beta) via the Extension
Manager, which you can now find under “Tools/Extensions and Updates...”. Note that all test adapters
are free of charge and that the functionality to search and download those adapters is fully integrated in
Visual Studio 2012.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 4 28/07/2015
After installing the NUnit test adapter plugin, you are able to use all Visual Studio 2012 unit test
functionalities together with NUnit. You may for example execute your NUnit unit tests directly from
within the Visual Studio 2012 IDE, display the test results in the standard view “Test Explorer” and run
a code coverage analysis on your code.
Support for C++ in native MSTest
Another big feature of Visual Studio 2012 is that it now supports native MSTest unit tests for C++
applications. Good news for C++ developers, who may now also apply Test Driven Development using
native MSTest as unit test framework.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 5 28/07/2015
Following is an example implementation of a unit test in Visual C++ using the native MSTest unit test
framework:
Support for async and await in VS 2012 and its unit test framework
Windows 8 and .NET 4.5 introduce a major feature that will have a high impact on software
development as we know it: Asynchronous Programming. Visual Studio 2012 and its unit test
framework support this new approach perfectly. The async and await keywords, available in .NET 4.5,
may now be used for creating unit tests of asynchronous methods.
Lets say that you want to create a unit test for the following asynchronous example method:
The corresponding NUnit test method could be:
Managementof unit tests via TestExplorer
The first impression when opening the “Test Explorer” window is a very positive one. Here are the
principal changes when compared to Visual Studio 2010:
 The “Test View” and “Test Results” windows have been deleted and consolidated into the
“Test Explorer” window. This is going to streamline the interaction between development
(the code) and tests.
 The interface is simple but efficient: all information is accessible via a simple mouse click in
a very intuitive way.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 6 28/07/2015
 Unit tests are grouped by their status (failed, passed,...), failed tests are shown on top, a
double mouse click allows for accessing the code source of a test (no need to open an
external window anymore).
 It is now much easier to execute a code coverage analysis. In previous versions this was not
handled in a very intuitive way, since you had to create a configuration file, start the analysis
via the Visual Studio menu and then open the adequate results window. In Visual Studio 2012
everything was consolidated and is now integrated in the “Test Explorer” interface.
PostBuild TestRuns
A best practice when adhering to TDD principles is to execute unit tests as soon and as much as
possible for being able to identify bugs, misbehaviors and regressions. There is a new feature, called
“Post Build Test Runs”, in Visual Studio 2012, which allows for automatically unit test execution after
each compilation. The feature can either be activated via the menu under “Test/Test Settings” or
directly from within the “Test Explorer” window. When using this features, unit test are executed on a
separated and dedicated thread, so there is no impact on developer efficiency.
Migration of unit tests from Visual Studio 2010 to Visual Studio 2012
As you might know, there a multiple difficulties and bugs, when migrating unit tests from Visual Studio
2008 to Visual Studio 2010, because the migration is not very transparent and sometimes even
somewhat complex. In some cases you even have to migrate to the .NET 4.0 Framework to make
everything work correctly!
I can assure you that there are no such migration problems, when trying to migrate from Visual Studio
2010 to Visual Studio 2012. This is partly due to the fact that most of the unit test components are the
same between those two versions of Visual Studio. The library is still
Microsoft.VisualStudio.QualityTools.unitTestFramework.dll in its version 10.0.0.0, which is still based
on .NET runtime v2.0.50727.
Fakes Framework (Stubs and Shims)
The “Fakes Framework” based on the “Moles” project, created by the Microsoft Research team, is now
exclusively integrated into the “Ultimate" version of Visual Studio 2012 (and sadly only in this
version!).
The goal of this framework is to aid development teams in producing unit tests rapidly and easily. For
this, the “Fakes Framework” adds 2 notions:
 Stubs: they provide automatic mock implementations of interfaces or abstract classes, that
can be used in unit tests for being able to isolate parts that need to be unit tested.
 Shims: they allow for runtime interception and redirection of method calls to specific objects.
For example , they may be used to mock objects, which normally can’t be mocked due to
access restrictions in the .NET framework. By using Shims it is possible to redirect calls to
these objects with calls to objects that provide your own implementations.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 7 28/07/2015
There are however some restrictions. One restriction is that classes included in the “mscorlib”
namespace cannot have any “Fake Assemblies”. Unfortunately, you cannot create any Shims for the
“System.Configuration.ConfigurationManager” class for example.
The “Fakes Framework” provides some real advantages, when compared to existing mock testing
frameworks such as RhinoMock, because developers do not need to modify their functional code for
being able to execute unit tests.
TDD using Visual Studio 2012:A practicalexample
I am now going to show you how to practically use all of those new features and apply them to Test
Driven Development. You are going to see a full cycle of TDD using Visual Studio 2012, during the
implementation of a simple calculator example!
Phase 1: Write a unit test for a new functionality
Respecting the Test Driven Development approach, you have to create your unit tests before starting
with any implementations. In our example we have to write some unit tests for the methods “Addition”
and “Multiplication”. But first of all, we have to add a project of type “Unit Test Project” to our
solution (if you do not already have one).
You may now add the necessary unit tests. Here is an example of the unit tests you could add based on
the unit test framework NUnit:
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 8 28/07/2015
Please note that the “Calculator” class and its methods “Addition” and “Multiplication” do not exist at
this stage yet.
Visual Studio 2012 provides the possibility to generate the missing code in an automatic way. For that
you just have to right-click on the “new Calculator” definition in the unit test project and choose to
generate the class via the “Generate/New Type” option in the menu.
A wizard opens and you are now able to configure multiple options such as the type (classe, struct,
interface, enum), the access (public, internal), the destination project and the file name for the
generation of the missing class.
The next step, after having auto-generated the “Calculator" class, consists of auto-generating the
missing methods within this class. This can be achieved in almost the same way as it was done for the
missing class. You do a right-click on the method calls “calculator.Addition(...)” and
“calculator.Multiplication(...)” in the unit test project and you generate them via the “Generate/Method
Stub” option in the menu.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 9 28/07/2015
Here is the auto-generated source code of those two methods:
In the last step of this phase you have to open the “Test Explorer” window where you may now execute
all your unit tests. This can be done by clicking on the “RunAll” button or by using the already
explained “Post Build Test Runs” option.
As expected your unit tests will fail, since the corresponding source code has not been implemented yet.
We will see how to do that in the next phase.
Phase 2: Implement the minimum code necessary to pass the test
Now in this phase, the only thing that needs to be done, is to implement the expected functionalities.
The idea is to develop the minimum code necessary, which responds to the functional requirements.
Everything that concerns optimization and amelioration must not be addressed since it will be treated
later in the next phase (refactoring).
Following the implementation you may now restart your unit tests by clicking on the “RunAll” button
or by using the already explained “Post Build Test Runs” option (see the previous blog post in the
series). Your should see that you unit tests have been terminated successfully. If this is not the case you
have to review your code and iterate until all of your unit tests pass successfully.
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 10 28/07/2015
At this stage the source code corresponds exactly to the functional needs and it provides the expected
behavior. The final project structure includes a unit test project as well as an application
implementation project.
But the source code might not be optimized. Its quality might not adhere to your quality standards, so it
has to be ameliorate. The refactoring can be done without any problems since the unit test assure that
there are no regressions. Moreover, regressions can be detected very quickly and thus can be handled as
soon as possible. This is going to be explained in the next phase.
Phase 3: Refactor and optimize the source code
The process of improving your source code after the initial implementation phase (Phase 2) is called
“Refactoring”. The source code structure is modified internally without any modifications to the
external behavior (very important!!). A source code that just “works” is now transformed into a source
code that works in an optimal way. Most of the time, the resulting source code is executing with better
performance, using less memory and/or with a better software design.
The refactoring consists of the following steps (non-exhaustive list):
 Detect and eliminate all code duplication
 Limit complexity and the number of classes
 Simplify and optimize method algorithms
 Relocate, rename and harmonize methods
TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012
Author: Hong Le Van Page 11 28/07/2015
 Improve code readability
 Remove not used code (also called “dead code”)
 Add comments to complex code sections
In our simple example there is nothing to be refactored, since there are neither enough methods nor
enough classes. But this last step has to be done in bigger developments at the end of each cycle.
Afterwards, a new development cycle starts with new functionalities from Phase1 on.
ConclusionThe TestDriven Developmentwith Visual Studio 2012
As you have seen Visual Studio 2012 was greatly extended and enables efficient Test Driven
Development. It helps you on all steps and even enhances and optimizes your productivity.

More Related Content

What's hot

Android testing
Android testingAndroid testing
Android testingBitbar
 
Full Testing Experience - Visual Studio and TFS 2010
 Full Testing Experience - Visual Studio and TFS 2010 Full Testing Experience - Visual Studio and TFS 2010
Full Testing Experience - Visual Studio and TFS 2010Ed Blankenship
 
Improving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester TrainingImproving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester TrainingAnna Russo
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 
Igor Cernopolc - Http authentication in automated testing - presentation script
Igor Cernopolc - Http authentication in automated testing - presentation scriptIgor Cernopolc - Http authentication in automated testing - presentation script
Igor Cernopolc - Http authentication in automated testing - presentation scriptCodecamp Romania
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mockingmrjawright
 
Test driven development and react js application go hand in hand
Test driven development and react js application go hand in handTest driven development and react js application go hand in hand
Test driven development and react js application go hand in handKaty Slemon
 
Telerik Test studio
Telerik Test studio Telerik Test studio
Telerik Test studio Ahamad Sk
 
Test & behavior driven development
Test & behavior driven developmentTest & behavior driven development
Test & behavior driven developmentTristan Libersat
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)David Ehringer
 
How to setup unit testing in Android Studio
How to setup unit testing in Android StudioHow to setup unit testing in Android Studio
How to setup unit testing in Android Studiotobiaspreuss
 
Katalon Studio - Best automation solution for software testing team
Katalon Studio - Best automation solution for software testing teamKatalon Studio - Best automation solution for software testing team
Katalon Studio - Best automation solution for software testing teamKatalon Studio
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Babul Mirdha
 
Whats new in visual studio 2017
Whats new in visual studio 2017Whats new in visual studio 2017
Whats new in visual studio 2017Md. Mahedee Hasan
 
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...agil8 Ltd
 

What's hot (20)

Android testing
Android testingAndroid testing
Android testing
 
Full Testing Experience - Visual Studio and TFS 2010
 Full Testing Experience - Visual Studio and TFS 2010 Full Testing Experience - Visual Studio and TFS 2010
Full Testing Experience - Visual Studio and TFS 2010
 
Test studio
Test studioTest studio
Test studio
 
Improving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester TrainingImproving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester Training
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Igor Cernopolc - Http authentication in automated testing - presentation script
Igor Cernopolc - Http authentication in automated testing - presentation scriptIgor Cernopolc - Http authentication in automated testing - presentation script
Igor Cernopolc - Http authentication in automated testing - presentation script
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mocking
 
Test driven development and react js application go hand in hand
Test driven development and react js application go hand in handTest driven development and react js application go hand in hand
Test driven development and react js application go hand in hand
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Telerik Test studio
Telerik Test studio Telerik Test studio
Telerik Test studio
 
Test & behavior driven development
Test & behavior driven developmentTest & behavior driven development
Test & behavior driven development
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
How to setup unit testing in Android Studio
How to setup unit testing in Android StudioHow to setup unit testing in Android Studio
How to setup unit testing in Android Studio
 
Katalon Studio - Best automation solution for software testing team
Katalon Studio - Best automation solution for software testing teamKatalon Studio - Best automation solution for software testing team
Katalon Studio - Best automation solution for software testing team
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)
 
Whats new in visual studio 2017
Whats new in visual studio 2017Whats new in visual studio 2017
Whats new in visual studio 2017
 
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
 

Viewers also liked

Agile vs. waterfall - The fundamentals differences
Agile vs. waterfall - The fundamentals differencesAgile vs. waterfall - The fundamentals differences
Agile vs. waterfall - The fundamentals differencesDavid Tzemach
 
Agile and waterfall
Agile and waterfallAgile and waterfall
Agile and waterfallJohn Morse
 
Waterfall vs agile approach scrum framework and best practices in software d...
Waterfall vs agile approach  scrum framework and best practices in software d...Waterfall vs agile approach  scrum framework and best practices in software d...
Waterfall vs agile approach scrum framework and best practices in software d...Tayfun Bilsel
 
Agile vs Waterfall Project management
Agile vs Waterfall  Project management Agile vs Waterfall  Project management
Agile vs Waterfall Project management Kostiantyn Trefiak
 
Agile vs Traditional Project Management
Agile vs Traditional Project ManagementAgile vs Traditional Project Management
Agile vs Traditional Project ManagementSaqib Javed John
 
Agile vs Iterative vs Waterfall models
Agile vs Iterative vs Waterfall models Agile vs Iterative vs Waterfall models
Agile vs Iterative vs Waterfall models Marraju Bollapragada V
 
BDD in Action - building software that matters
BDD in Action - building software that mattersBDD in Action - building software that matters
BDD in Action - building software that mattersJohn Ferguson Smart Limited
 

Viewers also liked (10)

Agile vs. waterfall - The fundamentals differences
Agile vs. waterfall - The fundamentals differencesAgile vs. waterfall - The fundamentals differences
Agile vs. waterfall - The fundamentals differences
 
Agile vs waterfall methodology
Agile vs waterfall methodologyAgile vs waterfall methodology
Agile vs waterfall methodology
 
Agile and waterfall
Agile and waterfallAgile and waterfall
Agile and waterfall
 
Waterfall vs agile approach scrum framework and best practices in software d...
Waterfall vs agile approach  scrum framework and best practices in software d...Waterfall vs agile approach  scrum framework and best practices in software d...
Waterfall vs agile approach scrum framework and best practices in software d...
 
Agile vs Waterfall Project management
Agile vs Waterfall  Project management Agile vs Waterfall  Project management
Agile vs Waterfall Project management
 
Waterfall Model
Waterfall ModelWaterfall Model
Waterfall Model
 
Agile vs Waterfall
Agile vs WaterfallAgile vs Waterfall
Agile vs Waterfall
 
Agile vs Traditional Project Management
Agile vs Traditional Project ManagementAgile vs Traditional Project Management
Agile vs Traditional Project Management
 
Agile vs Iterative vs Waterfall models
Agile vs Iterative vs Waterfall models Agile vs Iterative vs Waterfall models
Agile vs Iterative vs Waterfall models
 
BDD in Action - building software that matters
BDD in Action - building software that mattersBDD in Action - building software that matters
BDD in Action - building software that matters
 

Similar to Tutorial test driven development with Visual Studio 2012

TDD with Visual Studio 2010
TDD with Visual Studio 2010TDD with Visual Studio 2010
TDD with Visual Studio 2010Stefano Paluello
 
Visual studio Team system 2012
Visual studio Team system 2012Visual studio Team system 2012
Visual studio Team system 2012kunnathust
 
Introducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappellIntroducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappellAravindharamanan S
 
Introducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappellIntroducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappellAravindharamanan S
 
Testing SharePoint solutions overview
Testing SharePoint solutions overviewTesting SharePoint solutions overview
Testing SharePoint solutions overviewSpiffy
 
Visual Studio 2015 / Visual Studio Team Services Overview
Visual Studio 2015 / Visual Studio Team Services OverviewVisual Studio 2015 / Visual Studio Team Services Overview
Visual Studio 2015 / Visual Studio Team Services OverviewHimanshu Desai
 
Microsoft DevOps Solution - DevOps
Microsoft DevOps Solution - DevOps  Microsoft DevOps Solution - DevOps
Microsoft DevOps Solution - DevOps Chetan Gordhan
 
Visual Studio 2008 Overview
Visual Studio 2008 OverviewVisual Studio 2008 Overview
Visual Studio 2008 OverviewRoman Okolovich
 
Continuous Everything
Continuous EverythingContinuous Everything
Continuous EverythingAndrea Tino
 
Testing tools in visual studio
Testing tools in visual studioTesting tools in visual studio
Testing tools in visual studioMahdi Esmailoghli
 
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-StudioComparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-StudioPVS-Studio
 
235042632 super-shop-ee
235042632 super-shop-ee235042632 super-shop-ee
235042632 super-shop-eehomeworkping3
 
Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010joycsc
 
Integrated ALM using Microsoft 2012 Solutions
Integrated ALM using Microsoft 2012 SolutionsIntegrated ALM using Microsoft 2012 Solutions
Integrated ALM using Microsoft 2012 SolutionsAnup Hariharan Nair
 
Selenium - The Way Of Success
Selenium - The Way Of SuccessSelenium - The Way Of Success
Selenium - The Way Of SuccessZbyszek Mockun
 
Testingfor continuousdeliverywithvisualstudio2012
Testingfor continuousdeliverywithvisualstudio2012Testingfor continuousdeliverywithvisualstudio2012
Testingfor continuousdeliverywithvisualstudio2012Steve Xu
 
Software Development Standard Operating Procedure
Software Development Standard Operating Procedure Software Development Standard Operating Procedure
Software Development Standard Operating Procedure rupeshchanchal
 
CucumberSeleniumWD
CucumberSeleniumWDCucumberSeleniumWD
CucumberSeleniumWDVikas Sarin
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Jacinto Limjap
 

Similar to Tutorial test driven development with Visual Studio 2012 (20)

TDD with Visual Studio 2010
TDD with Visual Studio 2010TDD with Visual Studio 2010
TDD with Visual Studio 2010
 
Visual studio Team system 2012
Visual studio Team system 2012Visual studio Team system 2012
Visual studio Team system 2012
 
Introducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappellIntroducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappell
 
Introducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappellIntroducing visual studio_2010_v1.0--chappell
Introducing visual studio_2010_v1.0--chappell
 
Testing SharePoint solutions overview
Testing SharePoint solutions overviewTesting SharePoint solutions overview
Testing SharePoint solutions overview
 
Visual Studio 2015 / Visual Studio Team Services Overview
Visual Studio 2015 / Visual Studio Team Services OverviewVisual Studio 2015 / Visual Studio Team Services Overview
Visual Studio 2015 / Visual Studio Team Services Overview
 
Microsoft DevOps Solution - DevOps
Microsoft DevOps Solution - DevOps  Microsoft DevOps Solution - DevOps
Microsoft DevOps Solution - DevOps
 
Visual Studio 2008 Overview
Visual Studio 2008 OverviewVisual Studio 2008 Overview
Visual Studio 2008 Overview
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Continuous Everything
Continuous EverythingContinuous Everything
Continuous Everything
 
Testing tools in visual studio
Testing tools in visual studioTesting tools in visual studio
Testing tools in visual studio
 
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-StudioComparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
Comparing static analysis in Visual Studio 2012 (Visual C++ 2012) and PVS-Studio
 
235042632 super-shop-ee
235042632 super-shop-ee235042632 super-shop-ee
235042632 super-shop-ee
 
Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010Overview of Visual Studio Team System 2010
Overview of Visual Studio Team System 2010
 
Integrated ALM using Microsoft 2012 Solutions
Integrated ALM using Microsoft 2012 SolutionsIntegrated ALM using Microsoft 2012 Solutions
Integrated ALM using Microsoft 2012 Solutions
 
Selenium - The Way Of Success
Selenium - The Way Of SuccessSelenium - The Way Of Success
Selenium - The Way Of Success
 
Testingfor continuousdeliverywithvisualstudio2012
Testingfor continuousdeliverywithvisualstudio2012Testingfor continuousdeliverywithvisualstudio2012
Testingfor continuousdeliverywithvisualstudio2012
 
Software Development Standard Operating Procedure
Software Development Standard Operating Procedure Software Development Standard Operating Procedure
Software Development Standard Operating Procedure
 
CucumberSeleniumWD
CucumberSeleniumWDCucumberSeleniumWD
CucumberSeleniumWD
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 

Recently uploaded

Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitiva2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitivaDiego IvĂĄn Oliveros Acosta
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 

Recently uploaded (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitiva2.pdf Ejercicios de programaciĂłn competitiva
2.pdf Ejercicios de programaciĂłn competitiva
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 

Tutorial test driven development with Visual Studio 2012

  • 1. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 1 28/07/2015 TUTORIAL: TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 This blog post is going to talk about Test Driven Development (TDD) in an Agile environment using the latest Visual Studio 2012 features and how that is going to help you to be more efficient and productive. The goal of this post will be to show you some of the new features within Visual Studio 2012, that are going to help you when trying to apply the TDD approach. The first parts are more theoretical, while as the last part will be fully practical, since we are going to implement some example code using TDD. Contrary to traditional software development methodologies and processes, where writing unit test is often done as last step after code implementation, TDD consists of writing unit test upfront as first step in the software development cycle. No development can be started without having implemented the corresponding unit tests. You understand that TDD has large impacts on the organization of your teams. The following are the different steps when applying Test Driven Development:  Write a unit test for a new functionality, then validate that the test fails, since its implementation has not been realized  Implement the minimum code necessary to pass the test, then validate that the test passes successfully, meaning that the expected behavior is now provided by the implementation  Refactor and optimize your code, the unit tests ensure the coherence of the functionalities Using TDD helps in obtaining a code source that is very reliable, predictable, robust - and after refactoring - highly optimized. Unit tests ensure a correct behavior, independently from where it is going to be used, resulting in a code source that will work as expected under any circumstances. For being able to create good unit tests, you have at first to think about the conception and software design of your application. You must not hurry into doing your implementations, before being clear about your objectives (which should be true for any development, but sadly often it is not). Conceptual errors can be detected and resolved much quicker and in a much more efficient way. As explained the implementation is started only after the conceptual phase has been validated and tested via thorough unit tests. In this case, your unit tests become much than just tests. They become some sort of general specification, describing “units of functionality” for your application.
  • 2. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 2 28/07/2015 When refactoring and optimizing your code, you may restructure your code without any risks, since all your modifications are now verifiable. Unit tests ensure that there are no technical or functional regressions and furthermore the coherence of behavior. They validate that your application will always behave in the same way, if they are executed with success. Additionally if you combine TDD with Extreme Programming (XP) and pair programming, you obtain a code with a very high degree of quality. Visual Studio 2012 externaltest framework support The previous version Visual Studio 2010 already provides the possibility of using external test frameworks. But unfortunately with multiple limitations such as:  Dependency on third party applications for being able to execute unit tests. For example Gallio needs Icarus Runner if you want to run your unit tests.  Code coverage analysis does not work natively, only by using third party products (such as NCover for example).  Execution of unit tests is specific to certain plugins: for example there is a difference if you run your MSTest unit tests from within Visual Studio 2008 or by using Resharper, which may result in problems when trying to apply continuous integration processes. As you can see, you have to multiply plugins and tools, that might not always be compatible with each other, when using Visual Studio 2010 with external test frameworks. Really not an optimal situation, when you want to benefit from specialized external test frameworks and their advanced functionalities! One of the major updates of Visual Studio 2012 is the support of several external unit test frameworks for multiple languages without any of the limitations mentioned above. Here are some of the unit test frameworks that you can now use easily: For .NET:  NUnit (http://nunit.org)  xUnit.net (http://xunit.codeplex.com)  MBUnit (https://github.com/Gallio/Gallio-VS2011-Integration) For Javascript/HTML:  QUnit & Jasmine (http://chutzpah.codeplex.com) For C++:  MSTest Native (http://aka.ms/mstest-native-vs11) How to use NUnit as external testunit framework Lets see how to use NUnit as external test unit framework. First you have to install NUnit (currently as Prerelease) via NuGet.
  • 3. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 3 28/07/2015 Then you have to install the test adapter plugin for NUnit (currently as Beta) via the Extension Manager, which you can now find under “Tools/Extensions and Updates...”. Note that all test adapters are free of charge and that the functionality to search and download those adapters is fully integrated in Visual Studio 2012.
  • 4. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 4 28/07/2015 After installing the NUnit test adapter plugin, you are able to use all Visual Studio 2012 unit test functionalities together with NUnit. You may for example execute your NUnit unit tests directly from within the Visual Studio 2012 IDE, display the test results in the standard view “Test Explorer” and run a code coverage analysis on your code. Support for C++ in native MSTest Another big feature of Visual Studio 2012 is that it now supports native MSTest unit tests for C++ applications. Good news for C++ developers, who may now also apply Test Driven Development using native MSTest as unit test framework.
  • 5. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 5 28/07/2015 Following is an example implementation of a unit test in Visual C++ using the native MSTest unit test framework: Support for async and await in VS 2012 and its unit test framework Windows 8 and .NET 4.5 introduce a major feature that will have a high impact on software development as we know it: Asynchronous Programming. Visual Studio 2012 and its unit test framework support this new approach perfectly. The async and await keywords, available in .NET 4.5, may now be used for creating unit tests of asynchronous methods. Lets say that you want to create a unit test for the following asynchronous example method: The corresponding NUnit test method could be: Managementof unit tests via TestExplorer The first impression when opening the “Test Explorer” window is a very positive one. Here are the principal changes when compared to Visual Studio 2010:  The “Test View” and “Test Results” windows have been deleted and consolidated into the “Test Explorer” window. This is going to streamline the interaction between development (the code) and tests.  The interface is simple but efficient: all information is accessible via a simple mouse click in a very intuitive way.
  • 6. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 6 28/07/2015  Unit tests are grouped by their status (failed, passed,...), failed tests are shown on top, a double mouse click allows for accessing the code source of a test (no need to open an external window anymore).  It is now much easier to execute a code coverage analysis. In previous versions this was not handled in a very intuitive way, since you had to create a configuration file, start the analysis via the Visual Studio menu and then open the adequate results window. In Visual Studio 2012 everything was consolidated and is now integrated in the “Test Explorer” interface. PostBuild TestRuns A best practice when adhering to TDD principles is to execute unit tests as soon and as much as possible for being able to identify bugs, misbehaviors and regressions. There is a new feature, called “Post Build Test Runs”, in Visual Studio 2012, which allows for automatically unit test execution after each compilation. The feature can either be activated via the menu under “Test/Test Settings” or directly from within the “Test Explorer” window. When using this features, unit test are executed on a separated and dedicated thread, so there is no impact on developer efficiency. Migration of unit tests from Visual Studio 2010 to Visual Studio 2012 As you might know, there a multiple difficulties and bugs, when migrating unit tests from Visual Studio 2008 to Visual Studio 2010, because the migration is not very transparent and sometimes even somewhat complex. In some cases you even have to migrate to the .NET 4.0 Framework to make everything work correctly! I can assure you that there are no such migration problems, when trying to migrate from Visual Studio 2010 to Visual Studio 2012. This is partly due to the fact that most of the unit test components are the same between those two versions of Visual Studio. The library is still Microsoft.VisualStudio.QualityTools.unitTestFramework.dll in its version 10.0.0.0, which is still based on .NET runtime v2.0.50727. Fakes Framework (Stubs and Shims) The “Fakes Framework” based on the “Moles” project, created by the Microsoft Research team, is now exclusively integrated into the “Ultimate" version of Visual Studio 2012 (and sadly only in this version!). The goal of this framework is to aid development teams in producing unit tests rapidly and easily. For this, the “Fakes Framework” adds 2 notions:  Stubs: they provide automatic mock implementations of interfaces or abstract classes, that can be used in unit tests for being able to isolate parts that need to be unit tested.  Shims: they allow for runtime interception and redirection of method calls to specific objects. For example , they may be used to mock objects, which normally can’t be mocked due to access restrictions in the .NET framework. By using Shims it is possible to redirect calls to these objects with calls to objects that provide your own implementations.
  • 7. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 7 28/07/2015 There are however some restrictions. One restriction is that classes included in the “mscorlib” namespace cannot have any “Fake Assemblies”. Unfortunately, you cannot create any Shims for the “System.Configuration.ConfigurationManager” class for example. The “Fakes Framework” provides some real advantages, when compared to existing mock testing frameworks such as RhinoMock, because developers do not need to modify their functional code for being able to execute unit tests. TDD using Visual Studio 2012:A practicalexample I am now going to show you how to practically use all of those new features and apply them to Test Driven Development. You are going to see a full cycle of TDD using Visual Studio 2012, during the implementation of a simple calculator example! Phase 1: Write a unit test for a new functionality Respecting the Test Driven Development approach, you have to create your unit tests before starting with any implementations. In our example we have to write some unit tests for the methods “Addition” and “Multiplication”. But first of all, we have to add a project of type “Unit Test Project” to our solution (if you do not already have one). You may now add the necessary unit tests. Here is an example of the unit tests you could add based on the unit test framework NUnit:
  • 8. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 8 28/07/2015 Please note that the “Calculator” class and its methods “Addition” and “Multiplication” do not exist at this stage yet. Visual Studio 2012 provides the possibility to generate the missing code in an automatic way. For that you just have to right-click on the “new Calculator” definition in the unit test project and choose to generate the class via the “Generate/New Type” option in the menu. A wizard opens and you are now able to configure multiple options such as the type (classe, struct, interface, enum), the access (public, internal), the destination project and the file name for the generation of the missing class. The next step, after having auto-generated the “Calculator" class, consists of auto-generating the missing methods within this class. This can be achieved in almost the same way as it was done for the missing class. You do a right-click on the method calls “calculator.Addition(...)” and “calculator.Multiplication(...)” in the unit test project and you generate them via the “Generate/Method Stub” option in the menu.
  • 9. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 9 28/07/2015 Here is the auto-generated source code of those two methods: In the last step of this phase you have to open the “Test Explorer” window where you may now execute all your unit tests. This can be done by clicking on the “RunAll” button or by using the already explained “Post Build Test Runs” option. As expected your unit tests will fail, since the corresponding source code has not been implemented yet. We will see how to do that in the next phase. Phase 2: Implement the minimum code necessary to pass the test Now in this phase, the only thing that needs to be done, is to implement the expected functionalities. The idea is to develop the minimum code necessary, which responds to the functional requirements. Everything that concerns optimization and amelioration must not be addressed since it will be treated later in the next phase (refactoring). Following the implementation you may now restart your unit tests by clicking on the “RunAll” button or by using the already explained “Post Build Test Runs” option (see the previous blog post in the series). Your should see that you unit tests have been terminated successfully. If this is not the case you have to review your code and iterate until all of your unit tests pass successfully.
  • 10. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 10 28/07/2015 At this stage the source code corresponds exactly to the functional needs and it provides the expected behavior. The final project structure includes a unit test project as well as an application implementation project. But the source code might not be optimized. Its quality might not adhere to your quality standards, so it has to be ameliorate. The refactoring can be done without any problems since the unit test assure that there are no regressions. Moreover, regressions can be detected very quickly and thus can be handled as soon as possible. This is going to be explained in the next phase. Phase 3: Refactor and optimize the source code The process of improving your source code after the initial implementation phase (Phase 2) is called “Refactoring”. The source code structure is modified internally without any modifications to the external behavior (very important!!). A source code that just “works” is now transformed into a source code that works in an optimal way. Most of the time, the resulting source code is executing with better performance, using less memory and/or with a better software design. The refactoring consists of the following steps (non-exhaustive list):  Detect and eliminate all code duplication  Limit complexity and the number of classes  Simplify and optimize method algorithms  Relocate, rename and harmonize methods
  • 11. TEST DRIVEN DEVELOPMENT WITH VISUAL STUDIO 2012 Author: Hong Le Van Page 11 28/07/2015  Improve code readability  Remove not used code (also called “dead code”)  Add comments to complex code sections In our simple example there is nothing to be refactored, since there are neither enough methods nor enough classes. But this last step has to be done in bigger developments at the end of each cycle. Afterwards, a new development cycle starts with new functionalities from Phase1 on. ConclusionThe TestDriven Developmentwith Visual Studio 2012 As you have seen Visual Studio 2012 was greatly extended and enables efficient Test Driven Development. It helps you on all steps and even enhances and optimizes your productivity.