NUnit Mock with WinForms Code Review
Define the Form Test Fixture Class
Code Review
- NUnit Mock with WinForms Code Review: Define the DataAccess Interface and Handler Object
- NUnit Mock with WinForms Code Review: Define the Basket and BasketItem Objects
- NUnit Mock with WinForms Code Review: Define the Object Test Fixture Class
- NUnit Mock with WinForms Code Review: Define the Form Test Fixture Class
Code Walkthrough
* Based on NUnit version 2.4.8
Define another Test class to unit test the Windows Form application class.
[TestFixture]
public class TestFormShopping : NUnitFormTest
{
Form1 FormShopping;
public override void Setup() { FormShopping = new Form1(); FormShopping.Show(); }
[Test] public void TestFormAddToBasketButton() { TextBoxTester textbox = new TextBoxTester("txtSubTotal"); ButtonTester button = new ButtonTester("btnAddBasket"); ListBoxTester listbox = new ListBoxTester("lstProducts"); ControlTester numQty = new ControlTester("numQty");
Console.WriteLine("Add Product 1 to Basket"); listbox.SetSelected(0, true); button.Click();
Console.WriteLine("Add Product 5 to Basket"); listbox.SetSelected(1, true); numQty["Value"] = Convert.ToDecimal(3); button.Click();
Assert.AreEqual(textbox.Text, "240"); } }
public override void Setup() { FormShopping = new Form1(); FormShopping.Show(); }
[Test] public void TestFormAddToBasketButton() { TextBoxTester textbox = new TextBoxTester("txtSubTotal"); ButtonTester button = new ButtonTester("btnAddBasket"); ListBoxTester listbox = new ListBoxTester("lstProducts"); ControlTester numQty = new ControlTester("numQty");
Console.WriteLine("Add Product 1 to Basket"); listbox.SetSelected(0, true); button.Click();
Console.WriteLine("Add Product 5 to Basket"); listbox.SetSelected(1, true); numQty["Value"] = Convert.ToDecimal(3); button.Click();
Assert.AreEqual(textbox.Text, "240"); } }
This class inherits from the NUnitFormTest class and defines a test method to automatically test adding items to a basket.