简体   繁体   English

调试NUnit测试的最佳方法是什么?

[英]What is the best way to debug a NUnit test?

My platform: Visual C# 2008 Express Edition with NUnit 2.2.7 我的平台:带有NUnit 2.2.7的Visual C#2008 Express Edition

I have a solution with my code in one project and my NUnit unit tests in a different project in the same solution. 我的代码在一个项目中有一个解决方案,我的NUnit单元在同一个解决方案中的另一个项目中进行测试。

I have been struggling hard to debug and single-step through the NUnit tests. 我一直在努力调试和单步执行NUnit测试。 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?) 错误1命名空间“NUnit.ConsoleRunner”中不存在类型或命名空间名称“Runner”(您是否缺少程序集引用?)

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. 1)在我的测试项目中,我想要调试的测试用例的类名是MyTestClass。 It has a [TestFixtureSetUp] method named Init() and the actual test case is in [Test] function MyTest() 它有一个名为Init()的[TestFixtureSetUp]方法,实际测试用例在[Test]函数MyTest()中

2) In my code project, I have a console program TestProgram.cs which compiles to an EXE. 2)在我的代码项目中,我有一个控制台程序TestProgram.cs,它编译成一个EXE。

In TestProgram.cs, I call the test cases in the following way 在TestProgram.cs中,我以下列方式调用测试用例

// 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. 如果有人在没有支付额外插件的情况下使用Visual Studio 2008 Express有任何更好的建议,我感谢您的建议。

Jon as a good way but not the more automatic one ;) 乔恩是一个好方法,但不是更自动的;)

Here is what I already say on SO : 这是我在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 . 您可以创建一个空白项目(例如选择控制台应用程序),在项目的属性中,您可以选择DEBUG标签并选择Start External Program Put the path of Nunit. 把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...). 在启动选项中,命令行参数选择包含所有测试的DLL(我的总是在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. 比选择Enable unmanaged code debugging ,您将能够在VS内启动项目,甚至可以逐步使用调试器。

This way, you do not need macro or anything to be done every time, it's just a normal F5 :) 这样,你不需要每次都做宏或任何事情,这只是一个正常的F5 :)

Since you're using an Express version of Visual Studio, you cannot use the free TestDriven.NET add-in. 由于您使用的是Visual Studio的Express版本,因此无法使用免费的TestDriven.NET加载项。 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. Runner类可以在nunit-console-runner.dll程序集中找到,因此请务必添加对它的引用。 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() . 值得一提的另一个(脏)选项是单元测试调用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. 然后,您可以选择现有的VS实例并使用它进行调试。

Have a look at http://nunit.com/blogs/?p=28 看看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. 在缺点方面,这意味着设置可能与NUnit完全不同。 On the upside, it is quite simple to do and allows one to create a minimal environment to reproduce the bug. 从好的方面来说,这很简单,并且允许人们创建一个最小的环境来重现bug。

I found some references online that suggested calling the following: NUnit.ConsoleRunner.Runner.Main(args); 我在网上发现了一些建议调用以下内容: 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. JFYI您需要在NUnit DLL包中添加一个.NET DLL引用到nunit-console-runner程序集(我有2.2.4.0)似乎该类已经被重命名,但是如果你需要它的话。

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. 如果安装了ReSharper,它应该能够检测到[Test]灯具并在每个方法的左侧放置一个图标来运行,并在每个测试夹具的左侧运行全部。 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. 大多数工作人员也有TestDriven.NET,由于某些原因,它不在我的机器上,所以他们想知道发生了什么。 It's kinda funny. 这有点好笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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