8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DataTestMethodの使用例

Last updated at Posted at 2018-04-15

DataTestMethod

mstestのパラメーター化テストでDataRowにTupleを使用すると「error CS0182: 属性引数は、定数式、typeof 式、または属性パラメーター型の配列の作成式でなければなりません。」とエラーになる。
DataRowの代わりに、DynamicDataを使用してTupleを含むテストデーターを返すメソッドまたはプロパティを指定する。

UnitTest1.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace UnitTestProject1
{
    [TestClass()]
    public class UnitTest1
    {
        [DataTestMethod()]
        [DataRow("text1", 1)]
        [DataRow("text2", 2)]
        public void DataRowTest(string text, int number)
        {
            var tuple = (text, number);
            Assert.AreEqual(tuple.text, text);
            Assert.AreEqual(tuple.number, number);
        }

        public static IEnumerable<object[]> GetDynamicDataTestData()
        {
            yield return new object[] { ("text1", 1), "text1", 1 };
            yield return new object[] { ("text2", 2), "text2", 2 };
        }

        [DataTestMethod()]
        [DynamicData(nameof(GetDynamicDataTestData), DynamicDataSourceType.Method)]
        public void DynamicDataTest((string text, int number) tuple,
            string text, int number)
        {
            Assert.AreEqual(tuple.text, text);
            Assert.AreEqual(tuple.number, number);
        }
    }
}
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?