Unit Testing: Windows Forms using MS Visual Studio



  • I've created unit tests of my WinForms using MS Visual Studio. None of my tests have been approved. All of them (0/42 tests) resulted into some kind of failure described as follows:- System.NullReferenceException: Object reference not set to an instance of an object. Assert.Inconclusive failed. A method that does not return a value cannot be verified. Without running these tests, the normal output produced didn't seem to throw any errors. I'm completely new to testing so I have no idea how to handle these errors. /// <summary> ///A test for txtTel_Leave ///</summary> [TestMethod()] [DeploymentItem("Payroll master.exe")] public void txtTel_LeaveTest() { emp_Accessor target = new emp_Accessor(); // TODO: Initialize to an appropriate value object sender = null; // TODO: Initialize to an appropriate value EventArgs e = null; // TODO: Initialize to an appropriate value target.txtTel_Leave(sender, e); Assert.Inconclusive("A method that does not return a value cannot be verified."); }



  • To set up your unit tests they need to initialize all of the variables. They should end up looking something like this: Code To Be Tested: public class Class1 { public int CombineValues(int a, int b) { int c = 12; if (a != 0 && b > 10) { c += (a + b); } else { c += 1; } return c; } } Unit Tests Your values should be initialed and the test needs to assert something. [TestClass] public class UnitTest1 { [TestMethod] public void SimpleAdd() { int a = 1; int b = 12; Class1 cl1 = new Class1(); int expected = 25; int actual = cl1.CombineValues(a, b); Assert.AreEqual(expected, actual); } [TestMethod] public void SimpleAdd_a_is_zero_test() { int a = 0; int b = 12; Class1 cl1 = new Class1(); int expected = 13; int actual = cl1.CombineValues(a, b); Assert.AreEqual(expected, actual); } } For a far more comprehensive look at unit testing check out Roy Osherove's The Art of Unit Testing.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2