2
4

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 3 years have passed since last update.

C#のテストコードについて

Posted at

初めてC#のテストコードを学びましたので、備忘録として残します。

テストするプロジェクトを作成する

[新しいプロジェクトの作成」からテストするためのコードを作成します。

using DDD.WinForm.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TDD.UI

{

    public class Form1ViewModel : ViewModelBase

    {

        private IDB _db;

        public Form1ViewModel(IDB db)
        {
            _db = db;
        }
        private string _aTextBoxText = string.Empty;
        public string textBox1Text 
        {
            get { return _aTextBoxText;  }
            set
            {
                SetProperty(ref _aTextBoxText, value);
            }
        }

        private string _bTextBoxText = string.Empty;
        public string textBox2Text
        {
            get { return _bTextBoxText; }
            set
            {
                SetProperty(ref _bTextBoxText, value);
            }
        }

        private string _resultLabelText = string.Empty;
        public string ResultLabelText
        {
            get { return _resultLabelText; }
            set
            {
                SetProperty(ref _resultLabelText, value);

            }
        }


        public void CalclationAction()
        {
            int a = Convert.ToInt32(textBox1Text);
            int b = Convert.ToInt32(textBox2Text);

            
            int dbValue = _db.GetDBValue();
            ResultLabelText = (Calclation.Sum(a, b) + dbValue).ToString();

        }

    }

}

二つの入力値を取得してint型に変換します。
二つの値を足してデータベースの値を追加で足します。
最終的な値を文字列型で表示します。

public void CalclationAction()
        {
            int a = Convert.ToInt32(textBox1Text);
            int b = Convert.ToInt32(textBox2Text);

            
            int dbValue = _db.GetDBValue();
            ResultLabelText = (Calclation.Sum(a, b) + dbValue).ToString();

        }

テストコードを書く

ソリューションエクスプローラーで右クリックして「追加」からテストコード用のファイルを追加します。


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


namespace TDDTest.Tests

{

    [TestClass]

    public class UnitTest1
    {

        [TestMethod]

        public void TestMethod1()
        {

           var val = TDD.UI.Calclation.Sum(2, 5);

            Assert.AreEqual(7, val);
        }

        [TestMethod]
        public void 平均値を取得()
        {
            var list = new List<int> { 1, 2, 3, 4, 5 };
            var result = TDD.UI.Calclation.Ave(list);
            Assert.AreEqual(3, result);
        }
    }
}

Usingディレクティブの追加

テストコードにはあらかじめ以下のコードを書いておくことが必須です。

using Microsoft.VisualStudio.TestTools.UnitTesting;

テスト項目の追加

最初に[TestMethod]を書いてテスト内容を記述します。

[TestMethod]
        public void 平均値を取得()
        {
            var list = new List<int> { 1, 2, 3, 4, 5 };
            var result = TDD.UI.Calclation.Ave(list);
            Assert.AreEqual(3, result);
        }

リストで1~5の整数を用意しておいて関数で平均値を算出します。
引数の左側に予想値、右側に結果の値が来るようにします。
二つの値が正しければテストが通ります。

テストする

Visual Studioのメニュー「テスト」からテストエクスプローラを追加します。
「すべてのテストを実行」をクリック(左上の緑の矢印マークです)
testCode.PNG

結果が正しい場合はすべて緑のチェックが表示されます。
そうでない場合は赤でバツがつきます。

まとめ

テストコードの手順をテストするプロジェクトの開発からテストコード開発まで例を用いて紹介しました。
テストエクスプローラを用いたテストのやり方を書きました。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?