简体   繁体   中英

What is the best way to debug a NUnit test?

My platform: Visual C# 2008 Express Edition with NUnit 2.2.7

I have a solution with my code in one project and my NUnit unit tests in a different project in the same solution.

I have been struggling hard to debug and single-step through the NUnit tests. I found some references online that suggested calling the following:

NUnit.ConsoleRunner.Runner.Main(args);

But this doesn't even compile - it has the compiler error:

Error 1 The type or namespace name 'Runner' does not exist in the namespace 'NUnit.ConsoleRunner' (are you missing an assembly reference?)

I've added every assembly reference I could find, to no effect.

Finally, this is what I have hacked together and it works, but perhaps you good readers could suggest a better solution:

1) In my test project, the class name of a test case that I want to debug is MyTestClass. It has a [TestFixtureSetUp] method named Init() and the actual test case is in [Test] function MyTest()

2) In my code project, I have a console program TestProgram.cs which compiles to an EXE.

In TestProgram.cs, I call the test cases in the following way

// First instantiate the test class
MyTestClass tc = new MyTestClass();

// Call the TestFixtureSetup method
tc.Init();

// Now call the actual test
tc.MyTest();

This works and I can debug and single step through the test cases.

If anyone has any better suggestions using Visual Studio 2008 Express without paying for additional plugins , I appreciate your advice.

Jon as a good way but not the more automatic one ;)

Here is what I already say on SO :

You can create a blank project (choose console application for example) and in the property of the project you can select DEBUG tag and select Start External Program . Put the path of Nunit. Than, in the start option, the command line arguments select the DLL that contain all your test (mine is always in the nunit\\bin...). Than select Enable unmanaged code debugging and you will be able to start the project inside VS and even use the debugger step-by-step.

This way, you do not need macro or anything to be done every time, it's just a normal F5 :)

Since you're using an Express version of Visual Studio, you cannot use the free TestDriven.NET add-in. That's unfortunate, because that really is the best tool I've used to debug unit tests.

The Runner class can be found in the nunit-console-runner.dll assembly, so be sure to add a reference to that. I'm not sure there's anything it does that your simple entry point does not, but it is probably best to use it to be safe and for future compatibility.

One other (dirty) option worth mentioning is for your unit test to invoke Debugger.Attach() . I haven't tried this with an express install, but you should be prompted to attach a debugger when your unit test runs. You can then select your existing VS instance and debug with that.

Have a look at http://nunit.com/blogs/?p=28

This works a treat.

At my shop, we're doing it exactly as you do. On the downside, this means that setup might not be exactly as NUnit does it. On the upside, it is quite simple to do and allows one to create a minimal environment to reproduce the bug.

I found some references online that suggested calling the following: NUnit.ConsoleRunner.Runner.Main(args); But this doesn't even compile - it has the compiler error:

JFYI You need to add a .NET DLL reference to nunit-console-runner assembly in the NUnit DLL bundle ( I have 2.2.4.0 ) Seems like the class has also been renamed but its there if you need it.

NUnit.ConsoleRunner.ConsoleUi.Main(args);

If you have ReSharper installed it should be able to detect your [Test] fixtures and place an icon to the left of each method to run, and to the left of each test fixture to run all. I prefer this way.

Most of the people at work also have TestDriven.NET, which for some reason isn't on my machine, so they wonder what's going on. It's kinda funny.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM