C# 如何在MSTest中运行具有多个参数的测试方法?

C# 如何在MSTest中运行具有多个参数的测试方法?,c#,unit-testing,nunit,mstest,vs-unit-testing-framework,C#,Unit Testing,Nunit,Mstest,Vs Unit Testing Framework,NUnit有一个称为值的功能,如下所示: [Test] public void MyTest( [Values(1,2,3)] int x, [Values("A","B")] string s) { // ... } 这意味着测试方法将运行六次: MyTest(1, "A") MyTest(1, "B") MyTest(2, "A") MyTest(2, "B

NUnit有一个称为值的功能,如下所示:

[Test]
public void MyTest(
    [Values(1,2,3)] int x,
    [Values("A","B")] string s)
{
    // ...
}
这意味着测试方法将运行六次:

MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
我们现在正在使用MSTest,但是是否有任何等效的方法,以便我可以使用多个参数运行相同的测试

[TestMethod]
public void Mytest()
{
    // ...
}

MSTest不支持该功能,但您可以实现自己的属性来实现该功能


请查看。

不幸的是,旧版本的MSTest不支持该功能。显然有一个问题。另一个选择是使用

我个人的看法是,还是坚持NUnit吧


截至Visual Studio 2012更新1,MSTest具有类似的功能。请参阅。

与NUnit的
Value
(或
TestCase
)属性不完全相同,但MSTest具有该属性,允许您执行类似的操作


您可以将它连接到数据库或XML文件—它不像NUnit的功能那样简单,但它可以完成这项工作。

MSTest有一个强大的属性,名为。使用此功能,您可以根据需要执行数据驱动测试。您可以将测试数据保存在XML、CSV或数据库中。这里有几个链接可以指导您


编辑4:看起来这是在2016年6月17日的MSTest V2中完成的:

原始答案

大约一周前,在Visual Studio 2012 Update 1中,类似的功能现在已成为可能:

[DataTestMethod]
[DataRow(12,3,4)]
[DataRow(12,2,6)]
[DataRow(12,4,3)]
public void DivideTest(int n, int d, int q)
{
  Assert.AreEqual( q, n / d );
}
编辑:这似乎仅在WinRT/Metro的单元测试项目中可用。无赖

编辑2:以下是使用Visual Studio中的“转到定义”找到的元数据:

#region Assembly Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll, v11.0.0.0
// C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0\ExtensionSDKs\MSTestFramework\11.0\References\CommonConfiguration\neutral\Microsoft.VisualStudio.TestPlatform.UnitTestFramework.dll
#endregion

using System;

namespace Microsoft.VisualStudio.TestPlatform.UnitTestFramework
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class DataTestMethodAttribute : TestMethodAttribute
    {
        public DataTestMethodAttribute();

        public override TestResult[] Execute(ITestMethod testMethod);
    }
}
编辑3:这个问题是在VisualStudio的UserVoice论坛上提出的。 上次更新状态:

开始·Visual Studio团队管理Visual Studio团队(产品) 微软(Microsoft Visual Studio)团队于2016年4月25日作出回应,谢谢 为了得到反馈。我们已经开始了这方面的工作

普拉塔普·拉克什曼视觉工作室


当然,还有另一种方法可以做到这一点,这在本线程中没有讨论过,即通过继承包含TestMethod的类。在下面的示例中,只定义了一个TestMethod,但创建了两个测试用例

在Visual Studio 2012中,它在TestExplorer中创建两个测试:

  • 降级测试
  • 降级测试

    public class Demo
    {
        int a, b;
    
        public Demo(int _a, int _b)
        {
            this.a = _a;
            this.b = _b;
        }
    
        public int Sum()
        {
            return this.a + this.b;
        }
    }
    
    public abstract class DemoTestBase
    {
        Demo objUnderTest;
        int expectedSum;
    
        public DemoTestBase(int _a, int _b, int _expectedSum)
        {
            objUnderTest = new Demo(_a, _b);
            this.expectedSum = _expectedSum;
        }
    
        [TestMethod]
        public void test()
        {
            Assert.AreEqual(this.expectedSum, this.objUnderTest.Sum());
        }
    }
    
    [TestClass]
    public class DemoTest_A12_B4 : DemoTestBase
    {
        public DemoTest_A12_B4() : base(12, 4, 16) { }
    }
    
    public abstract class DemoTest_B10_Base : DemoTestBase
    {
        public DemoTest_B10_Base(int _a) : base(_a, 10, _a + 10) { }
    }
    
    [TestClass]
    public class DemoTest_B10_A5 : DemoTest_B10_Base
    {
        public DemoTest_B10_A5() : base(5) { }
    }
    

  • 实现起来非常简单-应该使用
    TestContext
    属性和
    TestPropertyAttribute

    例子
    publictestcontext TestContext{get;set;}
    私有列表GetProperties()
    {
    返回TestContext.Properties
    .Cast()
    .Where(=>u.Key.StartsWith(“par”))
    .Select(=>0.Value作为字符串)
    .ToList();
    }
    //用法
    [测试方法]
    [TestProperty(“par1”http://getbootstrap.com/components/")]
    [TestProperty(“par2”http://www.wsj.com/europe")]
    公共测试()
    {
    var pars=GetProperties();
    //...
    }
    
    编辑: 我准备了一些扩展方法来简化对
    TestContext
    属性的访问,就像我们有几个测试用例一样。请参见此处处理简单测试属性的示例:

    [TestMethod]
    [TestProperty(“fileName1”,@.\test\u file1”)]
    [TestProperty(“fileName2”,@.\test\u file2”)]
    [TestProperty(“fileName3”,@.\test\u file3”)]
    公共void TestMethod3()
    {
    TestContext.GetMany(“文件名”).ForEach(文件名=>
    {
    //安排
    var f=新文件信息(文件名);
    //表演
    var isExists=f.存在;
    //助理
    Assert.IsFalse(isExists);
    });
    }
    
    以及创建复杂测试对象的示例:

    [TestMethod]
    //案例1
    [TestProperty(名称为(FileDescriptor.FileVersionId),“673C9C2D-A29E-4ACC-90D4-67C52FBA84E4”)]
    //...
    公共void TestMethod2()
    {
    //安排
    TestContext.For().Fill(fi=>fi.FileVersionId).Fill(fi=>fi.Extension).Fill(fi=>fi.Name).Fill(fi=>fi.CreatedOn,new-CultureInfo(“en-US”,false)).Fill(fi=>fi.AccessPolicy)
    .ForEach(fileInfo=>
    {
    //表演
    var fileInfoString=fileInfo.ToString();
    //断言
    Assert.AreEqual($“Id:{fileInfo.FileVersionId};Ext:{fileInfo.Extension};Name:{fileInfo.Name};Created:{fileInfo.CreatedOn};AccessPolicy:{fileInfo.AccessPolicy};”,fileInfoString);
    });
    }
    
    有关更多详细信息,请查看的扩展和集。

    此功能现在可用,可与Visual Studio 2015配合使用

    例如:

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        [DataRow(1, 2, 2)]
        [DataRow(2, 3, 5)]
        [DataRow(3, 5, 8)]
        public void AdditionTest(int a, int b, int result)
        {
            Assert.AreEqual(result, a + b);
        }
    }
    

    我无法使
    DataRowAttribute
    在Visual Studio 2015中工作,这就是我最终得到的结果:

    [TestClass]
    public class Tests
    {
        private Foo _toTest;
    
        [TestInitialize]
        public void Setup()
        {
            this._toTest = new Foo();
        }
    
        [TestMethod]
        public void ATest()
        {
            this.Perform_ATest(1, 1, 2);
            this.Setup();
    
            this.Perform_ATest(100, 200, 300);
            this.Setup();
    
            this.Perform_ATest(817001, 212, 817213);
            this.Setup();
        }
    
        private void Perform_ATest(int a, int b, int expected)
        {
            // Obviously this would be way more complex...
    
            Assert.IsTrue(this._toTest.Add(a,b) == expected);
        }
    }
    
    public class Foo
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
    

    这里真正的解决方案是只使用NUnit(除非你像我在这个特定的实例中一样被困在MSTest中)。

    我们使用Selenium生成NUnit代码,所以我们改用NUnit:)我发现在Visual Studio 2012 Update 1中也可以使用类似的功能,仅供以后参考。@McAden你有带解释的链接吗?我在下面给出了一个示例和我博客文章的链接。它提到了必要的属性以及属性上的“DisplayName”属性,该属性用于区分测试资源管理器中的案例。CTP(现在已经正式发布)的10月份公告中也提到了这一点。我在这个问题中添加了这些信息,因为我花了很多时间寻找它。希望这能节省一些时间。Windows Phone现在也受支持,Visual Studio 2012更新2(目前为CTP 4)我有更新1,但无法识别DataTestMethod和DataRow,这些属性在哪个库中?关于DataTestMethod有官方来源吗?它位于哪个名称空间,哪个程序集?我发现UnitTestFramework.dll安装在我的计算机上,在手动引用它之后,我能够编写一个方法