Delivering solid user friendly software solutions since the dawn of time.

Unit Testing with MSTest - Notes

by Phani Tipparaju



public void AnEmptyTest() {}

Arrange, Act, Assert (3 A's pattern)

Test Run Configuration:
.testsettings files
Test - Select Active Test Setting


Asserts and Testing Lifecycle

Configure a resource shared among your tests (db, object, log)

// once before and after each unit tests.
public void TestInitialize() {}
public void TestCleanUp() {}
//once before and after all tests in the class
public static void ClassInitialize(TestContext context) {}
public static void ClassCleanup() {}

//once before and after all tests in assembly
public static void AssemblyInitialize(TestContext context) {}
public static void AssemblyCleanUp() {}


Assert.AreEqual(expected, actual, failMessage); // objectOne.Equals(objectTwo)
Assert.AreEqual(expected, actual, delta, failMessage);
Assert.AreEqual(expected, actual, true); // ignore case with strings
Assert.AreSame(a, b); // checks reference then value

CollectionAssert.AllItemsAreNotNull(collection, failMessage);
CollectionAssert.AllItemsAreUnique(collection, failMessage);
CollectionAssert.AreEqual(expected, actual); // compares element by element on reference base
CollectionAssert.AreEquivalent(expected, actual); // order of elements not important
CollectionAssert.IsSubsetOf(subset_collection, collection, failMessage);
StringAssert.Matches("Search for whitespaces", new System.Text.RegularExpressions.Regex(@"\s"));
StringAssert.StartsWith("string","start string");
StringAssert.EndsWith("string","end string");


  • Set by runtime
  • Provides information about the unit test run environment
  • Path to deployment directory
  • Test name
  • URL of the web service
  • Page object of asp.net apps
  • Access to data source with data rows
public TestContext TestContext { get; set; }
TestContext.WriteLine("TestRunDirectory: {0}", TestContext.TestRunDirectory);
TestContext.WriteLine("TestName: {0}", TestContext.TestName);
TestContext.WriteLine("Outcome: {0}", TestContext.CurrentTestOutcome);

Data driven unit-test

Test View - RightClick Properties - Data Connection String
"|DataDirectory|\\UserDetails.xml", "User", DataAccessMethod.Sequential),
DeploymentItem("UserDetailsEntryTest\\UserDetails.xml"), TestMethod]

string email = Convert.ToString(TestContext.DataRow["email"]);

Web Performance Test 

(since it is not part of the standard visual studio I use Selenium)
Create Web App > Create a test project & add web test > Record the web test > Parameterize - Insert Rules - Dynamic Params - Loops > Run Web Test > Analyse results
Web Tests work at HTTP Layer
public class MyCustomWebTest : WebTest {}

Unit Test Features

Ordered Tests

  • Appears as a single test in the Test Window (double click to see individual results)
  • Can execute one test many times
  • Used to group tests together, can contain other ordered tests but no load tests.

Generic Tests

  • Integrate Non-VS automated testing tools
  • Can be run from command line
  • Must return a value of Pass or Fail (return 0 is pass all else is fail)
  • Uses your credentials (trust the author and know what the program will do)

Test Attributes (will show up in .trx xml file)

[Description("This is for testing")]
[TimeOut(1000)] // in milliseconds
[Priority(1)] // not used by the system, but you can use to sort them in the test list editor
[Ignore] // do not run test

Relate to Team Foundation Server
[CssIteration] // Classification.Iteration
[CssProjectStructure] // Classification.Area
[WorkItem] // User Story


[Microsoft.VisualStudio.TestTools.UnitTesting.Web.AspNetDevelopmentServer("WcfService1", "path to service")]
public void TestMethod1()
var target = new WebServiceTesting.ServiceReference.ServiceClient();
var protocol = new System.Web.Services.Protocols.HttpGetClientProtocol();
protocol.Url = "http://myservice.svc";
Assert.IsTrue(WebServiceHelper.TryUrlRedirection(protocol, testContextInstance, "WcfService1"));
actual = target.GetData(1);


  • Instead of providing values in code, depend on configuration files
  • Allows flexibility for regions and file formats.

1. Create a app.config
2. Define "TestConfigurationSection"
<section name="microsoft.visualstudio.testtools" type="..." />
3. Define connectionstrings
<add name="MyExcel" connectionstring="Dsn=Excel ..." providerName="System.Data.Odbc" />
4. Define data source
<add name="MyExcelDataSource" connectionstring="MyExcel" dataTable="Sheet1$" dataAccessMethod="Sequential" />
5. Use the "DataSourceAttribute" for the test method
[DataSource("MyExcelDataSource")] instead of using [DataSource]