Search code examples
c#nunitnunit-console

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


I've been messing around with NUnitForms and I've run into a snag with my simple test. I'll provide more information below but to summarize the question briefly, I'm in a state that I need to inherit from the NUnitFormTest class to use the ExpectModal functionality but this causes by tests to not be found (using NUnit 3.6.1). If I downgrade to NUnit version 2.x the tests are found but I get errors on the TearDown function. Is there something I'm missing here?

Now for full information see below...

My test originally was referencing the NUnit 3.6.1 and was like the following:

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);
        }
    }
}

In my initial test above, I was not inheriting from the NUnitFormTest class (was not aware of it at the time) but even though that was missing, my test would be found by Visual Studio's Test Explorer and I could run my test via nunit3-console application (which passed).

Eventually I expanded my test to invoke a modal dialog which caused problems for me although eventually I read about the ExpectModal functionality which lead me to adding the NUnitFormTest inheritance. The test became the following:

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();
        }
    }
}

This is where the problems began. After adding the inheritance of the NUnitFormTest class, neither Visual Studio nor nunit3-console.exe detected any tests. I thought this was maybe related to the version of the referenced NUnit so I downgraded to various 2.x versions. This allowed Visual Studio to detect the tests (although nunit3-console.exe kept reporting an "Inconclusive" result) but all the tests would fail with the error:

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

I've down some searching around this issue but everything I found seemed to suggest this was some previously encountered issue with NUnit that was fixed at some point (don't quote me). So now I'm in a state that I need to inherit from the NUnitFormTest class to use the ExpectModal functionality but this causes by tests to not be found. Yet if I move down to NUnit version 2.x I get problems on the TearDown function. Is there something I'm missing here?


Solution

  • NUnitForms has not been updated for many years, so it remains dependent on NUnit V2. When you derive from NUnitFormTest, you are using NUnit 2.6.2, no matter what you think you have installed, because the code is tightly coupled to a version of NUnit.

    NUnitForms could easily be updated to NUNit 2.6.4, but beyond that it's a bigger change that might even call for a rewrite.

    BTW, Luke added me to the project a long while back, but I haven't been active. I once had hopes of making a version of it work with NUnit 3, but I'm doubtful that there is any longer a lot of demand for testing Windows Forms.

    You should remove any packages for the NUnit framework from your solution and reference the version that is installed with NUnitForms. If you want to experiment with different versions, they should be versions of NUnitForms so that both your tests and NUnitForms are referencing the same copy of NUnit.