luaunitの使用

7401 ワード

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 astertions
  • astertEquals
  • astert NotEquals
  • astert AlmostEquals(actual、expected、magin)
  • astert NotAlmostEquals
  • Valueassetions
  • astert True
  • astert False
  • astertNil(value)
  • astert NotNil
  • astertIs
  • astert NotIs
  • Stering astertions
  • astert StarContins(str,sub[,useRe])
  • astert StrIContains(str,sub)
  • astert NotStr Contins(str,sub,useRe)
  • astert Norticontains
  • astertStarMatch(str,pattern[,start[,final]])
  • Errer astertions
  • astertError
  • astertError MsgEquals(expectedMsg,func,…)
  • astertErrror MsgContins(partialMsg,func,…)
  • astertErrrorMsgMatch(expectedPattern,func,…)
  • Type astertions
     
  • astertIsNumber
  • astertIsString
  • astertIsTable
  • astertIs Boolean
  • astertIsNil
  • astertIsFunction
  • astertIsUserdaa
  • astertIs Thread
  •  
    Table assetions
  • astertItemsEquals(actual、expected)