Why Test
Catch bugs before users do.
Why Test is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test at All
Automated tests are code that checks your code. Instead of manually running a program and eyeballing the output, you write small assertions that the test runner verifies for you.
For Lua, the most popular framework is busted. It gives you a clean syntax for describing behavior and reporting pass or fail results.
The Cost of No Tests
Without tests, every change is a gamble. You fix one bug and silently break another. As a codebase grows, manual checking becomes impossible to do thoroughly.
Tests act as a safety net. When you refactor, a green test suite tells you the behavior is preserved.
A First Test File
A busted test file usually lives in a spec/ folder and ends in _spec.lua. Inside, you describe what you are testing.
Here is the smallest useful example. It checks that simple arithmetic works.
describe("math", function()
it("adds numbers", function()
assert.are.equal(4, 2 + 2)
end)
end)Running busted
You install busted with LuaRocks: luarocks install busted. Then run the busted command in your project root.
It discovers files ending in _spec.lua, runs every it block, and prints a dot for each pass and an F for each failure.
Testing Your Own Module
Real tests import the module under test with require. You call its functions and assert on the result.
Suppose you have a calculator.lua module that returns a table of functions.
local calc = require("calculator")
describe("calculator", function()
it("multiplies", function()
assert.are.equal(6, calc.mul(2, 3))
end)
end)Tests as Documentation
Well written tests double as living documentation. A new developer can read the spec file and understand exactly how a function is meant to behave.
Because the descriptions are plain strings, they read almost like sentences: "calculator multiplies".
describe("string utils", function()
it("trims whitespace", function()
assert.are.equal("hi", utils.trim(" hi "))
end)
end)Fast Feedback Loops
The real power of tests is speed. Running a full suite takes seconds, far faster than manually clicking through an app.
You can even run busted in watch mode or target a single file to keep feedback tight while developing.
-- run only one file
-- busted spec/calculator_spec.luaCatching Regressions
A regression is a bug that reappears in code that used to work. When you find a bug, the best practice is to write a test that reproduces it first.
Once the test fails, you fix the code until it passes. That test now guards against the bug returning.
it("handles empty input", function()
assert.are.equal(0, calc.sum({}))
end)What Makes a Good Test
Good tests are fast, isolated, and deterministic. They should not depend on network, time, or random values unless those are controlled.
Each test should check one clear behavior. If a test fails, the name should tell you what broke without reading the body.
Test Pyramid
The test pyramid suggests many small unit tests, fewer integration tests, and very few end to end tests. Unit tests are cheap and fast, so favor them.
busted is primarily a unit testing tool, though you can use it for integration tests too.
Confidence to Change
The deepest benefit of testing is confidence. With a trustworthy suite, you can refactor aggressively, upgrade dependencies, and ship faster.
This course teaches busted from the ground up: structure, assertions, and test doubles like mocks and spies.
describe("feature", function()
it("still works after refactor", function()
assert.is_true(feature.enabled())
end)
end)Quick Check
Test your understanding of why we write tests.
Recap
Tests are code that verifies code. busted is Lua's leading framework, discovering _spec.lua files and reporting pass or fail.
Good tests are fast, isolated, and deterministic. They catch regressions, serve as documentation, and give you confidence to change code freely.
Frequently asked questions
Is the “Why Test” lesson free?
Yes — the full text of “Why Test” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Why Test”?
Catch bugs before users do. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Why Test” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.