luaunit的使用

github的地址:

https://github.com/bluebird75/luaunit

使用说明文档:

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

使用方法:

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

UT目标文件:testlib.lua

 1 function add(v1,v2)
 2     -- add positive numbers
 3     -- return 0 if any of the numbers are 0
 4     -- error if any of the two numbers are negative
 5     if v1 < 0 or v2 < 0 then
 6         error('Can only add positive or null numbers, received '..v1..' and '..v2)
 7     end
 8     if v1 == 0 or v2 == 0 then
 9         return 0
10     end
11     return v1+v2
12 end

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

 1 EXPORT_ASSERT_TO_GLOBALS = true
 2 package.path=[[D:\3SG\Test\luaunit-master\?.lua;E:\learnSkill\lua\lua_test\?.lua;]]  ..  package.path
 3 luaunit = require('luaunit')
 4 --引入测试框架luaunit
 5 
 6 --[[
 7 UT
 8 --]]
 9 unit = require('testlib')
10 --包含所要测试的函数库(测试目标)
11 
12 --以下按照table分类测试用例(分组TestAdd与TestDiv)
13 TestAdd = {}
14 function TestAdd:testAddPositive()
15     local args = {1,1}
16     luaunit.assertEquals(add(1,1),2)
17 end
18 
19 function TestAdd:testAddZero()
20     luaunit.assertEquals(add(1,0),0)
21     luaunit.assertEquals(add(0,5),0)
22     luaunit.assertEquals(add(0,0),0)
23 end
24 
25 function TestAdd:testAddError()
26     luaunit.assertErrorMsgContains('Can only add positive or null numbers, received 2 and -3', add, 2, -3)
27 end
28 
29 function TestAdd:testAdder()
30     f = adder(3)
31     luaunit.assertIsFunction( f )
32     luaunit.assertEquals( f(2), 5 )
33 end
34 -- end of table TestAdd
35 
36 TestDiv = {}
37     function TestDiv:testDivPositive()
38         luaunit.assertEquals(div(4,2),2)
39     end
40 
41     function TestDiv:testDivZero()
42         luaunit.assertEquals(div(4,0),0)
43         luaunit.assertEquals(div(0,5),0)
44         luaunit.assertEquals(div(0,0),0)
45     end
46 
47     function TestDiv:testDivError()
48         luaunit.assertErrorMsgContains('Can only divide positive or null numbers, received 2 and -3', div, 2, -3)
49     end
50 -- end of table TestDiv
51 
52 --以下为用于每个测试用例执行前后的资源初始化setUp和释放tearDown
53 TestLogger = {}
54     function TestLogger:setUp()
55         -- define the fname to use for logging
56         self.fname = 'mytmplog.log'
57         -- make sure the file does not already exists
58         os.remove(self.fname)
59     end
60 
61     function TestLogger:tearDown()
62         -- cleanup our log file after all tests
63         os.remove(self.fname)
64     end
65 -- end of table TestLogger
66 --[[
67 UT
68 --]]
69 
70 
71 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)

你可能感兴趣的:(luaunit的使用)