by Paul Kohler
13. June 2007 23:30
Just a quick note that could drive you completely mad if you were not aware...
If you are using NUnitForms for testing your GUI and have code in the forms "Shown" event, it will not run unless you follow the Form.Show call with Application.DoEvents(), see sample code below
This example is just a Label (label1) dumped on a Form with the Load and Shown events updating the label with the respective event text:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NUnitFormsDemo1
{
public partial class ShownTestForm : Form
{
public ShownTestForm()
{
InitializeComponent();
}
private void ShownTestForm_Load(object sender, EventArgs e)
{
label1.Text = "Load";
}
private void ShownTestForm_Shown(object sender, EventArgs e)
{
label1.Text = "Shown";
}
}
}
Here is some sample NUnitForms test code, the first test asserts that after the
Form.Show call the label text is "Load" and the second test shows that the label text is "Shown".
using System;
using System.Windows.Forms;
using NUnit.Framework;
using NUnit.Extensions.Forms;
namespace NUnitFormsDemo1.UnitTests
{
[TestFixture]
public class TestFormShownIssue : NUnitFormTest
{
[Test]
public void TestFormShow()
{
ShownTestForm frm = new ShownTestForm();
LabelTester label1Tester = new LabelTester("label1");
frm.Show();
Assert.AreEqual("Load", label1Tester.Text);
}
[Test]
public void TestFormShowWithDoEvents()
{
ShownTestForm frm = new ShownTestForm();
LabelTester label1Tester = new LabelTester("label1");
frm.Show();
Application.DoEvents(); // allows the 'Shown' event to fire
Assert.AreEqual("Shown", label1Tester.Text);
}
}
}
I will take an educated guess that this is due to the GUI message pump etc.
In general if I come across this any of this type of unexpected behavior with NUnitForms I will try a DoEvents before tearing my hair out.