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)相关文章推荐
- lua-intf的小技巧
- R中创建not-yet-evaluated对象
- Lua 引用外部文件(require)的调试错误
- Lua基础记录,附一个在线测试网址
- 冒泡排序(lua实现)
- torch中narrow的用法
- LUA中的and与or
- torch7安装方法
- lua学习(一)
- luarocks模块管理工具
- lua优化
- weka:SimpleKMeans实现Class to clusters evaluation验证
- Lua实现类与继承的两种方式(复制)and (闭包)
- VS2012实现C++与Lua交互
- lua的栈
- quick 2.23 它们的定义c++代码lua与总结的一些细节
- lua string库函数详解、实例及lua正则表达式
- LeetCode -- Evaluate Reverse Polish Notation
- LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)
- leetcode笔记:Evaluate Reverse Polish Notation(逆波兰式的计算)