2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Minecraft Fabric】GameTest APIでコードをテストする

Last updated at Posted at 2024-04-04

MinecraftのFabricのmodでテストをする方法があまり見つからず、試行錯誤してやっと動かせるとこまで出来たので、テストの作成方法を解説します。

手順:

  1. build.gradleのloomに手を加える
  2. テスト用クラスをメインモジュールに追加する
  3. fabric.mod.jsonのentrypointsにfabric-gametestを追加する

build.gradleのloomに手を加える

まず最初に、build.gradleのloomに以下のブロックを挿入し、プロジェクトをビルドします。

挿入前

loom {
    splitEnvironmentSourceSets()

	mods {
		"modid" {
			sourceSet sourceSets.main
			sourceSet sourceSets.client
		}
	}
}

挿入後

loom {
    splitEnvironmentSourceSets()

	mods {
		"modid" {
			sourceSet sourceSets.main
			sourceSet sourceSets.client
		}
	}

	runs{
		gameTest {
			server()
			ideConfigGenerated(true)
			vmArg "-Dfabric-api.gametest=1"
		}
	}
}

テスト用クラスをメインモジュールに追加する

package fabrictestexample.gametest;

import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;

public class TestExample {
    @GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
    public void test(TestContext testContext){
        testContext.complete();
    }
}

fabric.mod.jsonのentrypointsにfabric-gametestを追加する

"entrypoints": {
		"fabric-gametest": [
			"fabrictestexample.gametest.TestExample"
		],	
}

パッケージ名やクラス名は各自で置き換えてください

そして、プロジェクトをビルドすると、実行構成に Minecraft Game Test が生えているはずなのでそれを実行またはデバッグすると、先ほど書いたクラスのGameTestアノテーションが付いているメゾットが実行されます。(ブレークポイントも効きます)

クライアントは実行されず、サーバーだけ動いてテストが終わると終了します

実行ログ

[19:57:04] [main/INFO] (FabricGameTestHelper) Starting test server
[19:57:05] [main/INFO] (Minecraft) Loaded 7 recipes
[19:57:06] [main/INFO] (Minecraft) Loaded 1395 advancements
[19:57:06] [main/INFO] (BiomeModificationImpl) Applied 0 biome modifications to 0 of 64 new biomes in 1.657 ms
[19:57:07] [Server thread/INFO] (Minecraft) Preparing start region for dimension minecraft:overworld
[19:57:07] [Server thread/INFO] (Minecraft) Preparing spawn area: 0%
[19:57:08] [Worker-Main-8/INFO] (Minecraft) Preparing spawn area: 83%
[19:57:08] [Worker-Main-3/INFO] (Minecraft) Preparing spawn area: 83%
[19:57:08] [Server thread/INFO] (Minecraft) Time elapsed: 1194 ms
[19:57:08] [Server thread/INFO] (Minecraft) Started game test server
[19:57:09] [Server thread/INFO] (Minecraft) Running test batch 'defaultBatch:1' (1 tests)...
[19:57:09] [Server thread/INFO] (Minecraft) 1 tests are now running at position -10051631, -59, 6091919!
[19:57:09] [Server thread/INFO] (Minecraft) [ ]
[19:57:10] [Server thread/INFO] (Minecraft) [ ]
[19:57:13] [Server thread/INFO] (Minecraft) [+]
[19:57:13] [Server thread/INFO] (Minecraft) ========= 1 GAME TESTS COMPLETE ======================
[19:57:13] [Server thread/INFO] (Minecraft) All 1 required tests passed :)
[19:57:13] [Server thread/INFO] (Minecraft) ====================================================
[19:57:13] [Server thread/INFO] (Minecraft) Stopping server
[19:57:13] [Server thread/INFO] (Minecraft) Saving players
[19:57:13] [Server thread/INFO] (Minecraft) Saving worlds
[19:57:14] [Server thread/INFO] (Minecraft) Saving chunks for level 'ServerLevel[Test Level]'/minecraft:overworld
[19:57:14] [Server thread/INFO] (Minecraft) Saving chunks for level 'ServerLevel[Test Level]'/minecraft:the_end
[19:57:14] [Server thread/INFO] (Minecraft) Saving chunks for level 'ServerLevel[Test Level]'/minecraft:the_nether
[19:57:14] [Server thread/INFO] (Minecraft) ThreadedAnvilChunkStorage (world): All chunks are saved
[19:57:14] [Server thread/INFO] (Minecraft) ThreadedAnvilChunkStorage (DIM1): All chunks are saved
[19:57:14] [Server thread/INFO] (Minecraft) ThreadedAnvilChunkStorage (DIM-1): All chunks are saved
[19:57:14] [Server thread/INFO] (Minecraft) ThreadedAnvilChunkStorage: All dimensions are saved
[19:57:14] [Server thread/INFO] (Minecraft) Game test server shutting down

大体こんな感じ

デバッグが楽になったよ
やったね!

終わりに

Fabric GameTestってググってもなかなか出てこないの鬼畜過ぎるだろ...
検索結果に統合版のも混ざっとるし...

参考にしたレポジトリ

使用したエディタ

IntelliJ IDEA Community版

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?