PackTest
PackTest 允许你在数据包中编写游戏测试。测试是位于 test 文件夹中的 *.mcfunction 文件。它们可用于测试自定义数据包。
示例
data/example/test/foo.mcfunction
#> 召唤一个盔甲架并找到它
# @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"
异步测试
测试函数可以是异步的,使用 await 关键字!
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]}
运行测试
可以使用 test 命令在游戏内运行测试。
test runall:运行所有测试test runall <namespace>:运行指定命名空间的所有测试test run <test>:运行指定名称的测试test runfailed:运行所有之前失败的测试test runthis:运行最近的测试test runthese:运行 200 格内的所有测试
自动测试服务器
测试也可以自动运行,例如在 CI 环境中。当设置 -Dpacktest.auto 时,游戏测试服务器将自动启动并加载已加载的测试。当所有测试完成后,进程将退出,退出代码设置为失败测试的数量。
设置 -Dpacktest.auto.annotations 将为所有测试失败和资源加载错误生成 GitHub 注释。
以下示例可以调整为 GitHub Action 工作流。
on: [push, pull_request]
env:
# 确保更新这些链接!
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
命令
fail
fail <text component>:使当前测试失败并从函数返回
succeed
succeed:始终使当前测试成功并从函数返回
assert
assert <condition>:如果条件不成功,则使当前测试失败并从函数返回assert not <condition>:如果条件成功,则使当前测试失败并从函数返回
await
await <condition>:类似于 assert,但会每刻尝试该条件,直到测试超时或条件成功await not <condition>:持续尝试该条件直到其失败await delay <time>:等待指定的时间(带单位)
条件
block <pos> <block>:检查指定位置的方块是否与方块谓词匹配data ...:使用与execute if score相同的语法检查 NBT 数据entity <selector>:检查选择器是否匹配任何实体(也可以查找结构边界之外的实体)predicate <predicate>:检查数据包中的谓词score ...:使用与execute if score相同的语法检查分数chat <pattern> [<receivers>]:检查过去一刻是否发送了匹配正则表达式模式的聊天消息
虚拟玩家
可以使用 /dummy 命令生成虚拟玩家。虚拟玩家不会从磁盘保存或加载其数据,也不会加载其皮肤。
dummy <name> spawn:生成一个新的虚拟玩家dummy <name> respawn:在虚拟玩家被杀死后重新生成它dummy <name> leave:使虚拟玩家离开服务器dummy <name> jump:如果虚拟玩家当前在地面上,使其跳跃dummy <name> sneak [true|false]:使虚拟玩家按住 Shift 键或松开 Shift 键(与当前蹲下状态不同)dummy <name> sprint [true|false]:使虚拟玩家奔跑或不奔跑dummy <name> drop [all]:使虚拟玩家掉落当前主手物品,要么是一个物品,要么是整个堆叠dummy <name> swap:使虚拟玩家交换其主手和副手物品dummy <name> selectslot:使虚拟玩家选择不同的快捷栏槽位dummy <name> use item:使虚拟玩家使用其手持物品,无论是主手还是副手dummy <name> use block <pos> [<direction>]:使虚拟玩家在方块位置使用其手持物品dummy <name> use entity <entity>:使虚拟玩家在实体上使用其手持物品dummy <name> attack <entity>:使虚拟玩家使用其主手攻击实体dummy <name> mine <pos>:使虚拟玩家挖掘方块
指令
可以通过在测试函数的开头放置特定的指令作为特殊注释来自定义测试。
@template:用于测试的结构模板的资源位置,默认为空的 1x1x1 结构@timeout:指定超时的整数,默认为100@optional:是否允许此测试失败,默认为false;如果指令后没有值,则视为true@dummy:是否在测试开始时生成一个假实体并将@s设置为该假实体,接受一个位置参数,默认为~0.5 ~ ~0.5@batch:此测试的批次名称,默认为packtestBatch@beforebatch:在此批次之前运行的命令,每个批次只能有一个@afterbatch:在此批次之后运行的命令,每个批次只能有一个
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
下载 PackTest
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PackTest
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PackTest
下载 PackTest
下载 PackTest
下载 PackTest
下载 PackTest
下载 PackTest
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 PackTest
下载 PackTest
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
/reload现在能正确处理测试函数- 新增
/log命令,可将消息记录到控制台但不显示在聊天栏中 - 新增
@dimension指令,用于指定测试运行的维度 pack.mcmeta文件中的错误现在会被记录并导致测试服务器启动失败
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
模组
查看物品与配方
模组
原版世界生成优化模组
模组
在游戏画面角落显示附近世界地形、玩家、生物和实体的地图。可创建路径点,帮助你找到标记过的位置。
模组
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
模组
缓冲的原始输入和线程化的事件轮询
评论 0
登录后参与评分
还没有评论,来抢沙发吧