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
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 tests
test runall <namespace>: runs all tests from a specified namespace
test run <test>: runs the test with a specified name
test runfailed: runs all the previously failed tests
test runthis: runs the closes test
test 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 function
assert 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 succeeds
await not <condition>: keeps trying the condition until it fails
await delay <time>: waits for a specified time with unit
Conditions
block <pos> <block>: checks if the block at the specified position matches the block predicate
data ...: checks NBT data using the same syntax as execute if score
entity <selector>: checks if the selector matches any entity (can also find entities outside the structure bounds)
predicate <predicate>: checks a predicate in a data pack
score ...: checks scores using the same syntax as execute if score
chat <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 dummy
dummy <name> respawn: respawns the dummy after it has been killed
dummy <name> leave: makes the dummy leave the server
dummy <name> jump: makes the dummy jump, if currently on ground
dummy <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-sprint
dummy <name> drop [all]: makes the dummy drop the current mainhand, either one item or the entire stack
dummy <name> swap: makes the dummy swap its mainhand and offhand
dummy <name> selectslot: makes the dummy select a different hotbar slot
dummy <name> use item: makes the dummy use its hand item, either mainhand or offhand
dummy <name> use block <pos> [<direction>]: makes the dummy use its hand item on a block position
dummy <name> use entity <entity>: makes the dummy use its hand item on an entity
dummy <name> attack <entity>: makes the dummy attack an entity with its mainhand
dummy <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 to 100
@optional: whether this test is allowed to fail, defaults to false, if there is no value after the directive it is considered as true
@dummy: whether to spawn a dummy at the start of the test and set @s to this dummy, taking a position which defaults to ~0.5 ~ ~0.5
@batch: the batch name for this test, defaults to packtestBatch
@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
评论 0
登录后参与评分
还没有评论,来抢沙发吧