0Pricing
Lua Academy · Lesson

Assertions

Express expected behavior.

Assertions is a free Lua Academy lesson on CoddyKit — lesson 3 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.

What Assertions Do

An assertion is the heart of a test. It states a fact that must be true. If it is, the test continues. If not, busted records a failure with a helpful message.

busted uses the luassert library, accessed through the global assert table.

assert.are.equal(4, 2 + 2)

are.equal

assert.are.equal checks equality with the == operator. The first argument is the expected value, the second is the actual value.

This works for numbers, strings, and booleans, but for tables it compares identity, not contents.

assert.are.equal("hello", greet())
assert.are.equal(10, count)

are.same for Tables

To compare table contents deeply, use assert.are.same. It recursively checks that two tables hold equal values.

Use same for data structures and equal for primitives or identity checks.

assert.are.same({1, 2, 3}, split("1,2,3"))
assert.are.same({x = 1}, point)

Truthiness

assert.is_true and assert.is_false check for the exact boolean values. assert.is_truthy accepts anything that is not nil or false.

Choose the strict variant when you specifically expect a boolean result.

assert.is_true(user.active)
assert.is_truthy(find("x"))
assert.is_false(list:isEmpty())

Nil Checks

assert.is_nil verifies a value is nil, and assert.is_not_nil verifies it is present. These are common when checking lookups.

They make intent clearer than comparing against nil with equal.

assert.is_nil(map["missing"])
assert.is_not_nil(map["found"])

Negating with is_not

Most assertions can be negated by inserting is_not or using the are_not form. This expresses that something must differ.

For example, assert.are_not.equal passes when the two values are different.

assert.are_not.equal(1, 2)
assert.is_not_nil(result)

Testing Errors

To assert that code raises an error, wrap it in a function and pass it to assert.has_error. busted calls the function and confirms it threw.

You can also check the error message by passing an expected string as a second argument.

assert.has_error(function()
  withdraw(-5)
end, "amount must be positive")

has_no.errors

The opposite check is assert.has_no.errors. It passes when the wrapped function runs without raising.

This is useful to confirm a happy path call does not crash, even when you do not assert on its return value.

assert.has_no.errors(function()
  parse("valid input")
end)

Custom Failure Messages

You can attach a message to most assertions to clarify failures. Pass it as an extra argument so the report explains the expectation.

Clear messages turn a cryptic failure into an actionable one.

assert.is_true(ok, "login should succeed")

Matching with Modifiers

luassert provides modifiers and matchers for richer checks. For example, assert.matches tests a string against a Lua pattern.

This is handy when an exact equality check is too strict for variable output.

assert.matches("%d%d%d%d", get_year())
assert.matches("error", log_line, 1, true)

Choosing the Right Assertion

Pick the most specific assertion for clarity. Use same for tables, equal for primitives, has_error for failures, and truthiness checks for booleans.

Specific assertions produce better failure messages, which speeds up debugging.

describe("queue", function()
  it("dequeues in order", function()
    assert.are.same({"a", "b"}, q:drain())
  end)
end)

Quick Check

Test your understanding of busted assertions.

Recap

luassert powers busted's assert table. Use are.equal for primitives, are.same for deep table comparison, and truthiness or nil checks where appropriate.

Test failures with has_error and successes with has_no.errors. Negate with is_not, and add messages for clearer reports.

Frequently asked questions

Is the “Assertions” lesson free?

Yes — the full text of “Assertions” 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 “Assertions”?

Express expected behavior. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Assertions” 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.

All lessons in this course

  1. Why Test
  2. describe and it Blocks
  3. Assertions
  4. Mocks and Spies
← Back to Lua Academy