LoginSignup
12
12

More than 5 years have passed since last update.

[Unity]Test Runnerのシンプルなサンプル

Posted at

準備

  1. Editorという名前のフォルダを作る
  2. 以下の画像のようにスクリプト作成

スクリーンショット 2018-01-13 19.28.45.png

ここではSampleTestと名付けました.

すると以下のようなスクリプトが生成されます.

SampleTest.cs
using UnityEngine;
using UnityEditor;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;

public class SampleTest {

    [Test]
    public void SampleTestSimplePasses() {
        // Use the Assert class to test conditions.
    }

    // A UnityTest behaves like a coroutine in PlayMode
    // and allows you to yield null to skip a frame in EditMode
    [UnityTest]
    public IEnumerator SampleTestWithEnumeratorPasses() {
        // Use the Assert class to test conditions.
        // yield to skip a frame
        yield return null;
    }
}

クラスの中身は一度全部消してしまいましょう.
そして以下のように書きます.

using UnityEngine;
using UnityEditor;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;

public class SampleTest {

    [Test]
    public void AnyTest(){
        int a = 5, b = 4;
        int ans = a * b;

        Assert.AreEqual (ans, 20);
    }
}

Unityエディタに戻り,
Window => Test Runnerを押す.
するとこんなWindowが出てくるはずです.

スクリーンショット 2018-01-13 19.36.23.png

左上のRun Allボタンを押すとテスト開始

スクリーンショット 2018-01-13 19.37.58.png
チェックマークがついたらテストが通ったことになります.

ポイント

using NUnit.Framework
必要です.

[Test]
テストアトリビュート.
このアトリビュートがついた関数がテストの対象になります.

Asset.AreEqual(val1, val2)
val1とval2が等しいときにテストを通す.

12
12
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
12
12