I've been working on Journey to the Center of Hawkthorne for over a year and a half, and thought it was time to starting sharing some of the code that we use day to day. With such a large project, testing becomes very important. I first started using busted and luarocks for my testing needs, but integrating them with LOVE was a pain. Also, most developers didn't have lua or luarocks installed on their system.
We switched over to using lunatest and have been very happy. However, we needed a way to run our tests using LOVE. I've taken the code that does this out of our main repository and put it up on GitHub.
If you have any questions about testing or how we do testing, I'd be more than happy to answer any of your questions. Stay tuned in the coming weeks, as I'm going to be writing about the process we use to develop and release the game (hint: it involves a lot of automation).
Features
- Simple setup with zero dependencies
- Run tests via the command line
- Create tests quickly
In `main.lua`, add the following lines to your `love.load` function. Add the
function if you don't have it already.
Code: Select all
local lovetest = require "test/lovetest"
function love.load(arg)
-- Check for the testing command line flags
if lovetest.detect(arg) then
-- Run the tests
lovetest.run()
end
end
Using the command line, you can now run your unit tests
Code: Select all
love path/to/game --test
Tests are just lua scripts stored in the `test` directory. Their filename must
begin with `test_`. Each unit test function's name must also begin with `test_`.
Example Test
We're going to test that addition works (dumb, I know). Create a file inside
the `test` directory named `test_math.lua` with the following contents.
Code: Select all
function test_addition()
assert_equal(2 + 2, 4)
end
function test_subtraction()
assert_equal(2 - 2, 0)
end