Unit Testing game/lib

I'm new to unit testing and have some problems with it, can someone please help me to set up environment to test my lib?
@james

We use LuaUnit to test the SDK and firmware in the simulator and device. If you want to use it you can do the following:

IMPORTANT: we're using luaunit 3.2.1. YMMV with other version.

  1. copy the luaunit source into your source folder

  2. Add these patch files to the lauunit source folder.
    panic-luaunit.zip (22.2 KB)

  3. In your main.lua add the following to the top

import 'luaunit/playdate_luaunit_fix'
import 'luaunit/luaunit'
  1. Import your unit tests

  2. run luaunit with the following:

-- turns off updating
playdate.stop()

-- when outputting a table, include a table address
luaunit.PRINT_TABLE_REF_IN_ERROR_MSG = true

-- process the command line args (if any)
local testOutputFilename = "test_output"
local outputType = "text"
local luaunit_args = {'--output', 'text', '--verbose', '-r'}

-- run the tests
local returnValue = luaunit.LuaUnit.run(table.unpack(luaunit_args))

print("unit test return value = "..returnValue)

CAVEATS: I haven't tested these instructions directly. They're simply taken from what we're doing with our own code.

9 Likes

That thank you for the detailed answer!

Hi Whitebrim, did you get this working? I'm pretty new to lua (and the playdate itself!) but would like to warm embrace of unit tests to help play with stuff :smile:.

1 Like

You can find my code here: https://github.com/Whitebrim/AnimatedSprite/tree/master/tests%2Funit-tests%2Fsource

Ah nice!

I got something working after much mashing my head off the wall, but I like your solution for setup and teardown so might steal it (my over complex solution looks like this currently https://github.com/neil-armstrong-fig/playdate/blob/5e0d925426ce7ae7e67f179a9373330a95213818/src/actors/Player-Test.lua).

Will have a closer look at yours later, thanks!

1 Like

I can confirm that this works, which is pretty sweet. Does anyone know where we can request to the playdate devs that they make a headless simulator so we can run tests like this in CI?

1 Like

I have been using this as well and really appreciate it. I recently attempted to switch to version 3.4 because I'd like the option to skip tests, however my naive attempts lead to an immediate crash of the simulator. Anyone gotten a more recent version working?

I spoke too soon. I got suspicious that there were no crash logs anywhere, at which point I discovered this in playdate_luaunit_fix.lua:

function os.exit(value)
    if playdate.isSimulator then
	    playdate.simulator.exit(value)
    end
end

After commenting out the exit I was able to access the command output to discover that there's an error in the command line flags being passed to LuaUnit in main.lua. The -r (repeat) flag is provided, but there's no number of repetitions specified, and evidently in the new version that's enforced. Just add an additional argument to the list to specify a number (or remove the -r entirely) and it works like a charm:

local luaunit_args = {'--output', 'text', '--verbose', '-r', '3'}