简体   繁体   中英

GUI interface testing using NUnit.Forms

I'm trying to write some tests for testing GUI interface. I decided to choose NUnit.Forms. But the tests fall with the following error:

TearDown : System.ComponentModel.Win32Exception : The requested resource is in use

I have two versions of the source code tests.

First:

using System.Windows.Forms;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using YAMP;

namespace Tests.GUITests
{
    [TestFixture]
    public class GuiTest : NUnitFormTest
    {
        private FrmMain _frm;

        //[SetUp] // or it is still needed
        public override void Setup()
        {
            base.Setup();
            _frm = new FrmMain();
            _frm.Show();
        }

        [Test]
        public void TestData()
        {
            var txtInput = new TextBoxTester("txtInput") {["Text"] = "2+2"};
            var txtOutput = new TextBoxTester("txtOutput");
            Assert.AreEqual("2+2", txtInput.Text);

            var btnRes = new ButtonTester("btnRes");
            btnRes.Click();
            Assert.AreEqual("4", txtOutput.Text);
        }
    }
}

Second:

using System.Windows.Forms;
using NUnit.Extensions.Forms;
using NUnit.Framework;
using YAMP;

namespace Tests.GUITests
{
    [TestFixture]
    public class GuiTest : NUnitFormTest
    {
        private FrmMain _frm;

        //[SetUp] // or it is still needed
        public override void Setup()
        {
            base.Setup();
            _frm = new FrmMain();
            _frm.Show();
        }

        [TearDown]
        public override void TearDown()
        {
            _frm.Close();
            _frm.Dispose();
        }

        [Test]
        public void TestData()
        {
            var txtInput = new TextBoxTester("txtInput") {["Text"] = "2+2"};
            var txtOutput = new TextBoxTester("txtOutput");
            Assert.AreEqual("2+2", txtInput.Text);

            var btnRes = new ButtonTester("btnRes");
            btnRes.Click();
            Assert.AreEqual("4", txtOutput.Text);
        }
    }
}

And there are two different versions of the method TestNoData:

    public void TestFormNoDataHandler()
    {
        var messageBoxTester = new MessageBoxTester("Message");
        messageBoxTester.ClickOk();
    }

    [Test]
    public void TestNoData()
    {
        ExpectModal("Message", TestFormNoDataHandler);
        var txtInput = new TextBoxTester("txtInput") {["Text"] = string.Empty};
        Assert.AreEqual(string.Empty, txtInput.Text);

        var btnRes = new ButtonTester("btnRes");
        btnRes.Click();
        Assert.IsFalse(_frm.DialogResult == DialogResult.OK);
    }

    [Test]
    public void TestNoData()
    {
        var txtInput = new TextBoxTester("txtInput") {["Text"] = string.Empty };
        Assert.AreEqual(string.Empty, txtInput.Text);

        var btnRes = new ButtonTester("btnRes");
        btnRes.Click();
        Assert.IsFalse(_frm.Enable);
    }

Testable form is very simple. There are two TextBox - "txtInput", "txtOutput" and button - "btnRes". In "txtInput" introduced a mathematical expression, and "txtOutput" output response. The decision of expression occurs when you press "btnRes". If the field "txtInput" empty, the button is disabled and you can not click on it.

When searching for solutions to this problem came on the following links:

Unfortunately I can attach only 2 links. But the information I learned is very different. Especially the part of writing methods Setup and TearDown.

In any case, I specify the version I use:

  • Visual Studio 2015 Community
  • NUnit - 2.6.4.14350
  • NUnitForms - 1.3.1771.29165

Because it seems to me that the problem might be too recent versions of frameworks, as article I learned quite old.

Thank you for any suggestion.

UseHidden Property: Tests are run on a separate hidden desktop. This makes them much faster and it works for any tests that are not using the keyboard or mouse controllers. They are less disruptive and input tests cannot interfere with other applications.

UseHidden property controls whether a separate desktop is used at all.

Though tests on the separate desktop are faster and safer (There is no danger of keyboard or mouse input going to separate running applications.), however for some operating systems or environments the separate desktop does not work. And the tests throw up errors like:

System.ComponentModel.Win32Exception : The requested resource is in use

--TearDown
   at NUnit.Extensions.Forms.Desktop.Destroy()
   at NUnit.Extensions.Forms.Desktop.Dispose()
   at NUnit.Extensions.Forms.NUnitFormTest.Verify()

In that case you can override UseHidden property from test class and set it to return false. This will cause the tests to run on original, standard desktop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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