VS2010 SP1 New Features: Unit Tests for Framework 3.5
Versions: 2010 SP1
Code: vstipDebug0041
If you haven’t heard yet, you now have “basic support for unit tests that target the .NET Framework 3.5” in VS2010 with SP1. There are a lot of reasons for adding this functionality but the main reason for putting it in is to support doing unit tests for your SharePoint 2010 projects. The service pack documentation refers to Amit Chatterjee's blog post for more information. Here is the link to the post:
Essentially, you are able to do the following:
- Create new unit test projects and target them to the .NET Framework 3.5.
- Run unit tests that target the .NET Framework 3.5 from Visual Studio 2010 SP1 on your local machine.
- Run unit tests that target the .NET Framework 3.5 using mstest.exe from the command prompt.
- Run unit tests on a build agent as part of a build.
Here are the known limitations:
- The default target framework in test projects is the .NET Framework 4. If you upgrade test projects from Visual Studio 2008, they will be upgraded to target the .NET Framework 4 by default. You can edit the project properties and explicitly re-target the project to the .NET Framework 3.5.
- In the .NET Framework 3.5, multi-targeting is supported for test projects that contain only unit tests. The .NET Framework 3.5 does not support any other test type, such as coded UI or load test. The re-targeting is blocked for test types other than unit tests.
- Execution of .NET Framework 3.5 tests is supported only in the default host adapter. It is not supported using the ASP.NET host adapter. ASP.NET applications that have to run in ASP.NET development server context must be .NET Framework 4-compatible.
- Data collection support is disabled when you execute tests that support .NET Framework 3.5 multi-targeting. You can run code coverage using the Visual Studio command line tools.
- Unit tests using the .NET Framework 3.5 unit tests cannot run on a remote machine.
Now let me show you how you would go about making .NET 3.5 unit tests:
VB
For Visual Basic it’s pretty easy. Just bring up the Add New Project dialog (CTRL + SHIFT + N) and change the Framework to 3.5:
Now go to the Test template area and select the Test Project then click OK:
You will now have a Framework 3.5 test project. You can verify this by going into the project properties, Compile tab, Advanced Compile Options:
You can also change a 4.0 Framework test project to 3.5 by going to this same location as well.
C#
In C# there is no test project template for the Framework 3.5 so you have to first create a 4.0 Test Project:
Then you go into the project properties, Application tab, Target Framework and change the version to 3.5:
You will get the typical warning message, just click Yes:
You will now have a Unit test that targets the 3.5 Framework. You can go back to project properties, Application tab, Target Framework and verify the change:
You are now ready to start writing tests that target the 3.5 Framework.
Comments
Anonymous
April 14, 2011
For some unit test project (containing no load tests, web test, etc...) after the warning message, I have an upgrade wizard that starts automatically. Next, the upgraded project is reloaded and if I go back to project properties/Application tab/Target Framework I see that the .Net Framework 4.0 is back ?!? How can I determine where is the issue with these unit test project s?Anonymous
April 14, 2011
Valery, I haven't seen that behavior before. Can you cancel the upgrade Wizard?Anonymous
April 14, 2011
If I cancel the "Conversion" wizard (that's the exact name), the project appears unloaded in the solution. And if I click to reload the project, the wizard restarts. Comparing the project before and after changing the target framework, I have the following differences :
- in the app.config added : <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
- in the csproj before: <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> after: <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> Comparing the project with the Framework v3.5 as a target and the project after the "Conversion", I see the following differences :
- in the app.config : none
- in the csproj before: <FileUpgradeFlags>40</FileUpgradeFlags> after: <FileUpgradeFlags>0</FileUpgradeFlags> before: <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> after: <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <= reset back to v4.0 What's possibly special in this project:
- use of a GlobalSuppressions.cs file (with [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage...])
- use of a "linked" public key (for delayed strongsign) from a network shared drive (unc path)
- an SQL express database (.mdf) to be attached locally during the unit tests
- and (I just noticed it and I think this is the issue) a "reference file" used by VS to generate private accessors !!! I will investigate this on next Monday...
Anonymous
April 15, 2011
Sounds good :) Post again and let me know if that was it. If not I'll dig deeper.Anonymous
July 19, 2011
How do i unit test with hierarchical data