Unit Testing Windows Forms using White and nUnit
Few days back I attended a session on TDD with ASP.NET MVC 1.0 on K-MUG Cochin User group meeting, from Shiju Varghese, the session was excellent one. Currently I was working on a Windows application, so after the session I searched for tool, using that I can Unit test my windows application. After few searches, I got one link from Codeplex, called White. White is open source project from Thoughtworks, you can download white from Codeplex. To unit test the Windows applications; you need both White as well as NUnit. Also I got another tool called NUnitForms, but I think this project is almost dead
I tried it with VS 2008, in a simple VC# application.
TDD often called Red-Green approach, which means you will initially write code for getting the Red signal. After getting the red signal, you will write some code to get the green signal. After getting the Green signal, your need to refactor the code.
HelloWorld Application (TDD Approach)
Requirements
Hello world application: Need a UI, which contains a Button with Text “HelloWorld”, clicking on that button, it will pop up a MessageBox with title “Helloworld”.
Unit TestCases.
- Check the UI exists or not
- Check the UI contains a Button
- Click on the Button, will it showing a MessageBox with title “Helloworld”?
Development
- Created a Windows Application Project, and added a Class library project to the solution.
- Added References of White.NUnit.dll, White.Core.dll and nunit.framework.dll to the class library.
- Wrote the code for setup and Teardown in the class library
[TestFixture]
public class HelloworldTest1
{
private string applicationPath = @"C:\Helloworld\HelloWorld.exe"; //Path of the Windows app to test
Application helloworldApp;
[SetUp]
public void SetupTest()
{
//Launching the application.
this.helloworldApp = Application.Launch(applicationPath);
}
[TearDown]
public void ShutdownTest()
{
this.helloworldApp.Kill();//Stopping the application.
}
}
Now we can add Test cases.
Test Cases
- Checking the UI existance
- Check the UI contains a Button or not
- Click on the Button, will it showing a MessageBox with title “Helloworld”
[Test]
public void CheckUI()
{
Window helloworldWin = this.helloworldApp.GetWindow("UI");
Assert.IsNotNull(helloworldWin);
}
This testcase will fail, because I don’t have a Form with the title UI, so nUnit will fail, with the reason “Couldn’t find window with title UI in process 980, after waiting for 5000 ms”. Now you got the green signal. Now change the Title of the form to UI, build the solution, run the NUnit test case. It will return a Green signal. You passed the first test case.
[Test]
public void CheckButtonExists()
{
Window helloworldWin = this.helloworldApp.GetWindow("UI");
Button button = helloworldWin.Get<button>("button1");
Assert.IsNotNull(button);
}
This will also fail because we haven’t added a Button on the Form. Lets add a button with Text “Helloworld” and compile and re-run the application. It will give you a green signal.
[Test]
public void CheckMessageBoxDisplaying()
{
Window helloworldWin = this.helloworldApp.GetWindow("UI");
Button button = helloworldWin.Get</button><button>("button1");
button.Click();
Window messageBox = helloworldWin.MessageBox("Helloworld");
Assert.IsNotNull(button);
messageBox.Close();
}
It will fail, because there is nothing in the click event, so NUnit will display an Object reference error, like this “HelloWorldTest.HelloworldTest1.CheckMessageBoxDisplaying: System.NullReferenceException : Object reference not set to an instance of an object.”
Lets add some code in the click event
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("this is click event","hello");
}
It will again fail, because we are looking for a MessageBox window with Title HelloWorld. But in the actual code, we wrote Title as “hello”. Now change the Title to “Helloworld” and re-run the application. It will run successfully.
Hope you got some idea about unit testing windows applications using White. Let me know your comments and feedback if you are facing any issues. You can also test WPF applications with White, but I didn’t tried it.
You can download
- NUnit from NUnit website
- White from Codeplex