有没有办法测试 roblox 游戏?

xal*_*ves 2 testing lua automation integration-testing roblox

当我开始对 Roblox 有更多了解时,我想知道是否有任何可能的方法来自动化测试。仅作为 Lua 脚本编写的第一步,但理想情况下还可以模拟游戏和交互。

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

Kyl*_*aaa 5

单元测试

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

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

1.将TestEZ库导入Roblox Studio

  1. 下载罗霍。该程序允许您将项目目录转换为 .rbxm(Roblox 模型对象)文件。
  2. 下载TestEZ 源代码
  3. 打开 Powershell 或终端窗口并导航到下载的 TestEZ 目录。
  4. 使用此命令构建 TestEZ 库 rojo build --output TestEZ.rbxm .
  5. 确保它生成了一个TestEZ.rbxm在该目录中调用的新文件。
  6. 打开 RobloxStudio 到您的位置。
  7. 将新创建的TestEZ.rbxm文件拖到世界中。它将把库解压到同名的 ModuleScript 中。
  8. 将此 ModuleScript 移至 ReplicatedStorage 等位置。

2. 创建单元测试

在此步骤中,我们需要创建名称以“.spec”结尾的 ModuleScript,并为源代码编写测试。

构建代码的常见方法是在 ModuleScripts 中使用代码类,并在它们旁边放置它们的测试。假设您在 ModuleScript 中有一个简单的实用程序类,名为MathUtil

local MathUtil = {}

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

return MathUtil
Run Code Online (Sandbox Code Playgroud)

要为此文件创建测试,请在它旁边创建一个 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
Run Code Online (Sandbox Code Playgroud)

有关使用 TestEZ 编写测试的完整详细信息,请查看官方文档

3. 创建测试运行器

在这一步中,我们需要告诉 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)
Run Code Online (Sandbox Code Playgroud)

4. 运行测试

现在我们可以运行游戏并检查输出窗口。我们应该看到我们的测试输出:
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
Run Code Online (Sandbox Code Playgroud)

自动化测试

不幸的是,不存在完全自动化游戏测试的方法。

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

一个用于此目的的内部服​​务,以及一个用于模拟输入的不可编写脚本的服务,但如果不覆盖 CoreScripts,目前确实不可能。