As a developer who has always worked for small startup companies, I have never had money in my budget for expensive applications to test my projects. Up until the introduction of the Microsoft .Net Framework, I really couldn't find anything that would work for me. At that point most of my programming was done in C++, VB6 and ASP. A good testing suite really would have been beneficial to me.
Today, nearly 10 years later, I still work for a small start-up company with a very low budget. The one thing that has change is that today there are very useful open source tools to make just about any kind of automated testing possible. In this article I will focus on only unit testing and functionality testing of windows forms applications written in a .Net language.
nUnit
For unit testing today, I now use nUnit. The concept of unit testing is really pretty simple. You want to write tests that test each unit of your project independently from everything else. Most articles recommend writing all of your unit tests up front before you start coding, but if you work at small company like the one I work for, chances are you don't have time for that. I write my tests either as I go and only for functionality that is not easily tested otherwise, or could easily be broken. Sure, it would be better to write every single test, and do it early in the development, but having the resources and time to do that isn't a luxury afforded to everyone.Here is a really simple example that shows how nUnit could be used to test a really simple class .
Multiplier.cs
using System;
namespace TestingExample
{
public class Multiplier
{
public int Multiply(int x, int y)
{
return x* y;
}
}
}
MultiplierTest.cs
using System
using nUnit.Framework; using TestingExample;
namespace TestingExample.Tests
{
[TestFixture]
public class MultiplierTests
{
[Test]
public void TestMultiply()
{
Multiplier mult = new Multiplier();
Assert.AreEqual(4, mult.Multiply(2,2);
Assert.AreEqual(8, mult.Multiply(1,8);
}
}}
As you can see in the code above, nUnit tests are fairly simple, yet at the same time can be very powerful. The “TestFixture” and “Test” allow nUnit to identify the tests within your code. The Assert class contains numerous assertions that can be made depending on the situation you're testing. The best place to go four more information is the examples and documentation available for nUnit.
Now you have a way to test the base functionality of your application in units, but still no way to test your application as a whole in an automated fashion. Without a tool capable of working with the UI elements in an automated fashion, you are stuck manually testing your UI any time you make a change which can be very tedious and time consuming. For this work, you will find nUnitForms useful.
nUnitForms
nUnitForms provides an architecture, and tester classes that allow you manipulate your .Net Forms UI from an nUnit test. Writing a test using to use nUnitForms involves extending the nUnit example a little bit.Add a form caled frmMultiply to the project you created your nunitTest in. On this form add 3 textboxes and labels if you wish. Name them txtVal1, txtVal2, and txtResult. Add a button, set its caption to "Calculate" and name it btnCalculate. Double click the button in the designer to create the click event handler, and then add the following code in the event handler:
...
Multiplier mult = new Multiplier();
txtResult.Text = mult.Multiply(Convert.ToInt32(txtVal1.Text), Convert.ToInt32(txtVal2.Text));
...
Now you will create a new nUnit test fixture to hold the class.
frmMultiplyTests.cs
using System; using System.Windows.Forms;using TestingExample;
using nUnit.Framework; using nUnit.Extensions.Forms;
namespace TestingExample.Tests
{
[TestFixture]
public class frmMultiplyTests
{frmMultiply testForm;[SetUp]public void SetUp(){testForm = new frmMultiply();testForm.Show();}[TearDown]public void TearDown(){testForm.Close();}[Test]public void TestCalculate{ButtonTester btnCalculate = new ButtonTester("btnCalculate");TextBoxTester txtResult = new TextBoxTester("txtResult");TextBoxTester txtVal1 = new TextBoxTester("txtVal1");TextBoxTester txtVal2 = new TextBoxTester("txtVal2");txtVal1.Enter("2");txtVal2.Enter("4");btnCalculate.Click();Assert.AreEqual("8", txtResult.Text, "2 * 4 should be 8");}}}
Now when you compile and load your assemble or executable into nUnit, you should see your "TestCalculate" test. Running it should display the form, fill in the fields, and click the calculate button very quickly. You probably won't be able to see any of this happening though. If you wish to have everything display properly you may need to add in "Application.DoEvents();" at the points where you want to wait for the other events to complete.
At this point you should be able to use nUnit and nUnitForms to build automated tests for your applications. Testing Asp.Net web sites and console applications are not within the scope of this article, but if enough interest is show, I may write articles on those later.
Related Links
- nUnit - http://www.nunit.org/
- nUnitForms - http://nunitforms.sourceforge.net/
1 comment:
Hey the code was very useful. Thanks a lot :)
Post a Comment