ユニットテスト - peace098beat/windows_applicaciton GitHub Wiki

テスト関連

いますぐ

Microsoft.VisualStudio.TestPlatform.TestFrameworkと

Microsoft.VisualStudio.TestPlatform.TestFramework.Extensionsを参照に追加

自動で追加したほうが簡単

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Seiryuu;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {

        [TestMethod]
        public void TestForm()
        {

            //***************** //
            // private属性のメンバにアクセスするためのラッパー

            var form = new Form1();
            var pbForm = new PrivateObject(form);

            var wfaIn_ = pbForm.GetFieldOrProperty("wfaIn_");
            var pbWfaIn_ = new PrivateObject(wfaIn_);

            var wfaOut_ = pbForm.GetFieldOrProperty("wfaOut_");
            var pbWfaOut_ = new PrivateObject(wfaOut_);
            
            var openFileDialog1 = pbForm.GetField("openFileDialog1");
            var pbOpenFileDialog1 = new PrivateObject(openFileDialog1);

            var saveFileDialog1 = pbForm.GetField("saveFileDialog1");
            var pbSaveFileDialog1 = new PrivateObject(saveFileDialog1);

            //***************** //
            // 読み込むwaveファイル
            string OPEN_WAVE_PATH = @"C:\\Users\\0160929\\Desktop\\02 トラック 02.wav";
            Assert.AreEqual(System.IO.File.Exists(OPEN_WAVE_PATH), true);
            string SAVE_WAVE_PATH = @"C:\\Users\\0160929\\Desktop\\___02 トラック 02.wav";

            //***************** //

            // ファイルを開く
            pbOpenFileDialog1.SetFieldOrProperty("FileName", OPEN_WAVE_PATH);

            Assert.AreEqual(pbOpenFileDialog1.GetFieldOrProperty("FileName"), OPEN_WAVE_PATH);

            // ファイルの読み込み
            pbForm.Invoke("EVH_Read");

            Assert.AreEqual(pbForm.GetFieldOrProperty("processed_"), false);

            // 音楽再生
            pbForm.Invoke("button2_Click", new object[] { null, null });

            Assert.AreEqual(pbWfaIn_.Invoke("IsPlay"), true);

            // 音楽停止
            pbForm.Invoke("button3_Click", new object[] { null, null });

            Assert.AreEqual(pbWfaIn_.Invoke("IsPlay"), false);
            Assert.AreEqual(pbWfaOut_.Invoke("IsPlay"), false);

            // 処理実行
            pbForm.Invoke("button4_Click", new object[] { null, null });

            Assert.AreEqual(pbForm.GetFieldOrProperty("processed_"), true);

            // 再生(処理済み)
            pbForm.Invoke("button5_Click", new object[] { null, null });

            Assert.AreEqual(pbWfaOut_.Invoke("IsPlay"), true);

            // 音楽停止
            pbForm.Invoke("button3_Click", new object[] { null, null });

            Assert.AreEqual(pbWfaIn_.Invoke("IsPlay"), false);
            Assert.AreEqual(pbWfaOut_.Invoke("IsPlay"), false);

            // ファイルの保存
            pbSaveFileDialog1.SetFieldOrProperty("FileName", SAVE_WAVE_PATH);

            pbForm.Invoke("EVH_save");

            // 保存したファイルの存在確認
            string saveFileName = pbSaveFileDialog1.GetFieldOrProperty("FileName") as string;

            Assert.AreEqual(System.IO.File.Exists(saveFileName), true);


        }
    }
}

MSTEST 使い方

http://qiita.com/mima_ita/items/55394bcc851eb8b6dc24

  1. NuGetでMSTESTをインストール
  2. プロジェクトを右クリックして、新規プロジェクトを追加する → ユニットテストの追加
  3. Ctrl+R, A
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using _WpfApplicationOpenTKTemplate1; // ← ターゲットのnamespaceを追加しておく

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var id = new IK();
        }
        [TestMethod]
        public void 足し算()
        {
            int exp = 10;
            int act = 5 + 5;
            Assert.AreEqual(exp, act);
        }

    }
}

MathNetのテスト

        [TestMethod]
        public void TestRz()
        {
            // 単位座標exを-90度回転させて,eyとなるかテスト
            var ik = new IK();
            // ez
            var ez = Vector<double>.Build.DenseOfArray(new double[] { 1, 0, 0 }); // 原点座標
            // -90度回転
            double rad = Math.PI / 2;
            // テスト対象
            Vector<double> Pe = ez * ik.Rz(-rad);
            // 真値
            var ey = Vector<double>.Build.DenseOfArray(new double[] { 0, 1, 0 });
            // 2つのベクトル距離を計算
            var n = Pe.L2Norm();
            // 2つが重なっているかテスト
            Assert.AreEqual((Pe - ey).L2Norm(), 0, 1e-15);

        }
⚠️ **GitHub.com Fallback** ⚠️