Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add game test section #279

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions develop/automatic-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ This page explains how to write code to automatically test parts of your mod. Th

Unit tests should be used to test components of your code, such as methods and helper classes, while game tests spin up an actual Minecraft client and server to run your tests, which makes it suitable for testing features and gameplay.

::: warning
Currently, this guide only covers unit testing.
:::

## Unit Testing {#unit-testing}

Since Minecraft modding relies on runtime byte-code modification tools such as Mixin, simply adding and using JUnit normally would not work. That's why Fabric provides Fabric Loader JUnit, a JUnit plugin that enables unit testing in Minecraft.
Expand Down Expand Up @@ -87,3 +83,31 @@ Add this to your `.github/workflows/build.yml` file, below the `./gradlew build`
**/build/reports/
**/build/test-results/
```

## Game Tests {#game-tests}

Minecraft provides the game test framework for testing server-side features. Fabric additionally provides client game tests for testing client-side features, similar to an end-to-end test.

### Setting up Game Tests with Fabric Loom {#setting-up-game-tests-with-fabric-loom}

Both server and client game tests can be set up manually or with Fabric Loom. This guide will use Loom.

To add game tests to your mod, add the following to your `build.gradle`:

@[code lang=groovy transcludeWith=:::automatic-testing:game-test:1](@/reference/latest/build.gradle)

To see all available options, see [the Loom documentation on tests](./loom/fabric-api#tests).

### Writing Game Tests {#writing-game-tests}

You can now create server and client game tests in the `src/gametest/java` directory. Here is a barebones example of each.

Server game test:

<<< @/reference/latest/src/gametest/java/com/example/docs/FabricDocsGameTest.java

Client game test:

<<< @/reference/latest/src/gametest/java/com/example/docs/FabricDocsClientGameTest.java

See the respective javadocs in Fabric API for more info.
2 changes: 1 addition & 1 deletion reference/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.9-SNAPSHOT' apply false
id 'fabric-loom' version '1.10-SNAPSHOT' apply false
id "com.diffplug.spotless" version "6.23.3" apply false
id 'checkstyle'
}
Expand Down
12 changes: 11 additions & 1 deletion reference/latest/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def minecraftVersion = "1.21.4"
def yarnVersion = "1.21.4+build.4"
def fabricApiVersion = "0.114.0+1.21.4"
def fabricApiVersion = "0.116.1+1.21.4"

dependencies {
minecraft "com.mojang:minecraft:${minecraftVersion}"
Expand All @@ -13,3 +13,13 @@ test {
useJUnitPlatform()
}
// :::automatic-testing:2

// :::automatic-testing:game-test:1
fabricApi {
configureTests {
createSourceSet = true
modId = base.archivesName // Your mod id here
eula = true
}
}
// :::automatic-testing:game-test:1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.docs;

import net.fabricmc.fabric.api.client.gametest.v1.FabricClientGameTest;
import net.fabricmc.fabric.api.client.gametest.v1.context.ClientGameTestContext;
import net.fabricmc.fabric.api.client.gametest.v1.context.TestSingleplayerContext;

@SuppressWarnings("UnstableApiUsage")
public class FabricDocsClientGameTest implements FabricClientGameTest {
@Override
public void runTest(ClientGameTestContext context) {
try (TestSingleplayerContext singleplayer = context.worldBuilder().create()) {
singleplayer.getClientWorld().waitForChunksRender();
context.takeScreenshot("fabric-docs-reference-singleplayer-test");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.docs;

import net.minecraft.block.Blocks;
import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;

import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;

public class FabricDocsGameTest implements FabricGameTest {
@GameTest(templateName = EMPTY_STRUCTURE)
public void test(TestContext context) {
context.expectBlock(Blocks.AIR, 0, 0, 0);
context.complete();
}
}
6 changes: 6 additions & 0 deletions reference/latest/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
],
"fabric-datagen": [
"com.example.docs.datagen.FabricDocsReferenceDataGenerator"
],
"fabric-gametest": [
"com.example.docs.FabricDocsGameTest"
],
"fabric-client-gametest": [
"com.example.docs.FabricDocsClientGameTest"
]
},
"mixins": [
Expand Down
Loading