Code coverage is used to determine how effectively your tests exercise the code in your application. This way you can identify sections of code that are covered, not covered or partially covered by your tests. Visual Studio uses 2 different types of analysis, block-based statement coverage (C1 coverage) and line-based coverage. - Block-based statement coverage
A block is defined as a sequence of instructions that have a single entry point and a single exit point. Exit points include branch instructions, a function call, a return instruction, or, for managed code, a throw instruction. - Line-based coverage
For line-based coverage, the tools identify all of the blocks that make up a line and then use this information to determine the level of coverage for the line. If all of the blocks that make up the line are covered, then the tools report that the line is covered. If no blocks in the line are covered, then the tools report that the line is not covered. If some, but not all, of the blocks in the line are covered, then the tools report that the line is partially covered. Take for example the following class that reside in MyProject.BusinessLogic assembly public class Foo
{
public int Calculate(int x, int y)
{
if (x > 0 && y < 0)
{
return -1;
}
else
{
return 1;
}
}
And a unit test that reside in MyProject.BusinessLogic.Test assembly
[TestClass]
public class FooTest
{
[TestMethod]
public void Calculate()
{
Foo foo = new Foo();
Assert.AreEqual(1, foo.Calculate(3, 4));
}
}
To enable code coverage you need to double-lick on the LocalTestRun.testrunconfig file that is located in the 'Solution Items' folder.
Inside the 'Code Coverage' tab you select the assembly that you want to instrument. In this case we select MyProject.BusinessLogic.dll assembly.
Now you will need to run your unit tests again. Note that code coverage doesn't work when you debug your unit tests, so you will need to run your unit tests through the menu 'Test –> Run –> All Tests in Solution (CTRL+R, A)'. After that you can view a report about the code coverage results through the menu 'Test –> Windows –> Code Coverage Results'.
From the results we notice that we don't have 100% code coverage because our unit test only reached one part of the condition inside the Calculate method. If you open the Foo class and enable the code coloring you see the parts that are covered, not covered or partially covered.
- Light Blue: Indicates that the entire line of code was exercised in the test run.
- Beige: Indicates that only a portion of the code blocks within the line of code were exercised in the test run.
- Reddish Brown: Indicates that the line was not exercised in the test run.
Code coverage inside Visual Studio uses statement coverage and in this case the number of IL instructions reached is taken into account. If we add some statements in the Foo class and run again our code coverage we notice that the coverage has been raised form 71,43% to 92,59%. It's important to notice, that when you refactor your class it influences the code coverage even when the contract of the class is the same! This is very different from Branch coverage where each control structure is evaluated to true and false. In this case we would have 50% code coverage. public class Foo
{
public int Calculate(int x, int y)
{
if (x > 0 && y < 0)
{
return -1;
}
else
{
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
Console.WriteLine(x.ToString());
return 1;
}
}
}
Often you have the need to iterate through a collection and most of the time the iteration logic is weaved with the action that need to be done. This is because we are used to program in an imperative approach. In some scenarios it's better to use a functional approach and let other functions decide which action need to be applied. This way we can for example reuse our iteration logic. Below is an iterator that starts from a solution or project and iterates through all project items inside the solution. public class ProjectItemIterator : IEnumerable<EnvDTE.ProjectItem>
{
IEnumerable<EnvDTE.Project> projects;
public ProjectItemIterator(EnvDTE.Solution solution)
{
if (solution == null)
throw new ArgumentNullException("solution");
projects = solution.Projects.Cast<EnvDTE.Project>();
}
public ProjectItemIterator(IEnumerable<EnvDTE.Project> projects)
{
if (projects == null)
throw new ArgumentNullException("projects");
this.projects = projects;
}
public IEnumerator<EnvDTE.ProjectItem> GetEnumerator()
{
foreach (EnvDTE.Project currentProject in projects)
foreach (var currentProjectItem in Enumerate(currentProject.ProjectItems))
yield return currentProjectItem;
}
IEnumerable<EnvDTE.ProjectItem> Enumerate(EnvDTE.ProjectItems projectItems)
{
foreach (EnvDTE.ProjectItem item in projectItems)
{
yield return item;
if (item.SubProject != null)
{
foreach (EnvDTE.ProjectItem childItem in Enumerate(item.SubProject.ProjectItems))
yield return childItem;
}
else
{
foreach (EnvDTE.ProjectItem childItem in Enumerate(item.ProjectItems))
yield return childItem;
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
In some cases it can be useful to quickly run your Microsoft unit tests on a machine where Visual Studio is not installed. For example on an end-user machine and/or during acceptance testing. Microsoft unit tests have a great integration with Visual Studio and Team Foundation Server, but unfortunately the unit tests cannot be run as a standalone application. I saw there was an open-source adapter for NUnit, called Microsoft Team System NUnit Adapter from Exact Magic Software that can run Microsoft unit tests inside NAnt. For my unit tests I had some problems with the ExpectedException attribute. Then I noticed there is a project called Gallio and it worked like a charm and it can do a lot more! I noticed that today a new version has been released, namely Gallio v3.0.4. Gallio is a extensible , open and neutral test automation platform. It provides tools and services needed to run and manipulate tests written using a wide range of other frameworks. Gallio can run tests from and it can integrate with the following tools To run the the tests there is a command-line runner, called ‘Echo’ and a graphical user-interface, called ‘Icarus’. 
When trying to edit some code during debug, I received the following dialog of Visual Studio: It appears that the edit and continue feature is not supported when the target is a 64-bit application. On this page you find a nice overview of the scenarios where the edit and continue features are not supported. To resolve the problem, you have to set the target to x86, which can be found in the project properties. 
In my current sample project when the runtime tried to access the MyMeta library (see following post) I always get the following FatalExecutingEngineError exception: It turns out that the problem is that the MyMeta library is compiled under a x86 platform whereas the application is running on a x64 platform. In the properties settings of your visual studio project you have to set the platform target to x86! It was set to 'Any CPU', but now everything works fine. 
Component Dropper is a component that resides in the toolbox of VS.NET and enables you to choose a component (controls, datasets, providers, components, etc.) from the assemblies that are referenced in the current project.
In Visual Studio .NET you can auto populate the controls in the toolbox by setting the AutoToolboxPopulate property to true in menu Tools -> Options -> Windows Forms Designer.
Component Dropper is an alternative way for dropping a component on the designer surface. It gives you a dialog with all the components that reside in the assemblies that are referenced in the current project. This means that it is not limited to the assemblies that reside in the current solution. This way you can easily browse and search throughout the assemblies and controls, this is very handy if you have bunch of assemblies and controls. There is never a need to refresh the toolbox, because it dynamically searches throughout the references in the current project.
Download: ComponentDropper v0.9 (35.04 KB)
There is also a demo (1.11 Mb) that illustrates the use of Component Dropper. Note that the demo uses an older version of Component Dropper, namely version 0.5, and it's not an VS add-in anymore.
If after installation you don't see the component dropper appearing in the toolbox, you can simply drag-and-drop the IStaySharp.RazorToolbox.dll to the toolbox. Any suggestions or remarks are welcome!
PowerCommands is a set of useful extensions for the Visual Studio 2008 adding functionality to various area of the IDE. The features I found very useful are: - Copy/Paste References
- Edit Project File
- Remove and Sort Usings
PowerCommands can be downloaded here and below you find the complete list of features: - Collapse Projects
This command collapses a hierarchy in the solution explorer starting from the root selected node. It can be executed from three different places: solution, solution folders and project nodes respectively. - Copy Class
This command copies a selected class entire content to the clipboard. It can be executed from a single project item or a project item with dependent sub items. - Paste Class
This command pastes a class entire content from the clipboard. It can be executed from a project or folder node. - Copy References
This command copies a reference or set of references to the clipboard. It can be executed from the references node, a single reference node or set of reference nodes. - Paste References
This command pastes a reference or set of references from the clipboard. It can be executed from different places depending on the type of project. For CSharp projects it can be executed from the references node. For Visual Basic and Website projects it can be executed from the project node. - Copy As Project Reference
This command copies a project as a project reference to the clipboard. It can be executed from a project node. - Edit Project File
This command opens the MSBuild project file for a selected project inside Visual Studio. It can be executed from a project node. - Open Containing Folder
This command opens a Windows Explorer window pointing to the physical path of a selected item. It can be executed from a project item node - Open Command Prompt
This command opens a Visual Studio command prompt pointing to the physical path of a selected item. It can be executed from four different places: solution, project, folder and project item nodes respectively. - Unload Projects
This command unloads all projects in a solution. It can be executed from the solution node. - Reload Projects
This command reloads all unloaded projects in a solution. It can be executed from the solution node. - Remove and Sort Usings
This command removes and sort using statements for all classes given a project. It can be executed from a solution node or a single project node. Note: The Remove and Sort Usings feature is only available for C# projects since the C# editor implements this feature as a command in the C# editor (which this command calls for each .cs file in the project). The Visual Basic IDE implements this functionality for Imports in an interactive way: Project properties, go to the References tab, then click the Unused References... button, then select which references you want removed via a listbox. - Extract Constant
This command creates a constant definition statement for a selected text. It can be executed from the code window over a selected text. - Clear Recent File List
This command clears the Visual Studio recent file list. - Clear Recent Project List
This command clears the Visual Studio recent project list. - Transform Templates
This command executes the associated custom tool with text templates items. It can be executed from a DSL project node or a folder node. - Close All
This command closes all documents. It can be executed from a document tab.
Jihaaa, Visual Studio 2008 has been released on MSDN. If you want to have a nice overview of the new features in VS2008, take a look at this post from ScottGu. Take a look at the following downloads available for VS2008
I has been a while that I released a new version of ComponentDropper and I am going to release a new update very soon. I received positive feedback, and I am planning to add some new features and certainly to provide an installer for the add-in. Please leave a comment if you have any suggestions about ComponentDropper.
This macro enables you to nest project items inside Visual Studio .NET. Until now, there is no easy way to nest project items through the Visual Studio IDE, you can only do it by manipulating the project (.csproj or .vbproj) file and adding the DependentUpon element.

Inside the IStaySharp.vsmacros file there is a macro called 'Create Dependency' which allows you to nest two selected items. I have even created a video (672,18 KB) to illustrate how to configure and use the macro.
I am currently building software factories and I needed a way to have a list of running Visual Studio instances and the corresponding EnvDTE._DTE object to manipulate the solution. Windows internally keeps a list of COM objects that are currently running, called the Running Object Table (ROT). VS .NET 2005 for example, register itself in the ROT as "!VisualStudio.DTE.8.0:pid" where 'pid' is the process id of the corresponding VS 2005 instance. In .NET 1.1 you would use the the following UCOMIRunningObjectTable, UCOMIBindCtx for enumerating the ROT. In .NET 2.0 these interfaces are obsolete and are replaced by IRunningObjectTable and BIND_OPTS respectively. Note that the same code can be used for getting other instances like MS Word, IE, etc. using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using EnvDTE;
public static class DTEHelper
{
const uint S_OK = 0;
[DllImport("ole32.dll")]
public static extern uint GetRunningObjectTable(uint reserved, out IRunningObjectTable ROT);
[DllImport("ole32.dll")]
public static extern uint CreateBindCtx(uint reserved, out IBindCtx ctx);
static IDictionary<string, object> GetRunningObjectTable()
{
IDictionary<string, object> rotTable = new Dictionary<string, object>();
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
IMoniker[] monikers = new IMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
monikerEnumerator.Reset();
IntPtr numberFetched = IntPtr.Zero;
while (monikerEnumerator.Next(1, monikers, numberFetched) == 0)
{
IBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
Marshal.ReleaseComObject(ctx);
object runningObjectValue;
runningObjectTable.GetObject(monikers[0], out runningObjectValue);
if (!rotTable.ContainsKey(runningObjectName))
rotTable.Add(runningObjectName, runningObjectValue);
}
return rotTable;
}
public static IDictionary<string, _DTE> GetRunningVSIDETable()
{
IDictionary<string, object> runningObjects = GetRunningObjectTable();
IDictionary<string, _DTE> runningDTEObjects = new Dictionary<string, _DTE>();
foreach (string objectName in runningObjects.Keys)
{
if (!objectName.StartsWith("!VisualStudio.DTE"))
continue;
_DTE ide = runningObjects[objectName] as _DTE;
if (ide == null)
continue;
runningDTEObjects.Add(objectName, ide);
}
return runningDTEObjects;
}
}
On IStaySharp.NET I created an article that shows you, how you can load a DSL file. This is necessary if you need to access your model from GAT, a Visual Studio AddIn, custom library, etc. The last couple of weeks I am digging into DSL and GAT for creating a set of software factories at Real Software that will be used as a baseline for building applications. Learning two (DSL & GAT) simultaneously and combining them was not so trivial and there is some learning curve. For GAT there are some interesting resources: The MSDN forums is a good resource when you have some issues and/or questions. DSL has also a specific forum. The capabilities of DSL are really impressive. Now you have finally the tools to write for example your own dataset designer with code generation!!!
Microsoft announces on the msdn site that service pack 1 for Visual Studio 2005 will appear around Q3 of 2006.
Today I released an add-in for Visual Studio .NET 2005 that allows you to drag components onto the designer surface. It's called ComponentDropper and you can even watch a movie to know what it does and how to use it.  A very good tool for screen capturing is Windows Media Encoder. It's from Microsoft and you can download it, if you have a legal version of Windows XP. Windows Media Encoder 9 Series is a powerful tool for content producers who want to capture audio and video content using the many innovations in Windows Media 9 Series including high-quality multichannel sound, high-definition video quality, support for mixed-mode voice and music content, and more.
RazorToolbox is a set of utilities/tools in the form of addins, macros, components, etc. for Visual Studio.NET. In the initial release of RazorToolbox we have ComponentDropper. In .NET you can easily build components (= also controls), but in order to use them on your designer surface you have to add them in the toolbox of Visual Studio.NET. But there are some problems. For example if you are writing an exe-application and added some components in the exe-project, you cannot add them to the toolbox, because the toolbox only accepts DLLs. Another issue is when you are developing a set of components in a control library, you will have to refresh the toolbox for each component you added in the library, it is not automatically refreshed and is time-consuming. Besides that there are some controls in .NET that are not displayed in the toolbox, for example the PropertyGrid. ComponentDropper allows you to drop a component on the designer surface from a set of assemblies. For example if you are writing an exe-application and added a usercontrol named UserControl1 like the screennshot below: 
Take the ComponentDropper from the toolbox and drag it over the form where you want to drop UserControl1. You get something like:  This list gives the set of components that reside in the current project and the assemblies that are referenced in the project. There you can double-click on UserControl1 and it's added to the form. There are some settings, like you can first build the project before ComponentDropper search for components in the current project. Also you can indicate that ComponentDropper searches in the list of references in the current project. A list of references can be excluded through expressions, for example: System.*.  Download:RazorToolbox.zip (223,59 KB) Any feedback and comments are greatly appreciated!
|