Unit Testing in .NET Projects
Pages: 1, 2, 3, 4, 5
VSTS
Visual Studio Team System (VSTS) is a beta-level commercial SCM and testing platform from Microsoft, with unit testing as one of its testing types. I will be covering only the unit testing features here, but it's worth noting that VSTS supports other testing, such as functional and load testing. This article is based on Beta 2 of VSTS; as this is a beta product, some features could change for the final release.
Test Class
The class used to demo the VSTS tests is a class with simple addition and subtraction methods.
public class Simple
{
public int Add(int intNum1, int intNum2)
{
return intNum1 + intNum2;
}
public int Subtract(int intNum1, int intNum2)
{
return intNum1 - intNum2;
}
}
Assert Tests
VSTS supports the Assert range of tests like MbUnit and NUnit, with commonly
found tasks such as Assert.AreEqual, etc. VSTS has also added other tasks to its
Assert test, including tasks such as GetHashCodeTests, Inconclusive,
IsInstanceOf, and IsNotIstanceOf. The following is an example of an Assert test,
here showing the AreEqual task.
[TestClass]
public class SimpleTest
{
VSTSTest.Simple objSimple;
[TestInitialize()]
public void Initialize()
{
objSimple = new VSTSTest.Simple();
}
[TestMethod]
public void TestAdd()
{
Assert.AreEqual(2, objSimple.Add(1, 1));
}
[TestMethod]
public void TestSubtract()
{
Assert.AreEqual(2, objSimple.Subtract(3,1));
}
}
Note how VSTS lays out its unit tests. The TestClass attribute marks a class
as a unit test class. The TestInitalize attributed is fired up each and every
time by a test runner (e.g., the IDE) to allow a test class to set up any
objects or values that the class might need. While not shown here, it is worth
considering the TestCleanup attribute, which allows a class to clean up when the
tests are complete. Finally, note that each test method in our class is marked
with the TestMethod attribute.
Data-Driven Test Fixture
It's often vital to drive different sets of data through your unit tests; by running a broad spectrum of data through your tests, you can build a bigger picture of inputs and expected outputs. VSTS supports running tests through a database table of your choice; for example, a sample table may be as follows.
CREATE TABLE [UnitTest] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TestVal1] [int] NULL ,
[TestVal2] [int] NULL ,
[Result] [int] NULL
) ON [PRIMARY]
GO
We can then alter our unit test as follows:
[TestClass]
public class SimpleDBTest
{
VSTSTest.Simple objSimple;
private TestContext testContextInstance;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestInitialize()]
public void Initialize()
{
objSimple = new VSTSTest.Simple();
}
[TestMethod, DataSource("System.Data.SqlClient",
"Data Source=LOCALHOST;
Initial Catalog=TestDB;
Integrated Security=True",
"UnitTest",
DataAccessMethod.Sequential)]
public void TestAddDB()
{
Assert.AreEqual((int)TestContext.DataRow["Result"],
objSimple.Add((int)TestContext.DataRow["TestVal1"],
(int)TestContext.DataRow["TestVal2"]));
}
}
This is almost like the previous example; however, to enable data-driven testing,
we extend the TestMethod attribute with the additional DataSource value. Here
we add a database connection string and table name (in this case, UnitTest), and
then reference the data using the TestContect.DataRow.

