如何使用 ReSharper 创建 NUnit 测试?
我正在尝试使用 C# 进行单元测试。很多人告诉我使用 NUnit,因为它比 MSTest 更好(显然,我不知道),而且它在我正在使用的 ReSharper 中也有很好的支持。
现在我以前从未编写过单元测试(请耐心等待,我是一名大学生)。 ReSharper 有一个很好的 CreateUnitTests 上下文菜单选项,我看到其他人(不经意地回头看)使用它取得了巨大的成功。您右键单击方法,选择 CreateUnitTests,然后就创建了一个测试框架。您只需填写重要的部分即可。
现在,当我尝试相同的操作时,ReSharper 希望我创建一个新的测试项目...当我允许它时,它会创建(我假设的)一个带有明显 MSTest 测试模板的 MSTest 项目。但我已经有一个类库项目,它引用“nunit.framework”,并且有一些 ReSharper 非常愿意运行的 NUnit 测试。尽管如此,它仅创建 MSTest 测试模板,并且仅在特殊的“测试项目”项目中。
我做错了什么?我是否做错了什么,或者使用 ReSharper 无法创建 NUnit 测试模板?我已经搜索过网络并阅读了 ReSharper 和 NUnit 的文档,但我仍然不知道这是否可能或是什么。
如果有人能为我提供使用 ReSharper + NUnit 的指南,我将不胜感激。
编辑:我正在使用 ReSharper 4.5 和 NUnit 2.5.3
编辑2:显然我是个白痴。 CreateUnitTests 不是 ReSharper 的一部分,而是 Visual Studio 的一部分,因此只能与 MSTest 配合使用。
I'm trying to get into unit testing with C#. Various people told me to go with NUnit since it's better than MSTest (apparently, I have no idea) and it also has very good support in ReSharper which I'm using.
Now I've never written a unit test before in my life (bear with me, I'm a university student). ReSharper has this nice CreateUnitTests context menu option that I've seen others (casually looking over the shoulder) use to great success. You right-click in a method, select CreateUnitTests and there you go, a test skeleton is created. You just fill in the important bits.
Now when I try the same, ReSharper wants me to create a new Test Project... and when I let it, it creates (what I'm assuming) a MSTest project with obviously a MSTest test template. But I already have a class libarary project which references "nunit.framework" and has a few NUnit tests that ReSharper is more than willing to run. Still, it only ever creates MSTest test templates, and only ever in special "Test Project" projects.
What am I doing wrong? Am I doing something wrong at all, or is creating NUnit test templates not possible with ReSharper? I've searched the net and read the documentation of ReSharper and NUnit and I still can't figure out is this even possible or what.
I'd be grateful if anyone could provide me with a sort of guide to using ReSharper + NUnit.
EDIT: I'm using ReSharper 4.5 and NUnit 2.5.3
EDIT2: Apparently I'm an idiot. CreateUnitTests is not part of ReSharper, but part of Visual Studio and thus only ever works with MSTest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Resharper 创建的测试项目中,删除对 Microsoft 单元测试 DLL 的引用(我不记得这个名称了,但它是一个很长的名称)。
然后添加一个新的引用 - nunit.framework.dll,您应该在“添加引用”对话框的第一个选项卡上找到它。
将
using NUnit.Framework
添加到单元测试类文件中。然后,您需要更改属性:
因此,如果您最终得到的是 MSTest 项目,请使用上述步骤来获取 NUnit。
注意:Resharper 4.5 及以上版本确实支持运行 MSTest 以及 NUNit 测试。所以你可以尝试一下。
In your test project which Resharper created, remove the reference to the Microsoft unit testing DLL (I don't recall the name off hand, but it's quite a long name).
Then add a new reference - nunit.framework.dll, you should find it on the first tab of the Add Reference dialog.
Add
using NUnit.Framework
to to the unit test class file.You then need to change attributes:
So if you end up with a MSTest project, use the above steps to get NUnit instead.
NOTE: Resharper 4.5 onwards does have native support for running MSTest as well as NUNit tests. So you could try that instead.
您无需运行任何向导即可使用 NUnit。您只需创建一个类库,添加对 NUnit 的引用并使用相应的属性标记您的测试。向导仅适用于 MSTest,即使如此,也不是必需的。
一旦您进行了单元测试,ReSharper 测试运行程序将检测它们,并且在左侧边缘您将获得一些允许您运行/调试测试的图标。请参阅此处的第一张图片作为示例:
NUnit 和 ReSharper
You don't need to run any wizards to use NUnit. You can just create a class library, add a reference to NUnit and mark your tests with the corresponding attribute. The Wizards are only for MSTest and even then, not required.
Once you have the unit tests, the ReSharper test runner will detect them and on the left-hand margin you'll get some icons that will allow you to run/debug tests. See the first image here for an example:
NUnit and ReSharper