| name | creating-server-tests |
| description | moorestech_serverのNUnitテストを作成するスキル。テストの雛形生成、初期化パターン、命名規約、 テスト用IDの使い方を含む。 Use when: (1) moorestech_serverに新しいテストクラスを追加する時 (2) 「テストを書いて」「テストを作成して」とサーバー側のテスト作成を依頼された時 (3) 既存機能のテストカバレッジを追加する時 (4) CombinedTest/UnitTest/PacketTestのいずれかを作成する時
|
Server Test Creator
moorestech_serverのテスト作成ガイド。NUnit + Unity Test Frameworkを使用。
テスト種別の選択
| 種別 | 用途 | 配置先 |
|---|
| UnitTest | 単一クラス・メソッドの検証 | Tests/UnitTest/{Layer}/{Category}/ |
| CombinedTest | 複数システムの統合検証 | Tests/CombinedTest/{Layer}/ |
| PacketTest | プロトコル通信の検証 | Tests/CombinedTest/Server/PacketTest/ |
テストファイルは moorestech_server/Assets/Scripts/Tests/ 配下の Server.Tests.asmdef に含まれる。
ワークフロー
1. テスト種別に応じたテンプレートを選択
UnitTest - 単一機能テスト:
using NUnit.Framework;
using Server.Boot;
using Tests.Module.TestMod;
using Game.Context;
namespace Tests.UnitTest.{Layer}.{Category}
{
public class {Feature}Test
{
[SetUp]
public void Setup()
{
var (_, serviceProvider) = new MoorestechServerDIContainerGenerator()
.Create(new MoorestechServerDIContainerOptions(TestModDirectory.ForUnitTestModDirectory));
}
[Test]
public void {動作内容}Test()
{
Assert.AreEqual(expected, actual);
}
}
}
CombinedTest - 統合テスト:
using System;
using Core.Master;
using Core.Update;
using Game.Context;
using NUnit.Framework;
using Server.Boot;
using Tests.Module.TestMod;
using UnityEngine;
namespace Tests.CombinedTest.{Layer}
{
public class {Feature}Test
{
[Test]
public void {動作内容}Test()
{
var (_, serviceProvider) = new MoorestechServerDIContainerGenerator()
.Create(new MoorestechServerDIContainerOptions(TestModDirectory.ForUnitTestModDirectory));
ServerContext.WorldBlockDatastore.TryAddBlock(
ForUnitTestModBlockId.ChestId,
new Vector3Int(0, 0, 0),
BlockDirection.North,
Array.Empty<BlockCreateParam>(),
out var block);
var component = block.GetComponent<TargetComponent>();
GameUpdater.UpdateOneTick();
Assert.AreEqual(expected, actual);
#region Internal
void HelperMethod() { }
#endregion
}
}
}
PacketTest - テンプレートは references/packet-test.md を参照。
2. 必須ルール
- テスト用IDは
ForUnitTestModBlockId / ForUnitTestItemId から取得 - マジックナンバー禁止
- コメントは日英2行セット で記述
- try-catch禁止 - 条件分岐で対応
- デフォルト引数禁止 - 呼び出し側を変更
- 複雑なテストでは
#region Internal + ローカル関数 を使用
#endregion の下にコードを書かない
- 新しいブロックIDが必要な場合は
ForUnitTestModBlockId.cs と blocks.json を更新
3. テスト実行
作成後、uloop CLIでテスト実行(サーバーテストはクライアントプロジェクトからインポートされているため、クライアントのproject-pathで実行する):
uloop run-tests --project-path ./moorestech_client --test-mode EditMode --filter-type regex --filter-value "^Tests\.{TestType}\.{Layer}\.{ClassName}$"
注意: 新規サーバー.csファイルはUnity再起動(Refresh不可)でないとコンパイルされない(file:パッケージのため)。
リソース