是否存在测试roblox游戏的方法?



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

有办法做这样的事情吗?此外,如果已经有在Roblox上进行测试的最佳实践(包括Lua脚本),我想知道更多关于它们的信息。

单元测试

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

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

1。将TestEZ库导入Roblox Studio

  1. 下载红色的。这个程序允许你将项目目录转换成。rbxm (Roblox模型对象)文件。
  2. 下载TestEZ源代码。
  3. 打开Powershell或Terminal窗口,导航到下载的TestEZ目录。
  4. rojo build --output TestEZ.rbxm .
  5. 命令构建testz库
  6. 确保在该目录下生成一个名为TestEZ.rbxm的新文件。
  7. 打开RobloxStudio到你的位置。
  8. 将新建的TestEZ.rbxm文件拖到世界中。它会将库解包成一个同名的ModuleScript。
  9. 把这个ModuleScript移到某个地方,比如ReplicatedStorage

2。创建单元测试

在这一步中,我们需要创建名称以'结尾的modulesscripts。为我们的源代码指定并编写测试。构建代码的一种常见方法是将代码类放在ModuleScripts中,并将它们的测试放在它们旁边。假设你在ModuleScript中有一个简单的实用程序类MathUtil

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

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

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编写测试的详细说明,请查看官方文档。

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)

4。运行测试

现在我们可以运行游戏并检查Output窗口。我们应该看到测试的输出:
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来创建测试,自动测试一些交互,比如玩家触碰击杀块或检查枪的子弹路径。但是没有一种公开的方式来开始你的游戏,记录输入,并验证游戏状态。

有一个内部服务,和一个非脚本化的服务来模拟输入,但没有覆盖coresscripts,这在目前是不可能的。

最新更新