C# 如何从 Visual Studio 在调试模式下运行 NUnit?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/759854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:09:15  来源:igfitidea点击:

How do I run NUnit in debug mode from Visual Studio?

c#visual-studio-2008unit-testingtestingnunit

提问by Jon Cage

I've recently been building a test framework for a bit of C# I've been working on. I have NUnit set up and a new project within my workspace to test the component. All works well if I load up my unit tests from Nunit (v2.4), but I've got to the point where it would be really useful to run in debug mode and set some break points.

我最近一直在为我一直在研究的一些 C# 构建一个测试框架。我在我的工作区中设置了 NUnit 和一个新项目来测试组件。如果我从 Nunit (v2.4) 加载我的单元测试,一切都很好,但是我已经到了在调试模式下运行并设置一些断点非常有用的地步。

I've tried the suggestions from several guides which all suggest changing the 'Debug' properties of the test project:

我已经尝试了几个指南中的建议,这些指南都建议更改测试项目的“调试”属性:

Start external program: C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe
Command line arguments: /assembly: <full-path-to-solution>\TestDSP\bin\Debug\TestDSP.dll

I'm using the console version there, but have tried the calling the GUI as well. Both give me the same error when I try and start debugging:

我在那里使用控制台版本,但也尝试过调用 GUI。当我尝试开始调试时,两者都给我同样的错误:

Cannot start test project 'TestDSP' because the project does not contain any tests.

Is this because I normally load \DSP.nunit into the Nunit GUI and that's where the tests are held?

这是因为我通常将 \DSP.nunit 加载到 Nunit GUI 中,这就是进行测试的地方吗?

I'm beginning to think the problem may be that VS wants to run it's own test framework and that's why it's failing to find the NUnit tests?

我开始认为问题可能是 VS 想要运行它自己的测试框架,这就是为什么它找不到 NUnit 测试?

Edit: To those asking about test fixtures, one of my .cs files in the TestDSP project looks roughly like this:

编辑:对于那些询问测试装置的人,我在 TestDSP 项目中的一个 .cs 文件大致如下所示:

namespace Some.TestNamespace
{
    // Testing framework includes
    using NUnit.Framework;

    [TestFixture]
    public class FirFilterTest
    {
        [Test]
        public void Test01_ConstructorTest()
        {
            ...some tests...
        }
    }
}

...I'm pretty new to C# and the NUnit test framework so it's entirely possible I've missed some crucial bit of information ;-)

...我对 C# 和 NUnit 测试框架还很陌生,所以我完全有可能错过了一些关键的信息;-)

Final Solution: The big problem was the project I'd used. If you pick Other Languages -> Visual C# -> Test -> Test Project...when you're choosing the project type, Visual Studio will try and use it's own testing framework as far as I can tell. You should pick a normalC# class library project instead and then the instructions in my selected answer will work.

最终解决方案:最大的问题是我使用的项目。如果您选择Other Languages -> Visual C# -> Test -> Test Project...当您选择项目类型时,Visual Studio 将尝试使用它自己的测试框架,据我所知。您应该选择一个普通的C# 类库项目,然后我选择的答案中的说明将起作用。

采纳答案by Patrick McDonald

I use the same technique as you are trying Jon, without the /assembly flag, i.e.

我使用与您尝试 Jon 相同的技术,没有 /assembly 标志,即

Start External Program: C:\Program Files\NUnit 2.4.8\bin\nunit.exe

Command line arguments: "<path>\bin\Debug\Quotes.Domain.Tests.dll"

Does TestDSP.dll contain all your TestFixtures?

TestDSP.dll 是否包含您所有的 TestFixtures?

As my test project is not the startup project in the solution, I run my tests by right-clicking on the test project and choosing Debug --> Start New Instance

由于我的测试项目不是解决方案中的启动项目,我通过右键单击测试项目并选择 Debug --> Start New Instance 来运行我的测试

回答by Jon Limjap

Install TestDriven.NET, which is a plugin for Visual Studio

安装TestDriven.NET,它是 Visual Studio 的插件

From there you can right click on your unit test assembly and click Run Tests to run the whole suite, right click on a TestFixture class to run just the tests in that class, or right click on a Test method to run just that method.

从那里,您可以右键单击单元测试程序集并单击运行测试以运行整个套件,右键单击 TestFixture 类以仅运行该类中的测试,或右键单击测试方法以仅运行该方法。

You also have the option to Test With Debugger, if you need to breakpoint into your tests in debug mode.

如果您需要在调试模式下对测试进行断点,您还可以选择使用调试器进行测试。

回答by Matt Hamilton

When I need to debug my NUnit tests, I simply attach to the NUnit GUI application nunit-agent.exeusing "Debug|Attach to Process" and run the tests from the GUI. Any breakpoints in my tests (or the code they're testing) are hit. Am I misunderstanding your question, or will that work for you?

当我需要调试我的 NUnit 测试时,我只需nunit-agent.exe使用“Debug|Attach to Process”附加到 NUnit GUI 应用程序并从 GUI 运行测试。我的测试(或他们正在测试的代码)中的任何断点都被命中。我误解了你的问题,还是对你有用?

回答by abhilash

Try NUnitit - a open source Visual Studio Addin for Debugging NUnit Test cases

尝试 NUnitit - 一个用于调试 NUnit 测试用例的开源 Visual Studio 插件

HomePage - http://nunitit.codeplex.com/

主页 - http://nunitit.codeplex.com/

回答by Gishu

See if this helps.. How to add NUnit in Visual Studio

看看这是否有帮助.. 如何在 Visual Studio 中添加 NUnit

(RighteousRant)Although personally I don't like this approach.. If you need a debugger while you are test-driving your code, it's a "smell" in that you do not have enough confidence/know how your code works & need the debugger to tell you that. TDD should free you from needing a debugger if done right. Use 'Attach debugger to NUNit' only for rare cases or when you are wading in someone else's code.

(RighteousRant)虽然我个人不喜欢这种方法..如果你在测试驱动代码时需要调试器,这是一种“气味”,因为你没有足够的信心/不知道你的代码是如何工作的,需要调试器告诉你。如果做得好,TDD 应该让您无需调试器。仅在极少数情况下或当您涉足其他人的代码时才使用“将调试器附加到 NUIt”。

回答by Mike737

It sounds like you are trying to use the wrong library. NUnit can only start if the dll you are using contains TestFixtures.

听起来您正在尝试使用错误的库。只有当您使用的 dll 包含 TestFixtures 时,NUnit 才能启动。

+1 on TestDriven.Net. I've had the chance to use it a number of times. You can download the personal version for evaluations purposes according the the license at http://testdriven.net/purchase_licenses.aspx.

在 TestDriven.Net 上 +1。我有机会多次使用它。您可以根据http://testdriven.net/purchase_licenses.aspx 上的许可证下载用于评估目的的个人版本。

回答by Steve

Remove ProjectTypeGuids from the project file.

从项目文件中删除 ProjectTypeGuids。

回答by Abdul Rahman Kayali

Regarding what Mr. Patrick McDonaldsaid

关于帕特里克·麦克唐纳先生所说的话

As my test project is not the startup project in the solution, I run my tests by right-clicking on the test project and choosing Debug --> Start New Instance

由于我的测试项目不是解决方案中的启动项目,我通过右键单击测试项目并选择 Debug --> Start New Instance 来运行我的测试

I tried to apply for my test class library but got some error regarding the path, so I tried to remove the 'Command Line Arguments', and luckily it worked well and as expected.

我尝试申请我的测试类库,但在路径方面遇到了一些错误,因此我尝试删除“命令行参数”,幸运的是它运行良好且符合预期。

回答by Vijay

I got the same error with MSTest. I found that in the Test Outputwindow, some of the tests had duplicate IDs and could not be loaded. I removed all duplicate tests and now I was able to run the tests when i start the project.

我在使用 MSTest 时遇到了同样的错误。我发现在Test Output窗口中,有些测试有重复的ID,无法加载。我删除了所有重复的测试,现在我可以在启动项目时运行测试。

回答by user276648

Simply remove the line that looks like

只需删除看起来像的行

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

from your project file. This line basically tells VS.Net that it's a Test project, thus the "Cannot start test project". FYI here the 1st Guid says "it's a test", the 2nd says "it's C#". For information on those Guids: http://www.mztools.com/Articles/2008/MZ2008017.aspx

从您的项目文件。这一行基本上告诉 VS.Net 它是一个测试项目,因此“无法启动测试项目”。仅供参考,第一个 Guid 说“这是一个测试”,第二个说“它是 C#”。有关这些指南的信息:http://www.mztools.com/Articles/2008/MZ2008017.aspx