C# 无法设置 TestContext 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1006702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:22:05  来源:igfitidea点击:

Unable to set TestContext property

c#unit-testing

提问by bsh152s

I have a visual studio 2008 Unit test and I'm getting the following runtime error:

我有一个 Visual Studio 2008 单元测试,但出现以下运行时错误:

Unable to set TestContext property for the class JMPS.PlannerSuite.DataServices.MyUnitTest.  Error:  System.ArgumentException:  Object of type 'Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext' cannot be converted to type 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext'

I have read that VS 2008 does not properly update the references to the UnitTestFramework when converting 2005 projects. My unit test was created in 2008 but it inherits from a base class built in VS 2005. Is this where my problem is coming from? Does my base class have to be rebuilt in 2008? I would rather not do this as it will affect other projects.

我已经读到 VS 2008 在转换 2005 项目时没有正确更新对 UnitTestFramework 的引用。我的单元测试是在 2008 年创建的,但它继承自 VS 2005 中内置的基类。这是我的问题的来源吗?我的基类必须在 2008 年重建吗?我宁愿不这样做,因为它会影响其他项目。

In other derived unit tests built in 2005, all that we needed to do was comment out the TestContext property in the derived unit test. I have tried this in the VS 2008 unit test with no luck. I have also tried to "new" the TestContext property which gives me a different runtime error.

在 2005 年构建的其他派生单元测试中,我们需要做的就是注释掉派生单元测试中的 TestContext 属性。我在 VS 2008 单元测试中尝试过这个,但没有成功。我还尝试“新建” TestContext 属性,这给了我一个不同的运行时错误。

Any ideas?

有任何想法吗?

回答by jcollum

I ran into the same issue. Turned out that one of my projects was referencing the 8.0 assemblies and the other was referencing 9.0. The solution compiled, but I got a runtime exception.

我遇到了同样的问题。结果发现我的一个项目引用了 8.0 程序集,另一个引用了 9.0。解决方案已编译,但出现运行时异常。

回答by KTHL

I'm posting this here in the hopes that it helps someone unit testing a smart device project. I got a very similar error when I first tried to run a unit test for a method in a smart device project I was working on in Visual Studio 2008:

我在此处发布此内容,希望它可以帮助某人对智能设备项目进行单元测试。当我第一次尝试对我在 Visual Studio 2008 中工作的智能设备项目中的方法运行单元测试时,我遇到了一个非常相似的错误:

Error: System.ArgumentException: Object of type 'Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext' cannot be converted to type 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext'

Googling the error returned results that suggested it was caused by upgrading a test project from Visual Studio 2005 format to 2008, and that the reference to the UnitTestFramework.dll was still for the old version (8.0) and required updating. This did not apply to me as I had not upgraded my test project from VS2005. So I continued searching.

谷歌搜索错误返回的结果表明它是由将测试项目从 Visual Studio 2005 格式升级到 2008 引起的,并且对 UnitTestFramework.dll 的引用仍然是旧版本 (8.0) 并且需要更新。这不适用于我,因为我没有从 VS2005 升级我的测试项目。于是我继续寻找。

After hours of searching for other causes for the error without success, I stumbled upon the following references in the smart device project:

经过数小时寻找错误的其他原因但没有成功,我在智能设备项目中偶然发现了以下参考资料:

  • Microsoft.WindowsMobile.dll
  • Microsoft.WindowsMobile.Status.dll
  • 微软.WindowsMo​​bile.dll
  • 微软.WindowsMo​​bile.Status.dll

Visual Studio was obtaining these references from the following folder:

Visual Studio 正在从以下文件夹获取这些引用:

C:\Program Files\Windows Mobile 5.0 SDK R2\Managed Libraries

However, the smart device project's target platform was the Windows Mobile 6 Professional SDK. I didn't notice this before as I did not originally create the smart device project. So I removed the references to the two DLLs and readded them from the following folder:

但是,智能设备项目的目标平台是Windows Mobile 6 Professional SDK。我之前没有注意到这一点,因为我最初没有创建智能设备项目。因此,我删除了对这两个 DLL 的引用,并从以下文件夹中读取了它们:

C:\Program Files\Windows Mobile 6 SDK\Managed Libraries

After rebuilding the smart device project, running the test method succeeded.

重建智能设备项目后,运行测试方法成功。

回答by srock

I had the same problem in Visual Studio 2012 when I upgraded an older project (i think 2005).

当我升级旧项目(我认为是 2005 年)时,我在 Visual Studio 2012 中遇到了同样的问题。

The Reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework needs to be removed by right clicking it in the Solution Explorer then removing it.

对 Microsoft.VisualStudio.QualityTools.UnitTestFramework 的引用需要通过在解决方案资源管理器中右键单击它然后将其删除来删除。

To add the right one:

添加正确的:

  1. Add New Reference
  2. Extensions, find Microsoft.VisualStudio.QualityTools.UnitTestFramework, there are likely several
  3. Mouse over to display the path, choose the one in ../IDE/PublicAssemblies
  4. Clean
  5. Rebuild
  6. Rerun tests
  1. 添加新参考
  2. 扩展,找到Microsoft.VisualStudio.QualityTools.UnitTestFramework,大概有几个
  3. 鼠标悬停显示路径,在 ../IDE/PublicAssemblies 中选择一个
  4. 干净的
  5. 重建
  6. 重新运行测试