ExpectedExceptionAttribute 在 MSTest 中不起作用
这很奇怪,但是突然有一天,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不是为了复活死线程,而是当这突然发生在我身上时我遇到了这个,以防它可以帮助其他人。 我终于找到了问题所在,这可能与乔恩的发现有关。 ExpectedException 属性似乎仅在项目被识别为 TestProject 时才起作用。 (不仅仅是.Net程序集)
卸载项目,编辑csproj文件并检查是否存在以下设置:(
假设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:
(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
这个帖子出现在 Google 搜索中,因为我今天也遇到了这个问题,但出于不同的原因,我将在此处添加另一个可能的答案。
我使用
对某些功能进行了单元测试[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 testedasync
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:
我有另一个答案,不需要编辑 csproj 文件。 看来根本原因只是缺少参考。
我将
Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
添加到项目的引用中,并将using Microsoft.VisualStudio.TestTools.UnitTesting;
添加到代码文件中。 该dll可以在中找到,或者
我希望我可以帮助任何人节省一些时间。
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 andusing Microsoft.VisualStudio.TestTools.UnitTesting;
to the code files. The dll can be found inor
I hope I could help anyone save some time.
我也遇到过同样的问题,但最终设法让它工作。 不太确定是怎么做的,但这里列出了我在它不起作用和再次开始工作之间所做的事情。
但不确定哪位修复了它。 无论如何,希望这有帮助!
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.
Not sure which bit fixed it though. Anyway, hope this helps!
还有另一个答案。 今天,我将 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.