PackTest
PackTest allows you to write game tests in a data pack. Tests are *.mcfunction files in a test folder. They can be used to test custom data packs.
Example
data/example/test/foo.mcfunction
#> Summons an armor stand and finds it
# @template example:small_platform
# @optional
summon armor_stand ~1.5 ~1 ~1.5
execute positioned ~1.5 ~1 ~1.5 run assert entity @e[type=armor_stand,dx=0]
assert predicate example:test
setblock ~1 ~1 ~1 grass_block
execute if block ~1 ~1 ~1 stone run succeed
fail "Oh no"
Async tests
Test functions can be asynchronous, using the await keyword!
setblock ~ ~ ~ stone
summon item ~ ~6 ~
await entity @e[type=item,distance=..2]
await delay 1s
data merge entity @e[type=item,distance=..2,limit=1] {Motion: [0.0, 0.01, 0.0]}
Running tests
Tests can be run in-game using the test command.
test runall: runs all the teststest runall <namespace>: runs all tests from a specified namespacetest run <test>: runs the test with a specified nametest runfailed: runs all the previously failed teststest runthis: runs the closes testtest runthese: runs all tests within 200 blocks
Auto test server
Tests can also be run automatically, for instance in a CI environment. When -Dpacktest.auto is set, the game test server will start automatically with the loaded tests. The process will exit when all tests have finished with the exist code set to the number of failed tests.
Setting -Dpacktest.auto.annotations will emit GitHub annotations for all test failures and resource load errors.
The following example can be adapted into a GitHub action workflow.
on: [push, pull_request]
env:
# Make sure to update these links!
TEST_FABRIC_SERVER: https://meta.fabricmc.net/v2/versions/loader/1.20.4/0.15.3/0.11.2/server/jar
TEST_FABRIC_API: https://cdn.modrinth.com/data/P7dR8mSH/versions/JMCwDuki/fabric-api-0.92.0%2B1.20.4.jar
TEST_PACKTEST: https://cdn.modrinth.com/data/XsKUhp45/versions/18smpIeE/packtest-1.6-mc1.20.4.jar
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Download and prepare files
run: |
curl -o server.jar $TEST_FABRIC_SERVER
mkdir mods
curl -o mods/fabric-api.jar $TEST_FABRIC_API
curl -o mods/packtest.jar $TEST_PACKTEST
mkdir -p world/datapacks
cp -r datapack world/datapacks/datapack
- name: Run tests
run: |
java -Xmx2G -Dpacktest.auto -Dpacktest.auto.annotations -jar server.jar nogui
Commands
fail
fail <text component>: fails the current test and returns from the function
succeed
succeed: always succeeds the current test and returns from the function
assert
assert <condition>: if condition is unsuccessful, fails the current test and returns from the functionassert not <condition>: if condition is successful, fails the current test and returns from the function
await
await <condition>: similar to assert, but keeps trying the condition every tick until the test times our or the condition succeedsawait not <condition>: keeps trying the condition until it failsawait delay <time>: waits for a specified time with unit
Conditions
block <pos> <block>: checks if the block at the specified position matches the block predicatedata ...: checks NBT data using the same syntax asexecute if scoreentity <selector>: checks if the selector matches any entity (can also find entities outside the structure bounds)predicate <predicate>: checks a predicate in a data packscore ...: checks scores using the same syntax asexecute if scorechat <pattern> [<receivers>]: checks whether a chat message was sent in the past tick matching a regex pattern
Dummies
Fake players can be spawned using the /dummy command. Dummies won't save or load their data from disk, they will also not load their skin.
dummy <name> spawn: spawns a new dummydummy <name> respawn: respawns the dummy after it has been killeddummy <name> leave: makes the dummy leave the serverdummy <name> jump: makes the dummy jump, if currently on grounddummy <name> sneak [true|false]: makes the dummy hold shift or un-shift (not the same as currently crouching)dummy <name> sprint [true|false]: makes the dummy sprint or un-sprintdummy <name> drop [all]: makes the dummy drop the current mainhand, either one item or the entire stackdummy <name> swap: makes the dummy swap its mainhand and offhanddummy <name> selectslot: makes the dummy select a different hotbar slotdummy <name> use item: makes the dummy use its hand item, either mainhand or offhanddummy <name> use block <pos> [<direction>]: makes the dummy use its hand item on a block positiondummy <name> use entity <entity>: makes the dummy use its hand item on an entitydummy <name> attack <entity>: makes the dummy attack an entity with its mainhanddummy <name> mine <pos>: makes the dummy mine a block
Directives
Tests can be customized by placing certain directives as special comments at the start of the test function.
@template: the resource location of a structure template to use for the test, defaults to an empty 1x1x1 structure@timeout: an integer specifying the timeout, defaults to100@optional: whether this test is allowed to fail, defaults tofalse, if there is no value after the directive it is considered astrue@dummy: whether to spawn a dummy at the start of the test and set@sto this dummy, taking a position which defaults to~0.5 ~ ~0.5@batch: the batch name for this test, defaults topacktestBatch@beforebatch: a command to run before this batch, there can only be one per batch@afterbatch: a command to run after this batch, there can only be one per batch
All Versions
+ Add Version| Name | Game version | Platforms | Published | Downloads | Actions |
|---|---|---|---|---|---|
|
R
2.8.0
|
11d ago | 1 | |||
|
R
2.7.0
|
1mo ago | 1 | |||
| 2mo ago | 1 | ||||
| 2mo ago | 1 | ||||
| 2mo ago | 1 | ||||
| 5mo ago | 1 | ||||
| 7mo ago | 1 | ||||
| 9mo ago | 1 | ||||
| 9mo ago | 1 | ||||
| 11mo ago | 1 | ||||
| 11mo ago | 1 | ||||
| 11mo ago | 1 | ||||
| 11mo ago | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 | ||||
| last yr. | 1 |
/reloadnow works correctly with test functions- Added
/logcommand that logs a message to the console but not to chat - Added new directive
@dimensionspecifying where the test should run - Errors in pack.mcmeta files are now logged and fail the test server
- Updated to 26.3-rc-1
- Fixed issue [#17](https://github.com/misode/packtest/issues/17) where
any_block_useandplayer_interacted_with_entityadvancement triggers did not work for dummies - Fixed issue [#9](https://github.com/misode/packtest/issues/9) where the wrong failure message could be reported (Thanks [Bafy78](https://github.com/Bafy78))
- Updated to 26.2
- Updated to 26.1-snapshot-6
- Fixes a crash from
/assert items entity ...with invalid slots
- Updated to 1.21.11-rc2
- The
/dummycommand now requires permission level 2
- Updated to 1.21.10
- Updated to 1.21.6 and 1.21.7
- Updated to 1.21.6
- Fixed [#8](https://github.com/misode/packtest/issues/8): Rotate command does not rotate dummies
- Fixes [#14](https://github.com/misode/packtest/issues/14) where some tick inconsistencies were caused by the game test ticker being ticked twice per tick
- Updated to 1.21.5
- BREAKING: Removed
@batch,@beforebatch, and@afterbatchdirectives - Added
@environmentdirective to specify atest_environment
- Updated to 1.21.5-rc1
- BREAKING: Removed
@batch,@beforebatch, and@afterbatchdirectives - Added
@environmentdirective to specify atest_environment - Known issue:
/reloadis completely broken
- Updated to 1.21.4-rc1
- Updated to 1.21.2
- Updated to 1.21.2-pre3
- Add
-Dpacktest.generate.commandsoption to generate a commands.json report - Fix error reporting when annotations are enabled and the error message contains
{} - Prevent dummies from getting the "Bring Home the Beacon"
- Only run tests inside the world border when auto running
- Fix crash caused by loot load errors
- Updated to 1.21
- Renamed
testsfolder totest
- Updated to 1.20.5
- Added
/assert items /assert predicatenow also allows inline predicates- Test structures are now encased with barrier blocks
- Added the
@skyaccessdirectives to remove the barriers at the top
- Update to 1.20.5-pre1
- Added
/assert items
- Update to 24w12a
/assert predicatenow also allows inline predicates
- Updated to 24w09a
- Test structures are encased with barrier blocks
- Added
@skyaccessdirectives to remove the barriers at the top
- Added line numbers to test failure messages
- Errors when loading resources now fails the auto test server
- Added an option to emit GitHub annotations with the auto test server using
-Dpacktest.auto.annotations
- Added
assert entity <selector> insidecommand, which only matches entities inside the test structure bounds (inflated by 1 block) - Dummies are now automatically removed when a test succeeds, like other entities
- Fixed issue where forceloaded chunks would get removed after running tests
- Fixes an issue where modded clients without the mod could not connect to servers with the mod
- Fixes some placeholder values in the mod metadata
- Fixes an issue causing the test server to fail
- Added
/awaitcommand, which works similar to/assertbut keeps testing the condition every tick - Removed
/succeed when, instead use/await - Added
/await delaycommand to sleep for a specified time - Added
/assert chatcommand to test for tellraw messages matching a regex pattern - Fixed issue with dummies loading data from disk from previous sessions