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

发布于 2024-07-07 22:00:26 字数 993 浏览 7 评论 0原文

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

我有一个解决方案,其中我的代码位于一个项目中,而我的 NUnit 单元测试位于同一解决方案的不同项目中。

我一直在努力调试和单步执行 NUnit 测试。 我在网上找到了一些参考资料,建议调用以下内容:

NUnit.ConsoleRunner.Runner.Main(args);

但这甚至无法编译 - 它有编译器错误:

错误 1 ​​类型或命名空间名称 “跑步者”不存在于 命名空间“NUnit.ConsoleRunner”(是 您缺少程序集引用吗?)

我已经添加了我能找到的所有程序集引用,但没有效果。

最后,这就是我一起破解的并且它有效,但也许你的好读者可以提出更好的解决方案:

1)在我的测试项目中,我想要调试的测试用例的类名是MyTestClass。 它有一个名为 Init() 的 [TestFixtureSetUp] 方法,实际测试用例在 [Test] 函数 MyTest() 中

2) 在我的代码项目中,我有一个控制台程序 TestProgram.cs,它编译为 EXE。

在 TestProgram.cs 中,我通过以下方式调用测试用例

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

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

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

这有效,我可以调试并单步执行测试用例。

如果有人在使用 Visual Studio 2008 Express 时有任何更好的建议,无需支付额外的插件,我非常感谢您的建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

無處可尋 2024-07-14 22:00:26

乔恩是一种好方法,但不是更自动的方法;)

这是我的已经说过了

您可以创建一个空白项目(例如选择控制台应用程序),在项目的属性中您可以选择 DEBUG 标签并选择启动外部程序。 放置Nunit的路径。 然后,在启动选项中,命令行参数选择包含所有测试的 DLL(我的始终位于 nunit\bin...)。 然后选择启用非托管代码调试,您将能够在VS中启动项目,甚至逐步使用调试器。

这样,您就不需要每次都执行宏或执行任何操作,这只是一个普通的 F5 :)

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

红焚 2024-07-14 22:00:26

看看 http://nunit.com/blogs/?p=28

这个工作是一种享受。

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

This works a treat.

姜生凉生 2024-07-14 22:00:26

由于您使用的是 Visual Studio Express 版本,因此无法使用免费的 TestDriven.NET 加载项。 这很不幸,因为这确实是我用来调试单元测试的最好工具。

Runner 类可以在 nunit-console-runner.dll 程序集中找到,因此请务必添加对此的引用。 我不确定它有什么作用是您的简单入口点所没有的,但为了安全和将来的兼容性,最好使用它。

值得一提的另一个(脏)选项是让您的单元测试调用 Debugger.Attach() 。 我还没有尝试过快速安装,但是当单元测试运行时,系统应该提示您附加调试器。 然后,您可以选择现有的 VS 实例并使用它进行调试。

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.

和影子一齐双人舞 2024-07-14 22:00:26

在我的店里,我们完全按照您的方式做。 不利的一面是,这意味着设置可能与 NUnit 的设置不完全一样。 从好的方面来说,它非常简单,并且允许创建一个最小的环境来重现错误。

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.

浪菊怪哟 2024-07-14 22:00:26

我在网上找到了一些参考资料
建议拨打以下电话:
NUnit.ConsoleRunner.Runner.Main(args);
但这甚至无法编译 - 它已经
编译器错误:

JFYI
您需要在 NUnit DLL 捆绑包中添加对 nunit-console-runner 程序集的 .NET DLL 引用(我有 2.2.4.0 )
似乎该类也已重命名,但如果您需要它,它就在那里。

NUnit.ConsoleRunner.ConsoleUi.Main(args);

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);
清君侧 2024-07-14 22:00:26

如果您安装了 ReSharper,它应该能够检测您的 [测试] 装置,并在每个要运行的方法的左侧放置一个图标,并在每个测试装置的左侧放置一个图标以运行所有方法。 我更喜欢这种方式。

大多数工作人员也有 TestDriven.NET,但由于某种原因它不在我的机器上,所以他们想知道发生了什么。 有点好笑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文