How do I run NUnit in debug mode from Visual Studi

2019-01-08 03:40发布

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.

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:

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?

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?

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

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 ;-)

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 normal C# class library project instead and then the instructions in my selected answer will work.

19条回答
来,给爷笑一个
2楼-- · 2019-01-08 04:19

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-08 04:19

In addition to the answer provided by @Justin here are some more details for NUnit 2.6.

Using NUnit 2.6 attach to nunit.exe or nunit-console.exe and NOT the agent. The configuration noted by @Justin is slightly different. Below is an example from nunit.exe.config (same for nunit-console.exe.config).

<startup useLegacyV2RuntimeActivationPolicy="true">
  <!-- Comment out the next line to force use of .NET 4.0 -->
  <supportedRuntime version="v2.0.50727" />  
  <supportedRuntime version="v4.0.30319" />
</startup>

For .NET 4 test project, to get break points to hit, you will have to comment out or remove the v2.0 line as the comment suggests. Once I did that I was able to debug the .NET 4.0 test project.

查看更多
ら.Afraid
4楼-- · 2019-01-08 04:20

When I need to debug my NUnit tests, I simply attach to the NUnit GUI application nunit-agent.exe using "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?

查看更多
甜甜的少女心
5楼-- · 2019-01-08 04:26

Now with pictures:

  1. Run NUnit gui (Download 2.6.2 from here) then go to File -> Open Project

enter image description here

  1. Select your test .dll from bin folder (C:\......\[SolutionFolder][ProjectFolder]\bin\Debug\xxxxTests.dll)

  2. Go to Visual Studio Debug -> Attach to process (Attach to process window will open)

  3. From the list scroll down and select nunit-agent.exe then click Attach

enter image description here

  1. At this point breakpoints in your tests should turn ripe red (from hollow).

  2. Click Run on Nunit Gui and you should get your breakpoint hit...

Hope this saves you some time.

查看更多
贼婆χ
6楼-- · 2019-01-08 04:32

In Nunit 3.0.1 (I'm using VS2013), Open from main menu > Test > Windows > Test Explorer. Then in "Test explorer", right-click the test case, you might see: enter image description here

Hope this helps.

查看更多
相关推荐>>
7楼-- · 2019-01-08 04:32

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

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

查看更多
登录 后发表回答