1
2

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

EditModeTestで楽をしよう

Posted at

はじめに

EditModeTestを利用することで実装の確認などで楽になることがありました。
今回はEditModeTestを利用して少しでも楽をしようという内容になっています。

注意点:
テストをしやすいデータ構造の紹介などは行いません。
簡単なテストのみ行います。

使用したバージョン

Unity 2018.2.11f1

テストのための設定

まずはテストを行うための設定を行っていきます。
メニューバーからWindow > General > Test Runnerを選択します。

launch-test-runner.jpg

Test Runnerウィンドウが表示されたと思います。
テスト用のスクリプトを入れるフォルダを作成するため、ウィンドウ内のCreate EditMode Test Assembly Folderボタンを押します。

test-runner-1.jpg

プロジェクト内にAssets/Testsフォルダが作成されていると思います。
Testsフォルダ内にテスト用のAssembly Definitionも作成されていると思います。
テストに利用するスクリプトはこちらのTestsフォルダ内に作成していきます。

test-folder-1.jpg

これでEditMode Testを行う準備ができました。

ダメージ計算っぽいものを作ってみる

プレイヤーキャラクターからエネミーキャラクターへのダメージ計算処理について考えてみます。
仕様は以下です。

  • プレイヤーの攻撃力からエネミーの防御力を引いたものが基礎ダメージ
  • プレイヤーとエネミーの属性相性が良ければダメージが1.5倍、普通ならダメージは1倍、悪ければ0.5倍
  • ステータスは現状では外部から受け取ることができないので、決め打ちでOK

そのためこのようなコードを書いていました。

BattleDirector.cs
using UnityEngine;

public class BattleDirector : MonoBehaviour
{
    private Player _player;
    private Enemy _enemy;

    private void Start()
    {
        _player = new Player { Attack = 10000, Attribute = CharacterAttribute.Red };
        _enemy = new Enemy { Hp = 10000, Defense = 5000, Attribute = CharacterAttribute.Green };
    }

    public void PlayerAttack()
    {
        var damage = CalcLogic.CalcPlayerAttackDamage(_player, _enemy);
        _enemy.Hp -= damage;
    }
}
CalcLogic.cs
using System.Collections.Generic;
using UnityEngine;

public class CalcLogic
{
    private static readonly List<List<float>> CharacterAttributeRateTable = new List<List<float>>
    {
        new List<float>{1.0f, 1.5f, 0.5f},
        new List<float>{0.5f, 1.0f, 1.5f},
        new List<float>{1.5f, 0.5f, 1.0f},
    };

    public static int CalcPlayerAttackDamage(Player attacker, Enemy defender)
    {
        var baseDamage = attacker.Attack - defender.Defense;
        var damage = baseDamage * GetCharacterAttributeRate(attacker.Attribute, defender.Attribute);
        return Mathf.FloorToInt(damage);
    }

    private static float GetCharacterAttributeRate(CharacterAttribute attackerAttribute, CharacterAttribute defenderAttribute)
    {
        return CharacterAttributeRateTable[(int)attackerAttribute - 1][(int)defenderAttribute - 1];
    }
}
Characters.cs
public enum CharacterAttribute
{
    None, Red, Green, Blue
}

public class Enemy
{
    public int Hp { get; set; }
    public int Defense { get; set; }
    public CharacterAttribute Attribute { get; set; }
}

public class Player
{
    public int Attack { get; set; }
    public CharacterAttribute Attribute { get; set; }
}

プレイヤーが攻撃するタイミングでBattleDirector.PlayerAttack()を呼び出すことでダメージ計算を行い、エネミーにダメージを与えるようになっています。

ダメージ計算が正しく行われているか確認する場合は、いちいちBattleDirector内で定義されているプレイヤーやエネミーのパラメータを変更してプレイするというのを何度か繰り返す必要があります。
スクリプトのコンパイル時間や画面遷移などの時間を考えると少なくない時間がかかってしまいます。

ダメージ計算のテストを作成してみる

ダメージ計算のテストを作成して、ダメージ計算の確認の時間を減らしてみたいと思います。
今回はCalcLogic.CalcPlayerAttackDamageのテストなので、そのままでも問題ありませんが、テストを行う処理はテストスクリプトからアクセスできる状態にする必要があります。
(publicメソッドにしたり、リフレクションなどを利用したりします。)

テスト用のスクリプトをAssets/Testsフォルダに以下に作成します。

CalcDamageTest.cs
using NUnit.Framework;

public class CalcDamageTest
{
    [TestCase(CharacterAttribute.Red, 5000)]
    [TestCase(CharacterAttribute.Green, 2500)]
    [TestCase(CharacterAttribute.Blue, 7500)]
    public void 属性の違いによるダメージ計算テスト(CharacterAttribute playerAttribute, int expectedDamage)
    {
        var player = new Player { Attack = 10000, Attribute = playerAttribute };
        var enemy = new Enemy { Defense = 5000, Attribute = CharacterAttribute.Red };

        var damage = CalcLogic.CalcPlayerAttackDamage(player, enemy);
        Assert.AreEqual(expectedDamage, damage);
    }
}

プレイヤーの属性によってダメージ量が正しく計算できるかのテストを作成しました。
(テストの細かい書き方などは、NUnitなどで調べてみてください。)

作成したテストはTest Runnerで実行することができます。
TestCaseで指定した分のテストがリストに表示されています。

test-runner-2.jpg

Run Allボタンなどからテストを実行します。
テストが無事に完了すると緑のチェックマークが付きます。

以前のように「パラメータを変更して実行する」を繰り返す必要がなくなり、テストを作成する時間が必要となりました。
ダメージ計算処理に仕様追加などがあった場合は、このテストにテスト項目を追加することで実装の確認が楽になります。

まとめ

  • テストを行う環境はTest Runnerから1ボタンで作成できる
  • テストを行う処理はテストスクリプトからアクセスできる必要がある
  • いくつかのパターンを確認するなどテストを利用すると楽ができる
  • 仕様変更などの実装確認でも楽ができる
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?