您的位置:首页 > 编程语言 > Lua

luaunit的使用

2015-10-19 17:28 357 查看

github的地址:

https://github.com/bluebird75/luaunit

使用说明文档:

http://luaunit.readthedocs.org/en/latest/

使用方法:

测试命令:lua.exe <测试用例文件:testcase.lua> -v(显示每一项测试结果)

UT目标文件:testlib.lua

function add(v1,v2)
-- add positive numbers
-- return 0 if any of the numbers are 0
-- error if any of the two numbers are negative
if v1 < 0 or v2 < 0 then
error('Can only add positive or null numbers, received '..v1..' and '..v2)
end
if v1 == 0 or v2 == 0 then
return 0
end
return v1+v2
end


测试用例文件(UT执行文件):testcase.lua

EXPORT_ASSERT_TO_GLOBALS = true
package.path=[[D:\3SG\Test\luaunit-master\?.lua;E:\learnSkill\lua\lua_test\?.lua;]]  ..  package.path
luaunit = require('luaunit')
--引入测试框架luaunit

--[[
UT
--]]
unit = require('testlib')
--包含所要测试的函数库(测试目标)

--以下按照table分类测试用例(分组TestAdd与TestDiv)
TestAdd = {}
function TestAdd:testAddPositive()
local args = {1,1}
luaunit.assertEquals(add(1,1),2)
end

function TestAdd:testAddZero()
luaunit.assertEquals(add(1,0),0)
luaunit.assertEquals(add(0,5),0)
luaunit.assertEquals(add(0,0),0)
end

function TestAdd:testAddError()
luaunit.assertErrorMsgContains('Can only add positive or null numbers, received 2 and -3', add, 2, -3)
end

function TestAdd:testAdder()
f = adder(3)
luaunit.assertIsFunction( f )
luaunit.assertEquals( f(2), 5 )
end
-- end of table TestAdd

TestDiv = {}
function TestDiv:testDivPositive()
luaunit.assertEquals(div(4,2),2)
end

function TestDiv:testDivZero()
luaunit.assertEquals(div(4,0),0)
luaunit.assertEquals(div(0,5),0)
luaunit.assertEquals(div(0,0),0)
end

function TestDiv:testDivError()
luaunit.assertErrorMsgContains('Can only divide positive or null numbers, received 2 and -3', div, 2, -3)
end
-- end of table TestDiv

--以下为用于每个测试用例执行前后的资源初始化setUp和释放tearDown
TestLogger = {}
function TestLogger:setUp()
-- define the fname to use for logging
self.fname = 'mytmplog.log'
-- make sure the file does not already exists
os.remove(self.fname)
end

function TestLogger:tearDown()
-- cleanup our log file after all tests
os.remove(self.fname)
end
-- end of table TestLogger
--[[
UT
--]]

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


断言列表

Equality assertions

assertEquals(actual, expected)

assertNotEquals(actual, expected)

assertAlmostEquals(actual, expected, margin)

assertNotAlmostEquals(actual, expected, margin)

Value assertions

assertTrue(value)

assertFalse(value)

assertNil(value)

assertNotNil(value)

assertIs(actual, expected)

assertNotIs(actual, expected)

String assertions

assertStrContains(str, sub[, useRe])

assertStrIContains(str, sub)

assertNotStrContains(str, sub, useRe)

assertNotStrIContains(str, sub)

assertStrMatches(str, pattern[, start[, final]])

Error assertions

assertError(func, ...)

assertErrorMsgEquals(expectedMsg, func, ...)

assertErrorMsgContains(partialMsg, func, ...)

assertErrorMsgMatches(expectedPattern, func, ...)

Type assertions

assertIsNumber(value)

assertIsString(value)

assertIsTable(value)

assertIsBoolean(value)

assertIsNil(value)

assertIsFunction(value)

assertIsUserdata(value)

assertIsThread(value)

Table assertions

assertItemsEquals(actual, expected)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: