Testing
Gamma test runs every *.spec.luau and *.spec.gma file under a folder. Each file runs in a game of its own, and the Test global works like Jest.
Test.describe("inventory", function()
Test.test("adds an item", function()
local bag = Inventory.new()
bag:Add("sword")
Test.expect(bag:Count()).toBe(1)
Test.expect(bag:Items()).toContain("sword")
end)
Test.skip("drops items when full")
end)Gamma test # everything under the current folder
Gamma test Tests/Inventory.spec.luau
Gamma test --filter inventory --watchGamma prints PASS or FAIL per file with its counts, then totals, and exits with 1 if a test failed or a file errored outside a test. Hidden folders, build and node_modules are skipped. Gamma run on a single spec file runs its tests too.
Writing tests
Test.test(name, body)runs a test. It fails if the body errors, and the body may yield.Test.describe(name, body)groups tests and prefixes their names.Test.skip(name)counts a test without running it.
Tests run in the order they're declared.
Matchers
Test.expect(value) takes:
| Matcher | Passes when |
|---|---|
toBe(x) | value == x |
toEqual(x) | deeply equal |
toBeNil(), toBeTruthy(), toBeFalsy() | as named |
toBeCloseTo(n, digits = 2) | equal to digits decimal places |
toBeGreaterThan, toBeGreaterThanOrEqual, toBeLessThan, toBeLessThanOrEqual | as named |
toContain(x) | a substring, or a list item (compared deeply) |
toHaveLength(n) | #value == n |
toBeA(typeName) | typeof(value) == typeName |
toThrow(message?) | calling value errors, with that message if given |
.never flips any of them: Test.expect(x).never.toBe(nil). A failure logs an error with the test's name and the line of the check.
Hooks
| Hook | Runs |
|---|---|
Test.beforeEach(fn) | Before each test in the same describe (or file) and the ones nested in it. Outer hooks first |
Test.afterEach(fn) | After each test, even a failed one. Outer hooks last. An error in it fails the test |
Test.beforeAll(fn) | Once, straight away. If it errors, the tests after it in its describe fail without running |
Test.afterAll(fn) | Once, when its describe ends. At the top of a file, when the game closes after the last test |
A hook only applies to tests written after it.
In CI
--junit <File.xml> writes JUnit XML, which GitHub Actions, GitLab and Jenkins all read: one testsuite per spec file, one testcase per test with its time, and a failure per failed test. A file that failed outside its tests gets a failed case named (outside a test).
--filter <Text> runs only tests whose full name (with the describe names in front) contains the text, and exits with 1 if none do.
Screenshot tests
Gamma play MyGame --frames 120 --screenshot out.png --window hiddenDraws 120 frames, saves the last one and exits. It fails if anything logged an error or the file couldn't be written. --frames alone just stops after N frames, and --screenshot alone saves the first frame. --window hidden keeps a window from popping up.
Fake input
VirtualInput sends input that never happened, for testing controls: SendGamepadButtonEvent(gamepad, keyCode, isDown), SendGamepadAxisEvent(gamepad, keyCode, position), SendTouchEvent(touchId, state, x, y), and keyboard and mouse events (see VirtualInput).