【UnityTestRunner】テストケースをScriptableObjectで定義してみた
今回やること
テストケースをScriptableObjectで作ります。
まずScriptableObjectを作成します。
TestCase_Move.cs
using UnityEngine; [CreateAssetMenu(menuName ="TestCase/Move")] public class TestCase_Move : ScriptableObject { public int size; public int[] x; public int[] y; }
数字を入れて・・・
テスト用のスクリプトを書きます・・・
Assertion.cs
using UnityEngine; using NUnit.Framework; public class Assertion : MonoBehaviour { private static string testCasePath = "TestCase/TestCase_Move"; private static object[] DivideCases () { TestCase_Move testCase_Move = Resources.Load(testCasePath) as TestCase_Move; object[] divideCases = new object[testCase_Move.size]; for(int i = 0;i < testCase_Move.size;i++) { divideCases[i] = new object[] { testCase_Move.x[i],testCase_Move.y[i] }; } return divideCases; } private Vector2Int pos = new Vector2Int(0,0); private float moveY = 1; private int fieldSize = 10; [SetUp] public void SetupTest () { pos = new Vector2Int(0,0); } [TestCaseSource("DivideCases")] public void Move2 (int x,int y) { pos.x += x; pos.y += y; Assert.IsTrue(pos.x >= 0 && pos.x <= (fieldSize - 1),"x方向の位置が範囲外です:" + pos.x); Assert.IsTrue(pos.y >= 0 && pos.y <= (fieldSize - 1),"y方向の位置が範囲外です:" + pos.y); } }
[TestCaseSource]でDivideCases()を読み込みます。
Resourcesフォルダの中にテストケース用ScriptableObjectを配置して、DivideCases()の中で読み込みます。
やや強引ですが、これでScriptableObjectで定義した数値を読み取れるようになります!