0
1

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 1 year has passed since last update.

[C#] 同じTestに複数のパラメータを渡してテスト実行する

Last updated at Posted at 2021-06-18

UnitTest関連記事

やりたいこと

1つのテストメソッドに、複数のパラメータを渡してテストをしたい。

やりかた

[DataTestMethod]を使う。

テスト対象.cs
namespace UnitTestJikken
{
    public class MyMethods
    {
        public int AddFunc(int v1, int v2)
        {
            return v1 + v2;
        }
    }
}
UnitTest.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestJikken;

namespace UnitTestProject
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var mm = new MyMethods();
            var ans = mm.AddFunc(1, 3);

            Assert.AreEqual(1 + 3, ans);
        }

        [DataTestMethod]
        [DataRow(0, 0)]
        [DataRow(1, 0)]
        [DataRow(0, -1)]
        [DataRow(100, 100)]
        [DataRow(-100, -100)]
        public void DataTestMethod1(int v1, int v2)
        {
            var mm = new MyMethods();
            var ans = mm.AddFunc(v1, v2);

            Assert.AreEqual(v1 + v2, ans);
        }
    }
}

メモ

UnitTest.csの中の、1個目のテストメソッドは普通のテストメソッド。
2つ目が、今回やりたかった、パラメータを渡して複数通りのテストを行えるメソッド。

コード

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?