MSTest Equivalent for NUnit’s Parameterized Tests?

For those using MSTest2, DataRow + DataTestMethod are available to do exactly this: [DataRow(Enum.Item1, “Name1”, 123)] [DataRow(Enum.Item2, “Name2”, 123)] [DataRow(Enum.Item3, “Name3”, 456)] [DataTestMethod] public void FooTest(EnumType item, string name, string number) { var response = ExecuteYourCode(item, name, number); Assert.AreEqual(item, response.item); } More about it here

TestInitialize vs ClassInitialize

Both attributes are available only for the classes (and hence tests) where they belong. TestInitialize runs before every test that is declared on the the same class where the attribute is declared. ClassInitialize runs only on the initialization of the class where the attribute is declared. In other words it won’t run for every class. … Read more

How can I write output from a unit test?

I was also trying to get Debug or Trace or Console or TestContext to work in unit testing. None of these methods would appear to work or show output in the output window: Trace.WriteLine(“test trace”); Debug.WriteLine(“test debug”); TestContext.WriteLine(“test context”); Console.WriteLine(“test console”); Visual Studio 2012 and greater (from comments) In Visual Studio 2012, there is no … Read more

Do MSTest deployment items only work when present in the project test settings file?

This post here helped me figure out what I needed to do WITHOUT having to manually add items to the .testsettings file. Step 1 – Enable the MS Test DeploymentItem attribute. First up, we need to turn on / enable the DeploymentItem attribute. Go to TEST -> EDIT TEST SETTINGS -> Current Active settings .. … Read more

Does MSTest have an equivalent to NUnit’s TestCase?

Microsoft recently announced “MSTest V2” (see blog-article). This allows you to consistently (desktop, UWP, …) use the DataRow-attribute! [TestClass] public class StringFormatUtilsTest { [DataTestMethod] [DataRow(“tttt”, “”)] [DataRow(“”, “”)] [DataRow(“t3a4b5”, “345”)] [DataRow(“3&5*”, “35”)] [DataRow(“123”, “123”)] public void StripNonNumeric(string before, string expected) { string actual = FormatUtils.StripNonNumeric(before); Assert.AreEqual(expected, actual); } } Again, Visual Studio Express’ Test Explorer … Read more

How do I use MSTest without Visual Studio?

MSTest can be used without installing Visual Studio. You will need to install Visual Studio Test Agent, which is a free download from Microsoft. I think this approach is better from a licensing perspective than manually copying MSTest.exe and its dependencies onto the build server. See this blog for reference: http://blogs.msdn.com/b/anutthara/archive/2009/12/16/running-tests-in-mstest-without-installing-the-vs-ide.aspx

How do I use Assert to verify that an exception has been thrown?

For “Visual Studio Team Test” it appears you apply the ExpectedException attribute to the test’s method. Sample from the documentation here: A Unit Testing Walkthrough with Visual Studio Team Test [TestMethod] [ExpectedException(typeof(ArgumentException), “A userId of null was inappropriately allowed.”)] public void NullUserIdInConstructor() { LogonInfo logonInfo = new LogonInfo(null, “P@ss0word”); }