How can I improve this unit test module I wrote?
I wrote this unit test so it look like javascript mocha testing module but in a easier way to use. Here is it’s code:
local module = {}
function module.isEqual<T>(value: T, expected: T)
return value == expected
end
function module.isNotEqual<T>(value: T, expected: T)
return value ~= expected
end
function module.doesNotThrow(func, ...)
local args = {...}
local succ = pcall(func, table.unpack(args))
return succ
end
function module.doesThrow(value, ...)
local args = {...}
local succ = pcall(value, table.unpack(args))
return not succ
end
function module.describe(idx, desc)
warn(idx .. ")", desc)
end
--- @Param {string} condition Something to return false to then make an error
function module.it(condition, stepName)
warn("\t> ".. stepName)
local errorMsg = "\t\tfailed the test `%s' ❌"
local succMsg = "\t\tPassed the test `%s' ✅"
if not condition then
print(errorMsg:format(stepName), "\n\t\t" .. debug.traceback(2))
else
print(succMsg:format((stepName)))
end
end
return module
Here is how one would use it:
You create a script where you enter your test like this:
local gameLogicRoot = script.Parent.Parent
local gameLogicApi = require(gameLogicRoot.gameLogicApi)
local modules = game.ReplicatedStorage.modules
local TestingLib = require(modules.TestFramework)
local RUN_TEST = true
if not RUN_TEST then
return
end
TestingLib.describe(1, "brick handling functions")
local normalBrick = gameLogicApi.createBrick(BrickColor.White(), BrickColor.Gray(),
"normal"
)
local wideBrick = gameLogicApi.createBrick(BrickColor.White(), BrickColor.Gray(),
"wide"
)
TestingLib.it(normalBrick.brickType == "normal", "normalBrick type should be normal")
TestingLib.it(TestingLib.isEqual(normalBrick:getPrimaryColor(), BrickColor.White()), "Primary color of normalBrick should be white")
TestingLib.it((function ()
local brickColor = BrickColor.new(Color3.fromHSV(0.0693333, 1, 1))
normalBrick:setSecondaryColor(brickColor)
return TestingLib.isEqual(normalBrick:getSecondaryColor(), brickColor)
end)(), "change normalBrickColor secondary brickColor. Verifying the info by using getSecondaryColor")
TestingLib.it(TestingLib.doesNotThrow(normalBrick.setBrickTransparency, normalBrick, 0.5),
"should be able to change the opacity without getting any error"
)
TestingLib.it(TestingLib.doesNotThrow(gameLogicApi.deplaceRaquette, 12), "c'est supposer de bouger la raquette sans érreur")
TestingLib.it(TestingLib.doesThrow(function ()
normalBrick.unexpected_property = "69 xd"
end), "it should throw a error when adding an unknown property")
Here is how it output:
1) brick handling functions - TestFramework:23
> normalBrick type should be normal - TestFramework:28
Passed the test `normalBrick type should be normal' ✅ - TestFramework:35
> Primary color of normalBrick should be white - TestFramework:28
Passed the test `Primary color of normalBrick should be white' ✅ - TestFramework:35
> change normalBrickColor secondary brickColor. Verifying the info by using getSecondaryColor - TestFramework:28
Passed the test `change normalBrickColor secondary brickColor. Verifying the info by using getSecondaryColor' ✅ - TestFramework:35
> should be able to change the opacity without getting any error - TestFramework:28
Passed the test `should be able to change the opacity without getting any error' ✅ - TestFramework:35
> c'est supposer de bouger la raquette sans érreur - TestFramework:28
Passed the test `c'est supposer de bouger la raquette sans érreur' ✅ - TestFramework:35
> it should throw a error when adding an unknown property - TestFramework:28
Passed the test `it should throw a error when adding an unknown property' ✅ - TestFramework:35