有没有办法测试 roblox 游戏?

Is there a way to test roblox games?

随着我开始对 Roblox 有了更多的了解,我想知道是否有任何可能的方法来自动化测试。仅作为 Lua 脚本编写的第一步,但最好也模拟游戏和交互。

有什么办法可以做到这一点吗? 此外,如果已经有关于在 Roblox 上进行测试的最佳实践(这包括 Lua 脚本),我想了解更多关于它们的信息。

单元测试

对于 lua 个模块,我会推荐库 TestEZ。它由 Roblox 工程师内部开发,以允许进行行为驱动测试。它允许您指定测试文件所在的位置,并会为您提供有关测试如何进行的非常详细的输出。

此示例将 运行 在 RobloxStudio 中,但您可以将它与其他库配对,例如 Lemur 用于命令行和持续集成工作流程。无论如何,请按照以下步骤操作:

1。将 TestEZ 库放入 Roblox Studio

  1. 下载Rojo。该程序允许您将项目目录转换为 .rbxm(Roblox 模型对象)文件。
  2. 下载 TestEZ source code.
  3. 打开 Powershell 或终端 window 并导航到下载的 TestEZ 目录。
  4. 使用此命令构建 TestEZ 库 rojo build --output TestEZ.rbxm .
  5. 确保它在该目录中生成了一个名为 TestEZ.rbxm 的新文件。
  6. 打开RobloxStudio到你的位置。
  7. 将新创建的 TestEZ.rbxm 文件拖到世界中。它会将库解压为同名的 ModuleScript。
  8. 将此 ModuleScript 移动到 ReplicatedStorage 之类的地方。

2。创建单元测试

在这一步中,我们需要创建名称以 .spec 结尾的 ModuleScripts 并为我们的源代码编写测试。

结构化代码的一种常见方法是使用 ModuleScripts 中的代码 classes 及其旁边的测试。因此,假设您在名为 MathUtil

的 ModuleScript 中有一个简单的实用程序 class
local MathUtil = {}

function MathUtil.add(a, b)
    assert(type(a) == "number")
    assert(type(b) == "number")
    return a + b
end

return MathUtil

要为此文件创建测试,请在其旁边创建一个 ModuleScript 并将其命名为 MathUtil.spec这个命名约定很重要,因为它允许 TestEZ 发现测试。

return function()
    local MathUtil = require(script.parent.MathUtil)
    
    describe("add", function()
        it("should verify input", function()
            expect(function()
                local result = MathUtil.add("1", 2)
            end).to.throw()
        end)
        
        it("should properly add positive numbers", function()
            local result = MathUtil.add(1, 2)
            expect(result).to.equal(3)
        end)
        
        it("should properly add negative numbers", function()
            local result = MathUtil.add(-1, -2)
            expect(result).to.equal(-3)
        end)
    end)
end

有关使用 TestEZ 编写测试的完整细目,请查看 the official documentation

3。创建测试 运行ner

在这一步中,我们需要告诉 TestEZ 在哪里可以找到我们的测试。所以在 ServerScriptService 中创建一个脚本:

local TestEZ = require(game.ReplicatedStorage.TestEZ)

-- add any other root directory folders here that might have tests 
local testLocations = {
    game.ServerStorage,
}
local reporter = TestEZ.TextReporter
--local reporter = TestEZ.TextReporterQuiet -- use this one if you only want to see failing tests
 
TestEZ.TestBootstrap:run(testLocations, reporter)

4。 运行 你的测试

现在我们可以 运行 游戏并检查输出 window。我们应该看到我们的测试输出:
Test results:
[+] ServerStorage
   [+] MathUtil
      [+] add
         [+] should properly add negative numbers
         [+] should properly add positive numbers
         [+] should verify input
3 passed, 0 failed, 0 skipped - TextReporter:87

自动化测试

遗憾的是,没有一种方法可以完全自动化地测试您的游戏。

您可以使用 TestService 创建自动测试 某些 交互的测试,例如玩家触摸杀戮块或检查枪支的子弹路径。但是没有公开的方式来启动游戏、记录输入和验证游戏状态。

an internal service for this, and a non-scriptable service for mocking inputs 但不覆盖 CoreScripts,目前确实不可能。