Monday, March 14, 2011

Testing MVC 3 Applications with xUnit.net


Microsoft Visual Web Developer Express edition doesn’t support MS Test. But we can create test with the help of xUnit.net testing framework mainly for MVC Web Applications.

  1. Start Visual Web Developer 2010 Express and create a MVC 3.0 Web Application

    image

  2. Select Internet Application project template and check the Create a unit test project. By default the test project name will be <Project Name>.Tests and the test framework will be xUnit.net.

    image

    The project structure will be as shown below

    image

  3. Right click on Controllers folder and select  Controller from Add menu.

    image

  4. Set some text to the Message property of ViewBag as shown in line number 12

       1: using System.Web.Mvc;
       2:  
       3: namespace SampleMVCWithxUnit.Controllers
       4: {
       5:     public class EmployeeController : Controller
       6:     {
       7:         //
       8:         // GET: /Employee/
       9:  
      10:         public ActionResult Index()
      11:         {
      12:             ViewBag.Message = "My xUnit.net test";
      13:             return View();
      14:         }
      15:  
      16:         //
      17:         // GET: /Employee/Details/5
      18:  
      19:         public ActionResult Details(int id)
      20:         {
      21:             return View();
      22:         }
      23:  
      24:         //
      25:         // GET: /Employee/Create
      26:  
      27:         public ActionResult Create()
      28:         {
      29:             return View();
      30:         } 
      31:  
      32:         //
      33:         // POST: /Employee/Create
      34:  
      35:         [HttpPost]
      36:         public ActionResult Create(FormCollection collection)
      37:         {
      38:             try
      39:             {
      40:                 // TODO: Add insert logic here
      41:  
      42:                 return RedirectToAction("Index");
      43:             }
      44:             catch
      45:             {
      46:                 return View();
      47:             }
      48:         }
      49:         
      50:         //
      51:         // GET: /Employee/Edit/5
      52:  
      53:         public ActionResult Edit(int id)
      54:         {
      55:             return View();
      56:         }
      57:  
      58:         //
      59:         // POST: /Employee/Edit/5
      60:  
      61:         [HttpPost]
      62:         public ActionResult Edit(int id, FormCollection collection)
      63:         {
      64:             try
      65:             {
      66:                 // TODO: Add update logic here
      67:  
      68:                 return RedirectToAction("Index");
      69:             }
      70:             catch
      71:             {
      72:                 return View();
      73:             }
      74:         }
      75:  
      76:         //
      77:         // GET: /Employee/Delete/5
      78:  
      79:         public ActionResult Delete(int id)
      80:         {
      81:             return View();
      82:         }
      83:  
      84:         //
      85:         // POST: /Employee/Delete/5
      86:  
      87:         [HttpPost]
      88:         public ActionResult Delete(int id, FormCollection collection)
      89:         {
      90:             try
      91:             {
      92:                 // TODO: Add delete logic here
      93:  
      94:                 return RedirectToAction("Index");
      95:             }
      96:             catch
      97:             {
      98:                 return View();
      99:             }
     100:         }
     101:     }
     102: }



  5. Create a class in the Test project and name it EmployeeControllerFacts.cs which will hold the test cases for Employee Controller.

    image


  6. Build the test project and open the xUnit test runner from the installation folder. Add the test project dll with the help of Open menu from Assembly toolbar.

    image


  7. Click the Run Selected button to execute the test methods

    image


    Thanks,
    Bimal

Thursday, March 10, 2011

Unit Testing with XUnit.net 1.7


Introduction

xUnit.net is a testing framework which is simple to use for .net developers. It comes with a testing framework library and Test runners for .net 2.0 and 4.0.

Implementation

  1. Download the xUnit.net framework from here and install it.

    image

  2. Add a Visual Studio Class Library Project and name it as Calculate.Test

    image

  3. Add reference to the Project that need to test.
  4. Add reference to xUnit dll. The solution structure will looks like this.  

    image

  5. Open CalculateTest.cs file write a test method for Add method in the Calculate Project.

    Let’s try for a fail case

    image

  6. Open xUnit.net Test Runner application from installation folder

    image

  7. Select Open from Assembly menu and add the test dll i.e Calculate.Test.dll which will be displayed in xUnit.net Test Runner as shown below

    image

  8. Click the Run All button to execute the test methods and the results will be displayed as shown below. 

    image 

  9. Modify the test case as shown below to fix the test case

    image

  10. Run the test will give the green signal.

    image

Conclusion

xUnit.net is a easy to use framework for .net developers.

Thanks,
Bimal

Tuesday, March 08, 2011

Unit Testing with NUnit 2.5.9


Introduction

NUnit is a xunit based unit-testing framework written in C# for Microsoft.Net. It comes with a testing framework assembly and a test runner for displaying the test results.

Implementation

  1. Download NUnit framework from here and install it.

    image

  2. Add a Visual Studio Class Library Project and name it as Calculate.Test.

    image

  3. Add reference to the Project that need to test.
  4. Add reference to NUnit framework

    image

    The Solution structure will looks like this

    image

  5. Open CalculateTest.cs file and decorate the public class with TestFixture attribute. The TextFixture attribute compiled into an assembly when we build the project.

    Let’s try for a fail case

    image

  6. Run NUnit runner application from Windows Program Menu

    image

  7. Select Open Project from File menu and load the Calculate.Test.dll. The test runner will load the assembly and using reflection API’s looks for any type that has a TestFixture attribute on it. Once the test runner identifies the TestFixture class, it will create an instance of that class and executes all the methods decorated with Test attribute.

    Click the Run button to see the result.

    image

  8. Modify the test case as shown below to fix the test case.

    image

  9. Run the test will give the green signal.

    image

Thanks,
Bimal

.Net Unit Testing Frameworks