22 July 2009

How to turn on code coverage when running unit tests in VSTS 2010

When running tests in Visual Studio Team System 2010, you can collect a lot of information about the code you’re testing and the environment the tests are running in. Previous versions were able to collect code coverage, but that’s was about it. VSTS 2010 provides a number of specific “collectors” that you can choose to enable for your tests. For instance, there is a collector for event log entries, system information and even a video recording of the test run (but this is not that interesting for unit tests :-)) The UI for enabling code coverage in VSTS 20210 has changed to accommodate these new features, and the code coverage settings is grouped together with the other collectors.

The easiest way to enable code coverage in VSTS 2010 is to open your existing Local.testsettings file (in VSTS 2008 these files had the extension testrunconfig), select the Execution Criteria category and enable Code Coverage in the list of Collectors:

Then you have to click the Advanced… button, which displays a dialog that is more or less equivalent to what you find when you select the Code Coverage category in the test run configuration in VSTS 2008. Here you select which assemblies you want to include in the code coverage:

You might not always want to run your unit tests with code coverage, so I recommend clicking Save As... in the Configuration Settings dialog to create a new test settings file.

Another interesting feature of VSTS 2010 is that Visual Studio can now identify which test you should run when modifying your code. This feature is implemented by a collector named Test Impact Collector, and should reduce the number of test you run while coding to only include the ones covering the code you have modified. When you create a new unit test project in Visual Studio 2010, it adds an extra test settings file called TraceAndTestImpact.testingsettings for this purpose, but I haven’t used this feature enough to get a feeling of how effective this functionality yet. I hope to publish another post about this in the future though.

14 July 2009

How to update ReSharper 5 for Visual Studio 2010

Now that the Early Access Program for ReSharper for Visual Studio 2010 has begun, I’m starting to feel comfortable working in VS2010 again. This is a very early build of the new ReSharper and there are some exceptions popping up here and there, but the overall experience is quite good.

One thing you need to now when trying out new builds is that ReSharper uses the new extension mechanism in VS2010. This mechanism allows for much faster installations and improved management, but currently you have to manually uninstall an existing version of ReSharper before installing a new build. You can do this through the Extension Manager window from the Tools menu:

I assume you can skip this step when VS2010 and ReSharper is RTM, because you can use the Extension Manager to search for updates to your installed extensions. You can even configure Extension Manager to automatically detect if updates are available.

09 July 2009

You can create new project templates in VS with your own predefined settings

When I create a new class library or another project type, there are a couple of properties I want to modify right away. This task can be a bit tedious (note to self: DRY), but luckily Visual Studio has a nice feature that allows you to create your own templates. It’s very easy, just make your changes and then select Export template… from the File menu. Make sure Automatically import the template into Visual Studio is checked if you want to use the template right away. What this checkbox does is simply to copy the resulting template zip file to the Visual Studio <Version>\Templates\ProjectTemplates sub folder under your documents folder.

Your new template is now available together with the other out-of-the-box templates when you create a new project:

For those who are interested, these are the class library properties I modify:

  • Application tab
    • If I don’t name the project with full namespace, I change these two properties
      • Assembly name prefix
      • Default namespace prefix
    • Assembly information…
      • Company
      • Copyright
  • Build tab
    • Treat Warning as errors = All
  • Signing
    • Sign the assembly
      I’m adding my own self generated strong name key file
  • Code Analysis
    • Enable Code Analysis on Build
    • For Visual Studio 2010: Rule Set = “Microsoft All Rules”
      I’m running all rules as default and suppress individual messages as needed, but I haven’t evaluated the other rule sets to see if one of those are more appropriate for my needs

I also make a couple of other change in addition to the strong name to satisfy the code analysis rules. The first is to add the CLSCompliant(true) attribute to the AssemblyInfo.cs file, and the second is to suppress code analysis warnings on my name because it thinks my name is misspelled in the namespace of the library.

And lastly, I always remove the Class1.cs file :)