Automatic Unit Test Generator for Microsoft Visual Studio 2015

In Microsoft Visual Studio 2015, we have a new feature called “Create Unit Tests”. By using this feature, we can generate unit tests for our methods automatically. Here we discuss how to generate Unit Tests for C# methods in Visual Studio 2015.

Open Microsoft Visual Studio 2015 => Open Create New Project Window => Create Console Application and name it as Company. Add new Class Employee to the project with GetEmployeeDetails() as shown below. To add a unit test case, C# class and method should be public.

using System.Data; 

namespace Company

{

    public class Employee

    {

        public DataTable GetEmployeeDetails(int iEmpId)

        {

            DataTable dtEmployee = new DataTable(); 

            //Have the logic to fetch Employee details, here I do not include it 

            return dtEmployee;

        }

    }

} 

To generate Unit Test for GetEmployeeDetails() method, right click on the method and select “Create Unit Tests” option as shown below.

It displays the “Create Unit Tests” window as shown below.

Provide the details for test project name & test class name and click OK, then it adds new Unit Test project as shown below. I have provided Test project name as “Company. Tests” in the above window.

using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace Company.Tests

{

    [TestClass()]

    public class EmployeeTests

    {

        [TestMethod()]

        public void GetEmployeeDetailsTest()

        {

            Assert.Fail();

        }

    }

 }

By default, generate test method returns Assert.Fail(). If you want, we can have “Empty body” or “Throw NotImplementedException” in the “Create Unit Tests” window.

In my future article, we discuss how can achieve this same feature in Microsoft Visual Studio 2013 & Microsoft Visual Studio 2012.