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

那年仲夏 提交于 2019-12-21 17:39:58

问题


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 MSTest, 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);
    }

回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/5397251/vs2010-sp1-unit-tests-targeting-3-5-framework-fail-if-using-private-accessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!