But, there are a reality and is that Lua is there, behind many big systems, videogame engines and servers.
It is quite common to use Lua for add functionality to these systems. Perhaps one of the most known systems that uses Lua is Roblox. That videogame application that gather thousand of players and, in which, you can create your own 3D games and make some money on the process. But, as I said, as with Roblox, there are many other places in which Lua is required and having some knowledge around this language could be something to consider for any developer toolbox.
So, I will assume that you already have some basic knowledge of Lua, but if you don’t, no worries, I will use very simple example which any developer could understand. And, of course, the important part here is testing over Lua as a language.
Creating code to test
Clearly, something that we need is some functional code to test. For the sake of this post (Keep it simple) we will write a couple of basic functions:
function sum(a, b) return a + b end function substract(a, b) return a - b end
Now we have some code and the question that requires an answer is: How we test this?
The testing framework
It is clear that for unit test any piece of code you don’t need a framework. A framework could give you some benefits but not having one is not a blocker. You can still test without a testing framework. In any case, we will use one 🙂
We will use Lua Unit, which has a wide set of functions to help us during the testing process. So, lets create another file in which we will write our unit tests.
--import Lua Unit local luaUnit = require('./luaunit') --import math functions file require('./math')
Lua Unit can be fully downloaded in one single file and included into our testing file using require command. In the same way, we can import the functions to test from the file in which we wrote them.
Unit testing our code
Having imported the files and the framework, the next step will be to write the testing code. One good practice for unit testing, as in any other language, is to encapsulate any data initialization and repeated code in such way that we can reuse it, helping the functional test to focus on that action: testing.
function DoTest(valA, valB, expected) local result = sum(valA, valB) luaUnit.assertEquals(result, expected) end
In the previous code, the function DoTest parameterize the test process allowing any other test to consume its services and act in consequence.
function When_Sum10and10_Expect_20() DoTest(10, 10, 20) end
The testing function follows one of the many naming conventions (Which can be changed at will) and encapsulate what need to be tested. Depending of how we are use to do testing, we could expect to have the assert call inside of the test and not in the helper function.
Finally, we need to perform a call to this function and execute the test.
If all goes good and the tests pass, Lua Unit will do its work and no messages will be shown. In the other hand, we can let Lua Unit to show more detailed information about the execution adding the following at the end of our script.
os.exit( luaUnit.LuaUnit.run() )
But even without the previous line we will be getting error messages if any unit test fails, having an instant feedback of our code.