Reputation: 2432
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?
Upvotes: 4
Views: 4172
Reputation: 10346
I would only change your source code to use ShowDialog() if you want your forms to actually be opened as Modal dialogs.
You are correct in that NUnitForms supports "Expect Modal", but does not have an "Expect Non-Modal". You can implement this yourself with relative ease using the FormTester class. FormTester is an extension class available from the latest repository for NUnitForms. You pass in the string of the .Name property of the form you are checking to see if it is shown.
public static bool IsFormVisible(string formName)
{
var tester = new FormTester(formName);
var form = (Form) tester.TheObject;
return form.Visible;
}
Upvotes: 0
Reputation: 2432
I figured it out myself. There are two ways Windows Forms can be 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.
Upvotes: 2