Does MSTest have an equivalent to NUnit's TestCase?

后端 未结 5 452
余生分开走
余生分开走 2020-11-27 15:03

I find the TestCase feature in NUnit quite useful as a quick way to specify test parameters without needing a separate method for each test. Is there anything s

5条回答
  •  盖世英雄少女心
    2020-11-27 15:33

    Khlr gave a good detailed explanations and apparently this approach started working in VS2015 Express for Desktop. I tried to leave the comment, but my lack of reputation didn't allow me to do so.

    Let me copy the solution here:

    [TestClass]  
     public class StringFormatUtilsTest  
     {  
         [TestMethod]  
         [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);  
         }  
     } 
    

    To use it, just install the NuGet packages MSTest.TestFramework and MSTest.TestAdapter.

    One problem is

    Error CS0433 The type 'TestClassAttribute' exists in both 'Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0 and 'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0

    So, please remove Microsoft.VisualStudio.QualityTools.UnitTestFramework from references of the project.

    You're very welcome to edit the original reply and delete this one.

提交回复
热议问题