Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Lua and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Lua:

-- a minimal test harness using assert (no framework needed)
local function add(a, b) return a + b end

local tests = {
  ["adds positives"] = function() assert(add(2, 3) == 5) end,
  ["adds negatives"] = function() assert(add(-1, -1) == -2) end,
}
for name, fn in pairs(tests) do
  local ok, err = pcall(fn)
  print((ok and "PASS  " or "FAIL  ") .. name .. (ok and "" or ": " .. err))
end

Lua note: Core Lua ships no test framework, but assert plus pcall is enough to roll a runner in a few lines (the pattern behind busted/luaunit), since assert raises on a falsy first argument and passes its message through.

Try it yourself

Exercise: In Lua, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Lua. Mark this lesson complete and continue to the next one.