ExpectedExceptionAttribute 在 MSTest 中不起作用

发布于 2024-07-29 01:41:52 字数 335 浏览 10 评论 0原文

这很奇怪,但是突然有一天,ExpectedExceptionAttribute 不再为我工作。 不确定出了什么问题。 我同时运行 VS 2010 和 VS 2005。 它在 VS 2010 中不起作用。该测试应该通过,但失败了:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_Exception()
{
    throw new ArgumentNullException("test");
}

有什么想法吗? 这真的很sux。

This is weird, but all of a sudden the ExpectedExceptionAttribute quit working for me the other day. Not sure what's gone wrong. I'm running VS 2010 and VS 2005 side-by-side. It's not working in VS 2010. This test should pass, however it is failing:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Test_Exception()
{
    throw new ArgumentNullException("test");
}

Any ideas? This really sux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深陷 2024-08-05 01:41:52

不是为了复活死线程,而是当这突然发生在我身上时我遇到了这个,以防它可以帮助其他人。 我终于找到了问题所在,这可能与乔恩的发现有关。 ExpectedException 属性似乎仅在项目被识别为 TestProject 时才起作用。 (不仅仅是.Net程序集)

卸载项目,编辑csproj文件并检查是否存在以下设置:(

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

假设VS2010项目)
重新加载项目并重建。 ExpectedException 测试现在应该通过。

我们在将测试从 NUnit 标准化到 MSTest 时遇到了这个问题(感谢 TFS CI Build),并发现在替换 Assert.Throws<> 后 美丽的简单和 [ExpectedException(Type)] 的灵活性废话,(更不用说丢失 [TestCase()]!)ExpectedException 测试无缘无故失败。 切换回带有 ExpectedException 的 NUnit,没问题,MSTest 拒绝运行它。

不用说,在发现: http 后,我将努力让 NUnit 回来。 ://blog.shawnewallace.com/2011/02/running-nunit-tests-in-tfs-2010.html

Not to resurrect a dead thread, but I came across this when this all the sudden happened to me, in case it can help others. I did finally track down what the problem was, which may correlate with what Jon found. The ExpectedException attribute appears to only work if the project is recognized as a TestProject. (Not just a .Net assembly)

Unload the project, edit the csproj file and check that the following setting is there:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

(Assuming VS2010 project)
Reload the project and rebuild. ExpectedException tests should now pass.

We ran into this issue when standardizing tests from NUnit to MSTest (Thank you TFS CI Build) and found that After replacing Assert.Throws<> beautiful simplicity & flexibility with [ExpectedException(Type)] crap, (Not to mention losing [TestCase()]!) the ExpectedException tests failed for no reason. Toggle back to NUnit with ExpectedException, no problem, MSTest refuses to run it.

Needless to say I will be pushing to get NUnit back, after finding: http://blog.shawnewallace.com/2011/02/running-nunit-tests-in-tfs-2010.html

水溶 2024-08-05 01:41:52

这个帖子出现在 Google 搜索中,因为我今天也遇到了这个问题,但出于不同的原因,我将在此处添加另一个可能的答案。

我使用 对某些功能进行了单元测试[ExpectedException] 属性已到位,但最近的代码更新使测试的函数变得异步以提高性能。

这导致这些单元测试失败。 简单的解决方案是还使单元测试异步,返回任务并等待函数调用:

[TestMethod]
[ExpectedException(typeof(Exception))]
public async Task UnitTestAnAsyncFunction()
{
    await sut.DoStuffAsync();

    //Assert
    //ExpectedException
} 

This thread came up on a Google search and since I encountered this today too, but for a different reason, I'll add another possible anser here.

I had unit tests for some function with the [ExpectedException] attribute in place, but a recent code update made the function that was tested async to improve performance.

This caused these unit tests to fail. The simple solution was to also make the unit-test async, return Task and await the function-call:

[TestMethod]
[ExpectedException(typeof(Exception))]
public async Task UnitTestAnAsyncFunction()
{
    await sut.DoStuffAsync();

    //Assert
    //ExpectedException
} 
段念尘 2024-08-05 01:41:52

我有另一个答案,不需要编辑 csproj 文件。 看来根本原因只是缺少参考。
我将 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 添加到项目的引用中,并将 using Microsoft.VisualStudio.TestTools.UnitTesting; 添加到代码文件中。 该dll可以在中找到,

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\

或者

C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\

我希望我可以帮助任何人节省一些时间。

I have another answer, which has no need to edit the csproj file. It seems that the root cause was just a missing reference.
I added Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll to the project's references and using Microsoft.VisualStudio.TestTools.UnitTesting; to the code files. The dll can be found in

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\

or

C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\

I hope I could help anyone save some time.

○愚か者の日 2024-08-05 01:41:52

我也遇到过同样的问题,但最终设法让它工作。 不太确定是怎么做的,但这里列出了我在它不起作用和再次开始工作之间所做的事情。

  • 将正在测试的项目转换为.NET 4
  • 关闭CodeCoverage
  • 再次打开CodeCoverage
  • 在测试项目上进行了RebuildAll

但不确定哪位修复了它。 无论如何,希望这有帮助!

I've had the same issue, but finally managed to get it working. Not really sure how but here's a list of things I did between it not working to when it started working again.

  • Converted the project being tested to .NET 4
  • Turned off CodeCoverage
  • Turned CodeCoverage back on again
  • Did a RebuildAll on the test project

Not sure which bit fixed it though. Anyway, hope this helps!

嘿嘿嘿 2024-08-05 01:41:52

还有另一个答案。 今天,我将 Visual Studio 2008 (net35) MSTest 项目升级到 Visual Studio 2017 (net462)。 Marcel Kalinowski 的回答指的是缺少的 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll -就我而言,它就在那里,但引用指向旧的 VS2008 版本,而其余部分已升级。 没有感叹号,没有编译错误,所有单元测试都按预期工作,除了带有 [ExpectedException] 属性的单元测试。

删除引用并添加当前版本(对我来说是 10.0.0.0)使这些测试再次起作用。

And another answer. Today I upgraded a Visual Studio 2008 (net35) MSTest project to Visual Studio 2017 (net462). The answer from Marcel Kalinowski refers to a missing Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll - in my case, it was there, but the reference was pointing to the old VS2008 version, while the rest was upgraded. No exclamation mark, no compilation errors and all unit tests worked as expected except for those with a [ExpectedException] attribute.

Removing the reference and adding the current version (10.0.0.0 for me) made those tests work again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文