Page 1 of 1

lovetest - Easily add unit tests to your games

Posted: Fri Nov 01, 2013 10:10 pm
by conroy
Hey everyone, here today to show off my latest project, lovetest. It's a tool for testing LOVE games.

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
Writing Tests

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

Re: lovetest - Easily add unit tests to your games

Posted: Fri Nov 01, 2013 10:52 pm
by Robin
Great! What happens when you run the unit tests? If they all pass, the game starts, if not, you get an error screen? Or something else?

Re: lovetest - Easily add unit tests to your games

Posted: Fri Nov 01, 2013 11:37 pm
by conroy
When you run the unit tests, you'll get an output of all passing / failing tests, and then the game will quit.

Code: Select all

-- Starting suite "main", 81 test(s)
.......................................................................
..........
---- Testing finished in 91.07 ms, with 144 assertion(s) ----
We use the tests in conjunction with our build system. Every commit is tested using Travis CI. You can see an example build here. While you can include your tests in the final build of your game, I would suggest against it, as no user will be running those tests.