Why does TestInitialize get fired for every test in my Visual Studio unit tests?

TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes. Relevant information from the auto generated test-file in Visual Studio: You can use the following additional … Read more

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

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 Assert to verify that an exception has been thrown with MSTest?

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”); }

tech