-
Notifications
You must be signed in to change notification settings - Fork 139
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
Open
kevinthegreat1
wants to merge
8
commits into
FabricMC:main
Choose a base branch
from
kevinthegreat1:game-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ded2885
Add game test section
kevinthegreat1 48b737e
Add example game tests
kevinthegreat1 051edc3
Only add game test to latest
kevinthegreat1 2fcdcf6
Fix checkstyle
kevinthegreat1 b710824
Merge branch 'main' into game-test
its-miroma 2aae014
Separate gametest fmj
kevinthegreat1 f08c1cd
Add separate fmj info
kevinthegreat1 8431d7a
Forgot anchor
kevinthegreat1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||||||||||||||||||||||||
|
@@ -87,3 +83,43 @@ 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). | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#### Setting up Game Test Directory {#setting-up-game-test-directory} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
::: info | ||||||||||||||||||||||||||||||
You only need this section if you enabled `createSourceSet`, which is recommended. You can, of course, do your own gradle magic, but you'll be on your own. | ||||||||||||||||||||||||||||||
::: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
If you enabled `createSourceSet` like the example above, your gametest will be in a separate source set with a separate `fabric.mod.json`. The module name defaults to `gametest`. Create a new `fabric.mod.json` in `src/gametest/resources/` as shown: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
<<< @/reference/latest/src/gametest/resources/fabric.mod.json | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Note that this `fabric.mod.json` expects a server game test at `src/gametest/java/com/example/docs/FabricDocsGameTest`, and a client game test at `src/gametest/java/com/example/docs/FabricDocsClientGameTest`. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
### 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. | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||
Comment on lines
+117
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
See the respective javadocs in Fabric API for more info. | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
reference/latest/src/gametest/java/com/example/docs/FabricDocsClientGameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
reference/latest/src/gametest/java/com/example/docs/FabricDocsGameTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"id": "fabric-docs-reference-test", | ||
"version": "1.0.0", | ||
"name": "Fabric docs reference", | ||
"icon": "assets/fabric-docs-reference/icon.png", | ||
"environment": "*", | ||
"entrypoints": { | ||
"fabric-gametest": [ | ||
"com.example.docs.FabricDocsGameTest" | ||
], | ||
"fabric-client-gametest": [ | ||
"com.example.docs.FabricDocsClientGameTest" | ||
] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you change line 60 to: