Visual Studio Team Services : Adding and Running Unit Tests on Every Build

In the previous post in Visual Studio Team Services series, I have explained the steps triggering a build in the Team Services portal whenever we check in the code and in this one, I am going to show you the steps needed for running unit tests when a build is done in the portal.

Setting up Unit Tests in your solution

Let's modify the program I used in the earlier post as shown below.

 public class Program
    {
        static void Main(string[] args)
        {
            int i = 1, j = 2;
            int c = Add(i,j);
            Console.WriteLine($"Sum of {i} and {j} is {c}");
            Console.ReadKey();
        }

        public static int Add(int Num1, int Num2)
        {
            return Num1 + Num2;
        }
    }

Added a new method for adding two numbers and returns the sum back to the calling function. Now, I am going to create a unit test project and will create a test method for testing the newly added method.

Right click on the solution to bring up the context menu and then select New Project from the Add menu item

From the dialog box select Unit Test Project under Test in the tree menu, give a name for the project and click on the Ok button.

If you look in the Solution Explorer, you can see that a new project has been added to the solution along with a cs file.

Let's modify the file to write the test method which targets the Add method in our application. First of all you need to add the reference of our Program class for accessing the Add method in our test project. 

namespace SampleVSOTests
{
    [TestClass]
    public class TestCases
    {
        [TestMethod]
        public void ShowMethodTest()
        {

            int retVal = Program.Add(1,5);
            Assert.AreEqual(6, retVal);
            
        }
    }
}

Here, you will note that the class is decorated with the [TestClass] attribute and the method with [TestMethod] attribute. The TestMethod attribute indicates that the class contains methods with TestMethod attribute and without this the methods inside that will be ignored when the unit test is run. Similarly for TestMethod, a test class can have normal methods as well as test methods. The test method attribute is used to indicate which all methods needs to be included while running the test cases by the framework.

So, ShowMethodTest is decorated with TestMethod attribute and inside that I am calling the Add method in the Program class and storing the return value in a variable. Then, using the AreEqual method in the Assert class I am verifying that if the returned value is matching the expected value. When the test method is run, the test case will be a success or failure depending on the result of the AreEqual method.

Running Unit Test in Local

To run the test method, right click on the method in the editor and select Run Tests option from the menu as shown below

And the results can be viewed from the Test Explorer window. If it passes successfully, then a green icon with a tick mark is shown otherwise it will have a red cross mark.

Syncing the Remto Repo.

For our scenario to work, I need to check in these newly added files to the remote repository, otherwise during the building process it won't be unable to find any unit test methods. In the earlier post, I used the command prompt for check in and syncing the files using Git commands, here I will be using the features in Team Explorer in Visual Studio for that.

If you got to the Changes section for the Team Explorer, it will list all the files with the changes that needs to be checked in as shown below.

Let's checkin the files by giving a description for the changes in the message text box and click on Commit All button.

  

If the commit was successfull then it will show the message as give below.

Note that I have commited these changes to my local repository only, to make the changes in the remote repo click on Sync link. The outgoing commit will the list the changeset which I am pushing into the remote repo.

On successful push, you will get the message as given below.

  

Build and Unit Tests in Portal.

Whenever you create a build definition for Visual Studio template, a Visual Studio Test step will be added automatically by Visual Studio Team Services. You can verify that by the editing the build definition and see if the highlighted one in the below image exists in your build definition

If it's not there, click on the Add Build Step button to bring up the Add tasks dialog and click on the Add button in the Visual Studion Entry as shown below

Viewing Test Results

Let's check whether our unit test cases were executed or not by checking the statistics of the completed build. Go to completed build page and click on the latest entry to the view the details.  

You will be redirected to the details page, where it will have a chart for Test Results showing the number of passed and failed ones and pass percerntage as shown below.

   


No Comments

Add a Comment