xal*_*ves 2 testing lua automation integration-testing roblox
当我开始对 Roblox 有更多了解时,我想知道是否有任何可能的方法来自动化测试。仅作为 Lua 脚本编写的第一步,但理想情况下还可以模拟游戏和交互。
有什么方法可以做这样的事情吗?另外,如果已经有在 Roblox 上进行测试的最佳实践(包括 Lua 脚本),我想了解更多有关它们的信息。
对于 lua 模块,我推荐库TestEZ。它由 Roblox 工程师内部开发,可进行行为驱动测试。它允许您指定测试文件存在的位置,并将为您提供有关测试如何进行的非常详细的输出。
此示例将在 RobloxStudio 中运行,但您可以将其与Lemur等其他库配对等其他库配对,以实现命令行和持续集成工作流程。无论如何,请按照以下步骤操作:
rojo build --output TestEZ.rbxm .TestEZ.rbxm在该目录中调用的新文件。TestEZ.rbxm文件拖到世界中。它将把库解压到同名的 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 编写测试的完整详细信息,请查看官方文档。
在这一步中,我们需要告诉 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)
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,目前确实不可能。
| 归档时间: |
|
| 查看次数: |
1641 次 |
| 最近记录: |