Unit testing with Microsoft Visual Studio
Microsoft Visual Studio 2012 comes with a large amount of new features and improvements in almost all areas. The changes introduced in the “unit testing” and generally “testing” area are very significative.
I’ve read the following sentence in one of the Microsoft slides and I couldn’t agree more:
Microsoft was working on making the testing framework extensible, indeed it allow us to use other testing frameworks such as xUnit.net and Nunit, something that in the past, in order to have something built into Visual Studio we had to use external plugins such as Resharper or others. All this can be seen in the below picture that depicts the current Microsoft Visual Studio Unit Test Platform that is in fact a Unit Test Runners-Runner. Yes, the responsibility of this layer is to run the third party unit test runners and it is supposed to be the “glue” between the user and the actual underlying unit testing framework.
Visually, the most important addition to the Visual Studio Unit testing is the so called Test Explorer
Test explorer supports a number of features (as illustrated in the picture) such as:
- Search: it is possible to search within the test names and execute only those displayed in the search
- Red-Green bar: Bar at the top of the window will display if the tests have been failed (red) or if everything have run smoothly.
- Most important tests will be shown first (at the top)
- Timings: Each tests has the duration so we can see quickly how long it takes for a test to execute and fix it if it runs slowly.
- Shows tests from any framework.
- Run details: there are details being shown in the bottom part that will tell why the test failed or other information.
Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested.
In the picture below you may see how the information are displayed directly in Visual Studio showing which part of the code haven’t been covered tested (in red) with a number of statistics around it.
Previously in order to see such number of statistics we had to use third party tools such as NCover, JetBrains dotCover or others.
Isolating code for better testing
There is another improvement named VS11 Fakes framework that lets us isolate almost anything in .NET. There are two flavors of fakes:
- Stubs: concrete implementations of interfaces or abstract classes. VS offers an easy way of creating stubs something that we previously had to do manually or using third part frameworks such as Moq, or others. It is not fully a replacement but a good start.
- Shims: run-time interception lets you replace calls, even those from the .NET Base Class Library.
Fakes and shims visually represented:
Continous testing in Visual Studio 2012 gives a possibility of continuously running the tests withing the solution. It helps you with the way you do TDD by taking care of all compiling and testing in the background to help you work more efficiently.
This framework could potentially replace the third party tools such as ContinousTest, NCrunch, or others.
Visual Studio 2012 has a list of improvements that will make the unit testing simpler and effective. The number of features is quite large so I encourage you further check and experience it by yourself. As I haven’t had the time to fully try all the above explained items, I am not yet sure if the above listed features would replace the other third party frameworks, but this is already a good step forward. Microsoft is showing us that it is moving into the right direction.