Test Driven Development(TDD) with VSTS

Posted by: Subodh Sohoni , on 4/4/2008, in Category VSTS & TFS (Azure DevOps)
Views: 27451
Abstract: Test Driven Development (TDD) is one of the many programming practices which are part of Agile Methodology. Visual Studio Team System encourages TDD when we select MSF Agile during the team project creation process. In this article, we will explore Test Driven Development with VSTS.
Test Driven Development(TDD) with VSTS
 
Test Driven Development (TDD) is one of the many programming practices which are part of Agile Methodology. Visual Studio Team System encourages TDD when we select MSF Agile during the team project creation process.
Benefits of using Test Driven Development are as follows: 
  1. Necessary and sufficient code is created.
  2. Created code is highly de-coupled since we write the code in response to a test which meets a specific user requirement.
  3.  Regression testing and recursive debugging is almost not required which reduces time to create code.
  4. Created code is usually bug free from the beginning since we are continuously testing the code.
The practice of TDD requires us to treat tests as first class artifacts while architecting a solution. During TDD we write tests not as an afterthought to ensure our code works, but instead as part of the normal course of building software. Instead of writing our detailed design specifications on paper in the form of functional specifications, we write them in code in the form of tests. Instead of first striving to perfectly design a system on paper, we use tests to guide our design. From the requirements we create test cases and start the actual coding in the form of tests. The crucial part is to write necessary and sufficient tests to satisfy all the finalized requirements. By writing the application code which ensures that test does not fail, we are guaranteed that the application code satisfies all the requirements as well as we have not created any un-necessary code.
The TDD methodology involves following steps:
TDD Methodology
Let us take an example to understand this methodology.
Most of the applications need a password to authenticate the user. A typical requirement is that password should not be null or blank and should have minimum 6 characters and maximum 12 characters. The same requirement may mention that characters in the password should be combination of upper, lower characters and numerals, each appearing at the least once. To reduce complexity of code, we will ignore this last part for the time being and create code to fulfill first two conditions. In the refined form of requirements, I will restate:
TDD Requirement
The next step is to create tests that fulfill these requirements.
We begin with creation of a small table in the testing database. It will contain columns like UserId, Password and IsValid fields. Last column is a Boolean field to set the information whether password is acceptable as per our requirements or not. We will pass these to the application code through the test method and check if the application code accepts it or not. To pass the test the result returned from application code should be same as whatever is in the database for that record.
The password needs to be validated when it is being changed by the user. In our solution we add New Unit Test and write code for tests as follows:
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace VSTSDemo.Test

{
///<summary> 

/// Summary description for ChangePasswordTest
///</summary>
[TestClass]
public class ChangePasswordTest
{

public ChangePasswordTest()

{
// 

// TODO: Add constructor logic here
//
}
enum Column
{
UserId,
Password,
IsValid
}

private TestContext TestContextInstance;

public TestContext TestContext

{
get 

{

return TestContextInstance;

}
set 

{

TestContextInstance = value;

}
}
/* We can use any database to get the data to be tested for validity. In this example I am getting data from the MS Access database which was created in the earlier example */

[DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\Documents and Settings\\Administrator\\My Documents\\TestDB.mdb\"", "Users", DataAccessMethod.Sequential), TestMethod]

public void ChangePasswordTestMethod()

{

string UserId = (string)TestContext.DataRow[(int)Column.UserId];

string Password = TestContext.DataRow[(int)Column.Password] asstring;

bool IsValid = (bool)TestContext.DataRow[(int)Column.IsValid];

// Create instance of the class which is to be tested setting some initial values.

LogonInfo logonInfo = new LogonInfo(UserId, "P@ssW0rd");

// Execute ChangePassword Method which internally should validate // the // passed password.

bool IsPasswordValid = logonInfo.ChangePassword(Password);

Assert.AreEqual(IsValid, IsPasswordValid, "Password test succeeded");

}
}

 
The test method in this example contains code to instantiate a class named LogonInfo. In the true TDD spirit, we have not created that class and its methods as yet. This will mean that the test class will not compile. We now start writing the code for application which is just to fulfill the necessary and sufficient condition.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace VSTSDemo

{
public class LogonInfo 

{
/* Write a constructor to initialize private fields _UserId and _Password through UserId and Password propertied. In the set accessor of each property do the validation of data to be assigned. During the validation of Password, check for null, blank and the length, this should be within 6 to 12 characters. */

public LogonInfo(string userId, string password)

{

this.UserId = userId;

this.Password = password;

}

private string _UserId;

public string UserId

{
get 

{

return _UserId;

}
set 

{

if (value == null)

throw new ArgumentNullException("Null Argument not acceptable");

elseif (value.Trim() == string.Empty)

throw new ArgumentException("User Id empty not acceptable");

_UserId = value;

}
}

private string _Password;

public string Password

{

get { return _Password; }

privateset 

{

string errorMessage;

if (!IsValidPassword(value, out errorMessage))

{

throw new ArgumentException(

errorMessage);
}

_Password = value;

}
}

public static bool IsValidPassword(string value, out string errorMessage)

{

bool isValid;

if (value == null || value.Trim() == string.Empty)

{

isValid = false;

errorMessage = "Password may not be null or blank.";

}
else

{

if (value.Length > 5 && value.Length < 13)

{

isValid = true;

errorMessage = "";

}
else

{

isValid = false;

errorMessage = "Password does not meet the complexity requirements.";

}
}

return isValid;

}

public bool ChangePassword(string newPassword)

{
try

{

Password = newPassword;
//TO DO: Write code to enter the password in the database

return true;

}
catch

{

return false;

}
}
}
}

 

Limitations and drawbacks of TDD

  1.  A lot of code gets created since we are writing test code for even every small requirement. The time required to create this code is more than recovered by saving in time for regression testing and recursive debugging.
  2. Automated testing tool like VSTS are essential to implement this methodology
Conclusion
MSF 4.0 Agile encourages Test Driven Development (Test First Development). TDD has many benefits for quality and productivity improvement. Code generated using TDD is bug free. Minimum application code gets generated in this process. VSTS provides automated testing features and attributes which are easy to use during TDD. I hope the article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Subodh is a consultant and corporate trainer. He has overall 28+ years of experience. His specialization is Application Lifecycle Management and Team Foundation Server. He is Microsoft MVP – VS ALM, MCSD – ALM and MCT. He has conducted more than 300 corporate trainings and consulting assignments. He is also a Professional SCRUM Master. He guides teams to become Agile and implement SCRUM. Subodh is authorized by Microsoft to do ALM Assessments on behalf of Microsoft. Follow him on twitter @subodhsohoni


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by jbaird on Tuesday, April 22, 2008 1:43 PM
the only comment I have is that you shouldn't be using a database to get your testing data.  You are testing network latency and database operations in addition to testing your password methods.  Use a mock to simulate the database and test away.  Otherwise, Good Job.
Comment posted by Thanks for your comments on TDD with VSTS article on Friday, April 25, 2008 2:08 AM
Thanks for your valuable feedback on the article. I agree with your observation that unit testing of the method only should be focused and will take care of such thing in my next articles.
Comment posted by Roger Pearse on Tuesday, June 3, 2008 6:54 PM
This is a frustrating article.  We've all got VB.Net 2008, we all have testing in it, and we all know what TDD is.  What we do NOT know is how to make it work in Visual Studio.  And we don't learn from this.  For instance, all the unit tests are in a separate assembly; so how do we test the private methods?  In Java you just make sure they're in the same package; but in .NET?

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook