简体   繁体   English

Nunit 未在 Visual Studio 调试模式下运行 SetUp 方法

[英]Nunit not running SetUp method in Visual Studio debug mode

I'm trying to debug into the tests after the setup method is called and the tests depend on the setup method being called.我正在尝试在调用 setup 方法后调试测试,并且测试取决于被调用的 setup 方法。

I'm using Nunit 2.6.0.12051 testing a .Net 4.0 class library.我正在使用 Nunit 2.6.0.12051 测试 .Net 4.0 类库。 The nunit Visual Studio project has a class marked with [SetUpFixture] and a method marked with [SetUp]. nunit Visual Studio 项目有一个用 [SetUpFixture] 标记的类和一个用 [SetUp] 标记的方法。

If I run the tests from the NUnit gui, I'm fairly certain the setup attrib'd class is called (because it isn't stopped at the setup class with a run-time error now) but I can't debug into it.如果我从 NUnit gui 运行测试,我很确定调用了 setup attrib'd 类(因为它现在没有在设置类中停止并出现运行时错误)但我无法调试它. If I try to change the settings to see Verbose Tracing, NUnit gui throws an unhandled excption.如果我尝试更改设置以查看详细跟踪,NUnit gui 会抛出未处理的异常。

If I run the tests from Visual Studio via Test View/Debug Selection, the break point at the setup method doesn't stop execution and the trace statements inside the method don't print in the debug window.如果我通过测试视图/调试选择从 Visual Studio 运行测试,则设置方法的断点不会停止执行,并且方法内的跟踪语句不会在调试窗口中打印。 So I'm certain the setup method isn't getting called.所以我确定 setup 方法不会被调用。

While I could just change the setup class to be the base of all test classes, I only need the method run once.虽然我可以将设置类更改为所有测试类的基础,但我只需要运行一次该方法。

Any help would be wonderful.任何帮助都会很棒。

I just ran into this issue and eventually found this significant sentence from the NUnit SetUpFixture documentation :我刚刚遇到了这个问题,并最终从NUnit SetUpFixture 文档中找到了这个重要的句子:

"This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace." “这是标记一个类的属性,该类包含给定命名空间下所有测试装置的一次性设置或拆卸方法。”

Turned out my SetUpFixture class was in an entirely different namespace than my tests, so it wasn't being run.结果我的 SetUpFixture 类与我的测试在一个完全不同的命名空间中,所以它没有运行。

I just noticed the same when using the latest NUnit from NuGet (2.6).我只是在使用 NuGet (2.6) 的最新 NUnit 时注意到了同样的情况。 The [Setup] method is not run before the [Test] -methods. [Setup] 方法不在 [Test] 方法之前运行。

I don't know why they changed this quite significant part of NUnit, but I fixed it for my purposes by going back to version 2.5.10 which does run [Setup] before [Test].我不知道为什么他们改变了 NUnit 的这个相当重要的部分,但我为了我的目的修复了它,回到 2.5.10 版本,它在 [测试] 之前运行 [Setup]。

I had this issue too but installing the latest version of the test runner (TestDriven.NET in my case) fixed it.我也有这个问题,但安装最新版本的测试运行器(在我的例子中是 TestDriven.NET)修复了它。 It wasn't an NUnit issue for me.对我来说这不是 NUnit 问题。

I just ran into a similar issue.我刚刚遇到了类似的问题。 My unit test was not calling setup either.我的单元测试也没有调用 setup。 After reading the NUnit doc.阅读 NUnit 文档后。 referred in the top answer, I figured my solution was extremely simple.在最佳答案中提到,我认为我的解决方案非常简单。 In very simple terms, I was missing the "Setup".用非常简单的术语来说,我错过了“设置”。 Realised after reading the doc.阅读文档后意识到。

*Example from NUnit SetUpFixture doc mentioned in top answer. *来自最佳答案中提到的NUnit SetUpFixture 文档的示例。

[SetUp] 
RunBeforeAnyTests()
{
//
}

----------------------------------------------------

// this was my fixed up code
// simply added [Setup] and boom, bob is your uncle.

[SetUp] 
public void Setup()
{
    readChoice.Reset();
}

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

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