Install Lua and Busted on Mac

18 May 2020

Install Lua with asdf

asdf plugin-add lua
asdf list all lua
asdf install lua 5.3.5

Install Lua package manager - luarocks

brew update
brew install luarocks

Install the Lua test framework - Busted

luarocks install busted
echo 'export PATH="/Users/username/.asdf/installs/lua/5.3.5/luarocks/bin:$PATH"'' > ~/.zshrc

Try busted

Create a test.lua file

describe("Busted unit testing framework", function()
  describe("should be awesome", function()
    it("should be easy to use", function()
      assert.truthy("Yup.")
    end)

    it("should have lots of features", function()
      -- deep check comparisons!
      assert.are.same({ table = "great"}, { table = "great" })

      -- or check by reference!
      assert.are_not.equal({ table = "great"}, { table = "great"})

      assert.truthy("this is a string") -- truthy: not false or nil

      assert.True(1 == 1)
      assert.is_true(1 == 1)

      assert.falsy(nil)
      assert.has_error(function() error("Wat") end, "Wat")
    end)

    it("should provide some shortcuts to common functions", function()
      assert.are.unique(\{\{ thing = 1 \}, \{ thing = 2 \}, \{ thing = 3 \}\})
    end)

    it("should have mocks and spies for functional tests", function()
      local thing = require("thing_module")
      spy.on(thing, "greet")
      thing.greet("Hi!")

      assert.spy(thing.greet).was.called()
      assert.spy(thing.greet).was.called_with("Hi!")
    end)
  end)
end)

Run test:

busted test.lua

#=> 4 successes / 0 failures / 0 errors / 0 pending : 0.01685 seconds

Bingo!

References

Back to top