如何设置NUnit来运行项目的单元测试?

时间:2023-01-13 12:35:57

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?

我开始使用NUnit在.NET 4.0上使用Visual Studio 2010在C#中编写测试用例。我想使用NUnit来测试.dll(一个C#类库项目)的公共函数。如何设置NUnit以使用我的项目?

  1. 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?

    我应该将NUnit代码添加到同一个类库项目进行测试,还是应该在同一个NUnit测试用例的解决方案中添加一个单独的项目?哪种是最佳做法?

  2. 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?

    如果我需要为NUnit测试用例创建一个单独的项目,我应该将它作为类库项目还是可执行文件?如果我把它作为一个类库项目,我该如何运行它?

  3. If I need to test against an executable and not a class library project, are there any changes to the process and/or projects?

    如果我需要测试可执行文件而不是类库项目,那么进程和/或项目是否有任何更改?

4 个解决方案

#1


15  

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.

通常,将[测试用例dll]与[业务逻辑dll]分开。您的业​​务逻辑DLL不应包含任何NUnit知识,以分离关注点并简化部署/维护。

Your test case dll should include a reference to NUnit, and to your business logic dll.

您的测试用例dll应该包含对NUnit和业务逻辑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.

您不需要共享命名空间。您可以通过修改业务逻辑dll的AssemblyInfo.cs文件以将内部暴露给测试用例dll,将业务逻辑dll的内部成员公开给测试用例dll。这使您可以在业务逻辑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.

您的测试用例应该进入dll(即类库项目)。您可以将其直接加载到NUnit的UI中,或者在集成环境中使用NUnit的控制台运行器来自动运行测试。

How I do it:

我是怎么做到的

  • open test case class library project properties, Debug tab
  • 打开测试用例类库项目属性,Debug选项卡

  • set to open with external program: point this to your nunit.exe
  • 设置为使用外部程序打开:将其指向您的nunit.exe

  • set command line arguments to the exact name of your test case dll: MyTests.dll
  • 将命令行参数设置为测试用例dll的确切名称:MyTests.dll

  • working directory: just click the ellipsis button and it will pre-select your test dll output directory for the current build config
  • 工作目录:只需单击省略号按钮,它将预先选择当前构建配置的测试dll输出目录

  • set the test project to be the 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.
  • 将测试项目设置为解决方案中的默认启动项目;这样,无论何时按F5(或“播放”按钮),NUnit都会预先加载您更新的测试 - 非常方便快捷。

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.

祝你好运 - 同时试用测试项目类型avl。在Visual Studio中,它与NUnit非常相似。我仍然更喜欢NUnit但是在学习它的同时尝试一些不同的选择是很好的。

#2


8  

  1. The Unit Tests should go in a separate Test project under the same solution.
  2. 单元测试应该在同一解决方案的单独测试项目中进行。

  3. Yes, make it a class library project. If you want to run the tests in NUnit, this tutorial can show you how.
  4. 是的,使它成为一个类库项目。如果您想在NUnit中运行测试,本教程可以向您展示如何操作。

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.

关于你的评论:当你将NUnit.exe设置为测试类库(包含你的单元测试的那个)的默认Start可执行文件时,你告诉NUnit你要测试哪个DLL;在第一次之后,它将随后记住您正在运行测试的项目。

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.

再次,我列出的教程经历了所有这些。

#3


2  

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.

2:您应该将所有NUnit测试放在同一解决方案中的单独项目中。构建项目构建测试,反之亦然,所以当你正在TDD这个项目时(你正在这样做,对吗?)你可以简单地运行测试,它将构建所有必要的东西。 3:类库。如果你使用NUnit,它不需要由Windows运行;你只需要使用测试运行器。

#4


2  

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.

我将为测试代码添加一个单独的测试项目,并从中引用NUnit和正在测试的库。它应该是一个类库,它由NUnit测试运行器运行,例如nunit-console test_assembly.dll - 请参阅文档。

#1


15  

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.

通常,将[测试用例dll]与[业务逻辑dll]分开。您的业​​务逻辑DLL不应包含任何NUnit知识,以分离关注点并简化部署/维护。

Your test case dll should include a reference to NUnit, and to your business logic dll.

您的测试用例dll应该包含对NUnit和业务逻辑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.

您不需要共享命名空间。您可以通过修改业务逻辑dll的AssemblyInfo.cs文件以将内部暴露给测试用例dll,将业务逻辑dll的内部成员公开给测试用例dll。这使您可以在业务逻辑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.

您的测试用例应该进入dll(即类库项目)。您可以将其直接加载到NUnit的UI中,或者在集成环境中使用NUnit的控制台运行器来自动运行测试。

How I do it:

我是怎么做到的

  • open test case class library project properties, Debug tab
  • 打开测试用例类库项目属性,Debug选项卡

  • set to open with external program: point this to your nunit.exe
  • 设置为使用外部程序打开:将其指向您的nunit.exe

  • set command line arguments to the exact name of your test case dll: MyTests.dll
  • 将命令行参数设置为测试用例dll的确切名称:MyTests.dll

  • working directory: just click the ellipsis button and it will pre-select your test dll output directory for the current build config
  • 工作目录:只需单击省略号按钮,它将预先选择当前构建配置的测试dll输出目录

  • set the test project to be the 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.
  • 将测试项目设置为解决方案中的默认启动项目;这样,无论何时按F5(或“播放”按钮),NUnit都会预先加载您更新的测试 - 非常方便快捷。

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.

祝你好运 - 同时试用测试项目类型avl。在Visual Studio中,它与NUnit非常相似。我仍然更喜欢NUnit但是在学习它的同时尝试一些不同的选择是很好的。

#2


8  

  1. The Unit Tests should go in a separate Test project under the same solution.
  2. 单元测试应该在同一解决方案的单独测试项目中进行。

  3. Yes, make it a class library project. If you want to run the tests in NUnit, this tutorial can show you how.
  4. 是的,使它成为一个类库项目。如果您想在NUnit中运行测试,本教程可以向您展示如何操作。

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.

关于你的评论:当你将NUnit.exe设置为测试类库(包含你的单元测试的那个)的默认Start可执行文件时,你告诉NUnit你要测试哪个DLL;在第一次之后,它将随后记住您正在运行测试的项目。

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.

再次,我列出的教程经历了所有这些。

#3


2  

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.

2:您应该将所有NUnit测试放在同一解决方案中的单独项目中。构建项目构建测试,反之亦然,所以当你正在TDD这个项目时(你正在这样做,对吗?)你可以简单地运行测试,它将构建所有必要的东西。 3:类库。如果你使用NUnit,它不需要由Windows运行;你只需要使用测试运行器。

#4


2  

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.

我将为测试代码添加一个单独的测试项目,并从中引用NUnit和正在测试的库。它应该是一个类库,它由NUnit测试运行器运行,例如nunit-console test_assembly.dll - 请参阅文档。