How to check button click opens a new form using NUnitForms?

I am new to C # and trying to write a simple Unit Test GUI with NUnit and NUnitForms. I want to check if clicking 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 of my test is as follows:

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

NUnit output:

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

Checking button text successfully. The problem is the ExpectModal method. I also tried it with the name of the form, but without success.

Does anyone know what could be wrong?

+4
source share
2 answers

I figured it out myself. There are two ways to display Windows forms:

  • Modal : "The modal form or dialog should be closed or hidden before you can continue working with the rest of the application"
  • Modeless : "... allows you to move 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."

Windows Modeling is opened using the Show () method and Modal using ShowDialog (). NUnitForms can only track modal dialogs (this also explains why the method is called "ExpectModal").

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

+2
source

I would only modify your source code to use ShowDialog () if you want your forms to actually be opened as modal dialogs.

You are right that NUnitForms supports Expect Modality, but does not Expect 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 the .Name property string of the form you are checking to see if it is displayed.

  public static bool IsFormVisible(string formName) { var tester = new FormTester(formName); var form = (Form) tester.TheObject; return form.Visible; } 
0
source

Source: https://habr.com/ru/post/1286424/


All Articles