开发者

VS2010 SP1 unit tests targeting 3.5 framework fail if using private accessor

开发者 https://www.devze.com 2023-02-19 14:37 出处:网络
I converted a solution from VS2008 to VS2010 SP1, and changed the unit test project to target the 3.5 framework. Other than having to fix a few references in the unit test project, everything worked o

I converted a solution from VS2008 to VS2010 SP1, and changed the unit test project to target the 3.5 framework. Other than having to fix a few references in the unit test project, everything worked ok and the solution built successfully. Most of the tests run successfully, but there were a handful that failed. The ones that failed are using a private accessor. Personally, I'd rather just remove these tests since I don't think they're necessary, but as long as it reveals a potential bug in SP1, I thought I'd see if anyone could figure out a work-around.

The error message that I receive when running the tests is "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded." As best as I can tell, it appears that the private accessor assembly is being built by the 4.0 runtime (most likely via Microsoft.VisualStudio.QualityTools.UnitTestFramework), but since the 3.5 runtime is loaded by MS开发者_开发技巧Test, the resulting error occurs.

I tried changing the reference for Microsoft.VisualStudio.QualityTools.UnitTestFramework to specifically use version 9.0 (currently it is 10.1). This results in a compile time error which says that the private accessor assembly uses version 10.0 of Microsoft.VisualStudio.QualityTools.UnitTestFramework, which is higher than version 9.0.

I've deleted the generated private accessor assembly and recreated it as well, and still have the same issue. It would seem that something is out of sync with VS2010 SP1 when the 3.5 framework is targeted in a unit test project.

Here is the code for one of the unit tests (again, not a very valuable test, but that's not the point of the post...):

    [TestMethod()]
    public void GetNullableCharValue_DBNull_ReturnsNull_Test()
    {
        object value = DBNull.Value;
        Nullable<char> expected = null;
        Nullable<char> actual;
        actual = RepositoryBase_Accessor.GetNullableCharValue(value);
        Assert.AreEqual(expected, actual);
    }


I ran into this problem as well. Visual Studio 2010 SP1 adds support for unit test projects based on .NET v3.5; previously unit tests were forced to use .NET4.

There is a Microsoft Connect bug on the subject but it was just filed the day I'm writing this answer so there is no meaningful response from Microsoft yet.

The workaround I chose was to manually generate the private accessor assembly using Visual Studio 2008 toolchain and add a manual reference to it from the unit test project.

The steps are:

1) Remove the autogenerated accessor from the unit test .csproj file:

<ItemGroup>
  <Shadow Include="Test References\Assembly.accessor" />
</ItemGroup>

2) Create the v3.5 compatible accessor assembly using Publicize VS2008:

"%VS90COMNTOOLS%vsvars32.bat"
publicize Assembly.dll

3) Copy the assembly to the source tree folder, e.g. under a folder Accessors:

copy Assembly_Accessor.dll ProjectDir\Accessors\Assembly_Accessors.dll

4) Add the accessor assembly as a reference to the unit test project using the Visual Studio interface:

Project -> Add Reference.. -> Browse...

5) Build your solution with Ctrl+Shift+B and run your tests.

You can now either check in the generated assembly or create it automatically in a pre-build event.


This solution does not work when the asseblies are signed and versioned or when there is a accessor class.

http://connect.microsoft.com/VisualStudio/feedback/details/677203/even-after-installing-vs2010-sp1-unit-tests-targeting-3-5-framework-fail-if-they-are-using-private-accessor


Try following these steps: http://blogs.msdn.com/b/vstsqualitytools/archive/2010/12/19/how-to-re-target-unit-tests-to-net-framework-3-5-in-vs-2010-sp1.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号