NUnit-Console 找不到使用 NUnit 3.x 进行 NUnitForms 测试的测试夹具

NUnit-Console finds no test fixtures for an NUnitForms test with NUnit 3.x

我一直在摆弄 NUnitForms,并且 运行 我的简单测试遇到了麻烦。我将在下面提供更多信息,但为了简要总结问题,我处于需要继承 NUnitFormTest class 才能使用 ExpectModal 功能的状态,但这会导致找不到测试(使用 NUnit 3.6.1)。如果我降级到 NUnit 版本 2.x 会找到测试,但我会在 TearDown 函数上遇到错误。我在这里遗漏了什么吗?

完整信息见下文...

我的测试最初是引用 NUnit 3.6.1 并且如下所示:

using EnterpriseManager;
using NUnit.Extensions.Forms;
using NUnit.Framework;

namespace ManagerTests
{
    [TestFixture]
    public class ManagerTests
    {
        [Test]
        public void ConnectTest()
        {
            ConnectForm form = new ConnectForm();
            form.Show();

            ButtonTester testButton = new ButtonTester("TestConnectionButton", "ConnectForm");
            testButton.Click();

            LabelTester testLabel = new LabelTester("StatusLabel", "ConnectForm");
            Assert.AreEqual("Connection successful", testLabel.Text);
        }
    }
}

在我上面的初始测试中,我没有从 NUnitFormTest class 继承(当时并没有意识到)但是即使它丢失了,我的测试也会被找到Visual Studio 的测试资源管理器,我可以通过 nunit3-console 应用程序(已通过)运行 我的测试。

最终我扩展了我的测试以调用一个模式对话框,这给我带来了问题,尽管最终我读到了 ExpectModal 功能,这导致我添加了 NUnitFormTest 继承。测试变成如下:

using EnterpriseManager;
using NUnit.Extensions.Forms;
using NUnit.Framework;

namespace ManagerTests
{
    [TestFixture]
    public class ManagerTests : NUnitFormTest
    {
        [Test]
        public void ConnectTest()
        {
            ConnectForm form = new ConnectForm();
            form.Show();

            ButtonTester testButton = new ButtonTester("TestConnectionButton", "ConnectForm");
            testButton.Click();

            LabelTester testLabel = new LabelTester("StatusLabel", "ConnectForm");
            Assert.AreEqual("Connection successful", testLabel.Text);

            ExpectModal("ConnectToServer", delegate { 
                LabelTester label = new LabelTester("ConnectStatusLabel", "ConnectToServer");
                Assert.AreEqual("Connected", label.Text);
            });

            // Launch the modal dialog
            ButtonTester connectButton = new ButtonTester("ConnectToServerButton", "ConnectForm");
            connectButton.Click();
        }
    }
}

这就是问题开始的地方。添加 NUnitFormTest class 的继承后,Visual Studio 和 nunit3-console.exe 均未检测到任何测试。我认为这可能与引用的 NUnit 版本有关,所以我降级到各种 2.x 版本。这允许 Visual Studio 检测测试(尽管 nunit3-console.exe 一直报告 "Inconclusive" 结果)但是所有测试都会失败并显示错误:

Result StackTrace:  
--TearDown
at NUnit.Extensions.Forms.Desktop.Destroy()
at NUnit.Extensions.Forms.NUnitFormTest.Verify()
Result Message: TearDown : System.ComponentModel.Win32Exception : The requested resource is in use

我已经围绕这个问题进行了一些搜索,但我发现的一切似乎都表明这是以前遇到的 NUnit 的一些问题,该问题已在某个时候得到修复(不要引用我的话)。所以现在我处于需要继承 NUnitFormTest class 才能使用 ExpectModal 功能的状态,但这会导致无法找到测试。但是,如果我向下移动到 NUnit 版本 2.x,我会在 TearDown 函数上遇到问题。我在这里缺少什么吗?

NUnitForms 多年未更新,因此它仍然依赖于 NUnit V2。当您从 NUnitFormTest 派生时,您使用的是 NUnit 2.6.2,无论您认为自己安装了什么,因为代码与 NUnit 版本紧密耦合。

NUnitForms 可以很容易地更新到 NUNit 2.6.4,但除此之外,它是一个更大的变化,甚至可能需要重写。

顺便说一句,Luke 很久以前就把我加入了这个项目,但我一直没有活跃。我曾经希望让它的一个版本与 NUnit 3 一起工作,但我怀疑是否还有很多测试 Windows Forms 的需求。

您应该从您的解决方案中删除 NUnit 框架的任何包,并参考与 NUnitForms 一起安装的版本。如果您想尝试不同的版本,它们应该是 NUnitForms 的版本,以便您的测试和 NUnitForms 都引用相同的 NUnit 副本。