views:

5416

answers:

10

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.

+3  A: 

Install TestDriven.NET, which is a plugin for 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.

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

Jon Limjap
I was hoping for a free solution...
Jon Cage
+9  A: 

When I need to debug my NUnit tests, I simply attach to the NUnit GUI application 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?

Matt Hamilton
I just tried and that does indeed work. If Patrick's answer is a no-go I'll accept this as a workable solution :-)
Jon Cage
+4  A: 

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

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?

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

Patrick McDonald
I've tried what you've suggested (removing the /assembly) but it makes no difference. When I do start new instance it produces the error.I'm thinking it's mostly to do with the fact that when I created the TestDSP project I created it from the built in VisualStudio test project template so it's looking for the wrong testing framework.
Jon Cage
How big is your project? Could you create a new empty project, add a reference to NUnit and add in all your TestFixture classes? If you created the project using the Visual Studio template, can you not use the Visual Studio test runner to run your tests?
Patrick McDonald
Finally got it working. I was right in so much as it was the project options that was stopping it - re-creating the test project using the standard class template fixed the issue.
Jon Cage
+4  A: 

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

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

AB Kolan
That's pretty good although I can't find a way to tell it to just run a single test(?)
Jon Cage
Check out NUnitForVS, http://nunitforvs.codeplex.com/
AB Kolan
A: 

See if this helps.. http://stackoverflow.com/questions/196740/how-to-add-nunit-in-visual-studio

(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.

Gishu
I tried the suggestions there to no avail.You clearly have a good nose. I know my code doesn't work as the output I'm getting for the first block of implementation is getting wildly different answers to my test reference. So now I'm trying to delve deeper to find the cause of the issue. I'd rather do that in isolation to the rest of the program (hence the need to run unit tests in debug mode). For the record, this is code someone else wrote that was converted from another persons algorithm :-/
Jon Cage
So that falls into the latter clause of my last line :) Strange that you can't get it to work though.. too bad. I'd say just attach to process (Alt+D+P) without dwelling on it..
Gishu
A: 

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

+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.

Mike737
See the recent edit - I have a test fixture although it's entirely possible I've not set it up correctly.
Jon Cage
+1  A: 

Remove ProjectTypeGuids from the project file.

Steve
A: 

Regarding what Mr. Patrick McDonald said

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

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.

Abdul Rahman Kayali
A: 

I got the same error with MSTest. I found that in the Test Output window, 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.

Vijay
+3  A: 

Just like Steve said (sorry I can't simply upvote or comment, not enough reputation - i hate it), 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

+1: Thanks; that's good to know :-)
Jon Cage