functiontestHelloWorld() localret = testModule.helloWorld("world") luaUnit.assertEquals(type(ret), "string") luaUnit.assertEquals(ret, "Hello, world!") ret = testModule.helloWorld("ming") luaUnit.assertEquals(type(ret), "string") luaUnit.assertEquals(ret, "Hello, ming!") ret = testModule.helloWorld(integer) luaUnit.assertEquals(type(ret), "string") luaUnit.assertEquals(ret, "Hello, ming!") end os.exit(luaUnit.LuaUnit.run())
Run LuaUnit
1 2 3 4 5 6
~$ lua test_helloworld.lua -v Started on Mon Jul 8 15:06:09 2019 testHelloWorld ... Ok ========================================================= Ran 1 tests in 0.000 seconds, 1 success, 0 failures OK
localfunctionhelloWorld(name) -- assert(type(name) == "string", {message="name expects a string"}) iftype(name) ~= "string"then error({message="name expects a string"}) end
returnstring.format("Hello, %s!", name) end
functionmodule.HelloWorld(name) -- Error Handling -- Reference: https://blog.golang.org/error-handling-and-go local success, result = pcall(helloWorld, name) ifnot success then return"", result.message end
~$ lua test_helloworld.lua -v Started on Mon Jul 8 15:42:30 2019 testHelloWorld ... Ok testHelloWorldWrongCase ... Ok ========================================================= Ran 2 tests in 0.000 seconds, 2 successes, 0 failures OK