Поделиться через


How to: Configure Unit Tests to Target .NET Framework 3.5

When you use Visual Studio 2010, the default target version of the .NET Framework in test projects is the .NET Framework 4. Additionally, if you upgrade test projects from Visual Studio 2008, they are 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.

Warning

To re-target test projects to use the .NET Framework 3.5, you must install Visual Studio 2010 Service Pack 1.

Visual Studio 2010 Service Pack 1 enables the following basic support for unit tests that target .NET Framework 3.5:

  • You can create unit test projects and target them to the .NET Framework 3.5.

  • You can run unit tests that target the .NET Framework 3.5 from Visual Studio 2010 Service Pack 1 on your local machine.

  • You can run unit tests that target the .NET Framework 3.5 by using MSTest.exe from the command prompt.

  • You can run unit tests on a build agent as part of a build.

Testing SharePoint 2010 Applications

The capabilities listed above also enable you to write unit tests and integration tests for SharePoint 2010 applications using Visual Studio 2010 Service Pack 1. For more information about how to develop SharePoint 2010 applications using Visual Studio 2010, see SharePoint Development in Visual Studio, Building and Debugging SharePoint Solutions and Verifying and Debugging SharePoint Code by Using ALM Features.

Limitations

The following limitations apply when you re-target your test projects to use the .NET Framework 3.5:

  • In the .NET Framework 3.5, multitargeting 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 in the ASP.NET host adapter. ASP.NET applications that have to run in the ASP.NET Development Server context must be compatible with the .NET Framework 4.

  • Data collection support is disabled when you run tests that support .NET Framework 3.5 multitargeting. You can run code coverage by using the Visual Studio command-line tools.

  • Unit tests that use .NET Framework 3.5 cannot run on a remote machine.

Re-targeting to the .NET Framework 3.5 for Visual Basic Unit Test Projects

  1. Create a new Visual Basic unit test project. On the File menu, select New and then click Project.

    The New Project dialog box is displayed.

  2. Under Installed Templates, expand Visual Basic. Select Test and then select the Test Project template.

  3. In the Name text box, type a name for your Visual Basic test project and then click OK.

  4. In Solution Explorer, right-click your new Visual Basic test project and select Properties.

    The properties for your Visual Basic test project are displayed.

  5. Select the Compile tab and then click the Advanced Compile Options button as shown in the following illustration.

    Advanced Compile Options

  6. Use the Target framework (all configurations) drop-down list to change the target framework from .NET Framework 4 to .NET Framework 3.5 as shown in callout B in the following illustration.

    Target framework drop-down list

    Note

    If you are prompted with an error message, either follow the instructions provided by the link in the message, or see the following procedure Possible Additional Steps to Enable Re-targeting of Test Projects to .NET Framework 3.5.

Re-targeting to the .NET Framework 3.5 for Visual C# Unit Test Projects

  1. Create a new Visual C# unit-test project. On the File menu, select New and then click Project.

    The New Project dialog box is displayed.

  2. Under Installed Templates, expand Visual C#. Select Test and then select the Test Project template.

  3. In the Name text box, type a name for your Visual C# test project and then click OK.

  4. In Solution Explorer, right-click your new Visual C# test project and select Properties.

    The properties for your Visual C# test project are displayed.

  5. Select the Application tab and then use the Target framework drop-down list to change the target framework from .NET Framework 4 to .NET Framework 3.5 as show in the following illustration.

    Target framework drop-down list

    Note

    If you are prompted with an error message, either follow the instructions provided by the link in the message, or see the following procedure Possible Additional Steps to Enable Re-targeting of Test Projects to .NET Framework 3.5.

Re-targeting to the .NET Framework 3.5 for Managed C++/CLI Unit Test Projects

  1. Create a new C++ unit-test project. On the File menu, select New and then click Project.

    The New Project dialog box is displayed.

    Warning

    If you have unit projects that were created using Visual Studio 2008 and you want to build C++/CLI unit test projects that target .NET Framework 3.5 using Visual Studio 2010 Service Pack 1, you must install the Visual Studio 2008 Service Pack 1 on Visual Studio 2008 before you use the projects in Visual Studio 2008. This is a requirement for targeting framework version 3.5. Installing only the .NET Framework 3.5 redistributable will not work. Visual C++ tools such as cl.exe in Visual Studio 2010 are only capable of targeting the .NET 4 Framework. Therefore, to target the .NET Framework 3.5, you must have the tools from that previous version. These tools were released with Visual Studio and not with the .NET Framework.

  2. Under Installed Templates, expand Visual C ++. Select Test and then select the Test Project template.

  3. In the Name text box, type a name for your Visual C++ test project and then click OK.

  4. In Solution Explorer, right-click your new Visual C++ test project and select Unload Project.

  5. In Solution Explorer, right-click the unloaded Visual C++ test project and select Edit <project name>.vcxproj.

    The .vcxproj file opens in the editor.

  6. Set the TargetFrameworkVersion to version 3.5 in the PropertyGroup labeled "Globals":

    <PropertyGroup Label="Globals">
        <TargetName>DefaultTest</TargetName>
        <ProjectTypes>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}</ProjectTypes>
        <ProjectGUID>{CE16D77A-E364-4ACD-948B-1EB6218B0EA3}</ProjectGUID>
        <TargetFrameworkVersion>3.5</TargetFrameworkVersion>
        <Keyword>ManagedCProj</Keyword>
        <RootNamespace>CPP_Test</RootNamespace>
      </PropertyGroup>
    
  7. Save and close the .vcxproj file.

  8. In Solution Explorer, right-click your new Visual C++ test project and select Reload Project.

Possible Additional Steps to Enable Re-targeting of Test Projects to .NET Framework 3.5

If you modified the devenv.exe.config file before you installed Service Pack 1 to Visual Studio 2010, then additional steps are required to enable targeting the .NET Framework 3.5. The installation of Service Pack 1 does not overwrite the configuration file if it detects that is has been modified.

Note

The devenv.exe.config file can be modified if you have installed Visual Studio add-ons or plugins.

If the devenv.exe.config file was modified before you installed Service Pack 1 and the file is not corrected, you will receive an error message in the following situations:

  • You try to re-target to the .NET Framework 3.5 for a test project using Visual Studio 2010 with Service Pack 1 installed.

  • You try to open a test project that has already been re-targeted.

To correct this issue, use the following procedure.

To Add Missing XML Entries to the Configuration File

  1. Close all instances of Visual Studio 2010.

  2. Open the Visual Studio 2010 Command Prompt as an Administrator.

  3. Change the directory to: "%programfiles%\MicrosoftVisual Studio 10.0\Common7\IDE" if you are using 32-bit Windows or "%programfiles(x86)%\MicrosoftVisual Studio 10.0\Common7\IDE" if you are using 64-bit Windows.

  4. Make a backup copy of the devenv.exe.config file.

  5. Open the devenv.exe.config file in a text editor, such as notepad.exe.

  6. Add the following entries under the <assemblyBinding> node:

    <dependentAssembly>
    
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.UnitTestFramework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
    
          </dependentAssembly>
    
          <dependentAssembly>
    
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
    
          </dependentAssembly>
    
          <dependentAssembly>
    
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
    
          </dependentAssembly>
    
          <dependentAssembly>
    
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Tip" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
    
          </dependentAssembly>
    
  7. Add the following entry under the <configuration> node:

    Important

    Do not add this new element to the top of the file. The first child node of <configuration> must be <configSections>.

    <appSettings>
        <add key="TestProjectRetargetTo35Allowed" value="true" />
     </appSettings>
    
  8. Save the devenv.exe.config file.

  9. Restart Visual Studio 2010.

    You should now be able to re-target to the .NET Framework 3.5 for your test project.

See Also

Concepts

Creating and Running Unit Tests for Existing Code

Other Resources

SharePoint Development in Visual Studio

Building and Debugging SharePoint Solutions