Jump to content

Unit testing in lua


Rick
 Share

Recommended Posts

Found a nice and easy lua unit testing framework and thought I'd share for anyone interested: https://github.com/bluebird75/luaunit

 

 

You don't hear much about unit testing here but it can really help you write solid code and gives you a way to run all your tests after you make changes to make sure you didn't break anything.

 

What I did:

 

1) Under the main game directory I created a UnitTesting folder

2) I downloaded and put lua5.1.exe in this folder (this is what LE uses)

3) Download the luaunit file (it's just one file) from the github link above

4) I Create test files in here named *.Tests.lua

5) Created a .bat file named RunAll.bat with the code below that will loop over all files with *.Tests.lua in them and run them piping the output to a file of the same name but with .txt at the end:

 

echo off

for %%f in (*.Tests.lua) do (
echo %%~f
lua5.1.exe %%~f >> %%~f.txt
)

 

 

I generally make "classes" like the AnimationManager.lua class and so I'm doing 1 lua test file per class that it's testing.

 

An example of my lua test file is:

 

 

luaunit = require('luaunit')
dofile("../Scripts/BattleLibrary/Actor.lua")

TestActor = {}

function TestActor:testStaminaZero()
local actor = Actor:Create(0, "Rick")

luaunit.assertEquals(actor.stamina, 0)
end

os.exit(luaunit.LuaUnit.run())

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...