163

NUnit 有一个名为 Values 的特性,如下所示:

[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()
{
    // ...
}
4

9 回答 9

188

编辑 4:看起来这是在 2016 年 6 月 17 日在 MSTest V2 中完成的:https ://blogs.msdn.microsoft.com/visualstudioalm/2016/06/17/taking-the-mstest-framework-forward-with-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:这个问题是在 Visual Studio 的 UserVoice 论坛中提出的。最后更新状态:

已开始 · Visual Studio 团队管理员 Visual Studio 团队(产品团队,Microsoft Visual Studio)已回复 · 2016 年 4 月 25 日 感谢您的反馈。我们已经开始着手这方面的工作。

Pratap Lakshman Visual Studio

https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/3865310-allow-use-of-datatestmethod-datarow-in-all-unit

于 2012-12-04T19:51:48.090 回答
58

此功能现在处于预发布状态,适用于 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);
    }
}
于 2016-06-17T14:27:50.680 回答
49

不幸的是,旧版本的 MSTest 不支持它。显然有一个可扩展模型,您可以自己实现它。另一种选择是使用数据驱动的测试

我个人的意见是坚持使用 NUnit ......

从 Visual Studio 2012 更新 1 开始,MSTest 具有类似的功能。请参阅 McAden 的回答

于 2012-01-26T16:55:21.770 回答
13

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

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

于 2012-01-26T16:58:31.623 回答
7

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

于 2012-01-27T05:15:37.337 回答
5

我无法让 TheDataRowAttribute在 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 中)。

于 2016-10-27T16:36:47.740 回答
5

实现起来非常简单——你应该使用TestContext属性和TestPropertyAttribute.

例子

public TestContext TestContext { get; set; }
private List<string> GetProperties()
{
    return TestContext.Properties
        .Cast<KeyValuePair<string, object>>()
        .Where(_ => _.Key.StartsWith("par"))
        .Select(_ => _.Value as string)
        .ToList();
}

//usage
[TestMethod]
[TestProperty("par1", "http://getbootstrap.com/components/")]
[TestProperty("par2", "http://www.wsj.com/europe")]
public void SomeTest()
{
    var pars = GetProperties();
    //...
}

编辑:

我准备了一些扩展方法来简化对TestContext属性的访问,并且就像我们有几个测试用例一样。请参阅此处处理简单测试属性的示例:

[TestMethod]
[TestProperty("fileName1", @".\test_file1")]
[TestProperty("fileName2", @".\test_file2")]
[TestProperty("fileName3", @".\test_file3")]
public void TestMethod3()
{
    TestContext.GetMany<string>("fileName").ForEach(fileName =>
    {
        //Arrange
        var f = new FileInfo(fileName);

        //Act
        var isExists = f.Exists;

        //Asssert
        Assert.IsFalse(isExists);
    });
}

以及创建复杂测试对象的示例:

[TestMethod]
//Case 1
[TestProperty(nameof(FileDescriptor.FileVersionId), "673C9C2D-A29E-4ACC-90D4-67C52FBA84E4")]
//...
public void TestMethod2()
{
    //Arrange
    TestContext.For<FileDescriptor>().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 =>
        {
            //Act
            var fileInfoString = fileInfo.ToString();

            //Assert
            Assert.AreEqual($"Id: {fileInfo.FileVersionId}; Ext: {fileInfo.Extension}; Name: {fileInfo.Name}; Created: {fileInfo.CreatedOn}; AccessPolicy: {fileInfo.AccessPolicy};", fileInfoString);
        });
}

查看扩展方法示例集以了解更多详细信息。

于 2016-05-16T07:48:38.110 回答
4

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

查看使用 PostSharp 在 MSTest 中启用参数化测试

于 2012-01-26T16:55:03.903 回答
4

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

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

  1. DemoTest_B10_A5.test
  2. DemoTest_A12_B4.test

    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) { }
    }
    
于 2014-03-25T17:13:28.817 回答