如何设置 NUnit 来运行我的项目的单元测试?
我开始使用 NUnit 在 .NET 4.0 上的 Visual Studio 2010 中使用 C# 编写测试用例。我想使用 NUnit 来测试 .dll(一个 C# 类库项目)的公共函数。如何设置 NUnit 来处理我的项目?
我应该将 NUnit 代码添加到同一个类库项目中进行测试,还是应该在 NUnit 测试用例的同一解决方案中添加单独的项目?哪种是最佳实践?
如果我需要为 NUnit 测试用例创建一个单独的项目,我应该将其设为类库项目还是可执行文件?如果我把它做成一个类库项目,我该如何运行它?
如果我需要测试可执行文件而不是类库项目,流程和/或项目是否有任何更改?
I'm starting to use NUnit to write test cases in C# with Visual Studio 2010 on .NET 4.0. I want to use NUnit to test against a .dll (a C# class library project)'s public functions. How do I set up NUnit to work with my project?
Should I add NUnit code to the same class library project to test against, or should I add a separate project in the same solution for NUnit test cases? Which is the best practice?
If I need to create a separate project for the NUnit test cases, should I make it a class library project or an executable? If I made it a class library project, how do I run it?
If I need to test against an executable and not a class library project, are there any changes to the process and/or projects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
回复 2
一般来说,将 [测试用例 dll] 与 [业务逻辑 dll] 分开。您的业务逻辑 dll 不应包含任何 NUnit 知识,以分离关注点并简化部署/维护。
您的测试用例 dll 应包含对 NUnit 和业务逻辑 dll 的引用。
您不需要共享名称空间。您可以通过修改业务逻辑 dll 的 AssemblyInfo.cs 文件向测试用例 dll 公开内部成员,从而向测试用例 dll 公开业务逻辑 dll 的内部成员。这使您可以在业务逻辑 dll 中保留所需的可见性。
Re 3
您的测试用例应该放在 dll 中(即类库项目)。您可以将其直接加载到 NUnit 的 UI 中,或者在集成环境中使用 NUnit 的控制台运行程序来自动运行测试。
我是怎么做的:
属性,调试选项卡
将其指向您的 nunit.exe
您的测试用例 dll 的确切名称:
MyTests.dll
省略号按钮,它会
预先选择您的测试 dll 输出
当前构建的目录
config
默认启动项目在
解决方案;这样,每当你击中
F5(或“播放”按钮),NUnit 将
立即提出您的更新测试
预装 - 非常方便
快的。
祝你好运 - 也尝试一下 avl 测试项目类型。在 Visual Studio 中,它与 NUnit 非常相似。我仍然更喜欢 NUnit,但在学习的过程中尝试一些不同的选项是很好的。
Re 2
Generally, keep your [test case dll] separate from your [business logic dll]. Your business logic dll shouldn't include any knowledge of NUnit, to separate concerns and simplify deployment/maintenance.
Your test case dll should include a reference to NUnit, and to your business logic dll.
You do not need to share a namespace. You can expose internal members of your business logic dll to your test case dll by modifying the business logic dll's AssemblyInfo.cs file to expose internals to the test case dll. This lets you preserve your desired visibility in the business logic dll.
Re 3
Your test cases should go in a dll (i.e. class library project). You can load this directly into NUnit's UI, or use NUnit's console runner in an integration environment to run your tests automatically.
How I do it:
properties, Debug tab
point this to your nunit.exe
exact name of your test case dll:
MyTests.dll
ellipsis button and it will
pre-select your test dll output
directory for the current build
config
default start project in the
solution; this way, whenever you hit
F5 (or the "Play" button), NUnit will
come right up with your updated tests
preloaded - very convenient and
quick.
Best of luck - also try out the Test project type avl. in Visual Studio, it's very similar to NUnit. I still prefer NUnit but while learning it's good to try some various options.
关于您的评论:当您将 NUnit.exe 设置为测试类库(包含单元测试的库)的默认启动可执行文件时,您告诉 NUnit 您要测试哪个 DLL;第一次之后,它会记住您正在针对哪个项目运行测试。
您还需要确保单元测试库中有引用其他项目的引用。
同样,我列出的教程涵盖了所有这些内容。
Regarding your comment: When you set NUnit.exe up as the default Start executable for the Test Class Library (the one that contains your Unit Tests), you tell NUnit which DLL you want to test; after the first time it will subsequently remember which project you're running the tests against.
You also want to make sure you have references in the Unit Test library that refer to the other project.
Again, the Tutorial I listed goes through all of this.
2:您应该将所有 NUnit 测试放在同一解决方案的单独项目中。构建项目会构建测试,反之亦然,因此当您对该项目进行 TDD 时(您正在这样做,对吧?),您可以简单地运行测试,它将构建执行此操作所需的所有内容。
3:类库。如果您使用 NUnit,则它不需要可由 Windows 运行;您只需要使用测试运行程序。
2: You should place all NUnit tests in a seperate project in the same solution. Building the project builds the tests, and vice versa, so as you're TDDing this project (you are doing that, right?) you can simply run the tests and it will build everything necessary to do so.
3: Class library. It doesn't need to be runnable by Windows if you're using NUnit; you just need to use the test runner.
我会为测试代码添加一个单独的测试项目,并从中引用 NUnit 和被测库。它应该是一个类库,并且由 NUnit 测试运行程序运行,例如
nunit-console test_ assembly.dll
- 请参阅 文档。I would add a separate test project for the test code and reference NUnit and the library under test from that. It should be a class library, and it gets run by the NUnit test runner(s) e.g.
nunit-console test_assembly.dll
- see the documentation.