views:

387

answers:

1

Hi,

I'm new to C# and trying to write a simple GUI Unit Test with NUnit and NUnitForms. I want to test if a click on a button opens a new Form. My application has two forms, the main form (Form1) with a button that opens a new form (Password):

private void button2_Click(object sender, EventArgs e)
{
    Password pw = new Password();
    pw.Show();
}

The source for my Test looks like this:

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

namespace Foobar
{
    [TestFixture]
    public class Form1Test : NUnitFormTest
    {
        [Test]
        public void TestNewForm()
        {
            Form1 f1 = new Form1();
            f1.Show();

            ExpectModal("Enter Password", "CloseDialog");

            // Check if Button has the right Text
            ButtonTester bt = new ButtonTester("button2", f1);
            Assert.AreEqual("Load Game", bt.Text);

            bt.Click();
        }
        public void CloseDialog()
        {
            ButtonTester btnClose = new ButtonTester("button2");
            btnClose.Click();
        }
    }
}

The NUnit output is:

  NUnit.Extensions.Forms.FormsTestAssertionException : expected 1 invocations of modal, but was invoked 0 times (Form Caption = Enter Password)

The Button Text check is successfull. The problem is the ExpectModal method. I've also tried it with the Form's name but without success.

Does anyone know what might be wrong?

+1  A: 

I figured it out myself. There are two ways Windows Forms can be displayed:

  • Modal: "A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application"
  • Modeless: "... let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed."

Modeless Windows are opened with the Show() method and Modal ones with ShowDialog(). NUnitForms can only track Modal Dialogs (that's also why the method is named 'ExpectModal').

I changed every "Show()" to "ShowDialog()" in my source code and NUnitForms worked fine.

ak