lua unit test introduction

Unit Test

Unit testing is about testing your code during development, not in production. Typically you start by preparing the testing environment, writing some code that calls production code and check expected results with actual results.css

http://lua-users.org/wiki/UnitTestinghtml

單元測試是在開發過程當中測試你的代碼, 不是放在產品中測試。git

典型步驟:github

1 開始準備測試環境api

2 寫一些代碼調用產品功能代碼框架

3 檢驗指望的結果和實際結果是否相符dom

 

各種測試框架

Lua has several frameworks to do that:ide

  • [lunit]
  • [Lunity] (similar to lunit, but can only run a single file and doesn't distinguish between errors and test assertions)
  • [lunatest] (upwardly compatible from lunit, but no dependencies, and also supports randomized testing)
  • [LuaUnit] (supports Lua 5.1 and 5.2, no dependencies, see below)
  • [Shake] - source preprocessing using basic assert-like syntax (see below)
  • [build-test-deploy (BTD)] - Unit testing inspired by jUnit and originally based on LuaUnit
  • [luaspec][1] - Behavior Driven Development (BDD) test framework
  • [PenLight pl.test] - PenLightLibraries? has a very small unit testing library, mostly for internal testing. See the Penlight tests [here]
  • [telescope] - A highly customizable test library for Lua that allows for declarative tests with nested contexts. Uses BDD-style spec names.
  • [lua-TestMore] - port of the Perl5 module Test::More (Test Anything Protocol).
  • [busted] - Behavior Driven Development (BDD) unit testing library, with various outputs formats including TAP and focus on elegance.
  • [gambiarra] - Ultimately minimal unit testing library for Lua 5.1 and Lua 5.2.
  • [Testy] - Lua script for minimal unit testing on Lua 5.1/5.2/5.3 that collects test functions from local variables.

 

They have mostly the same feature set, which is:函數

  • a special set of assert functions
  • facilities to group tests into test suites
  • facilities to detect where test fails

測試框架大都有相同的特性集合:單元測試

1 assert函數的特殊集合

2 將測試分組爲若干測試套件

3 檢測測試失敗的代碼點

 

框架比較

http://lua-users.org/wiki/UnitTesting

名稱 地址 最近發佈時間 是否支持group 測試用例寫法 支持lua版本
LuaUnit https://github.com/bluebird75/luaunit

Version 3.2 - 12. Jul 2016

OOP & 函數 Lua 5.1, LuaJIT 2.0, LuaJIT 2.1 beta, Lua 5.2 and Lua 5.3

lunit

https://www.mroth.net/lunit/ 5. November 2009: Lunit Version 0.5 released. 函數 Lua 5.1

busted

http://olivinelabs.com/busted/#spies-mocks-stubs

v2.0.rc11-0 28 Oct 2015

  BDD lua >= 5.1, moonscript, terra, and LuaJIT >= 2.0.0.

lunatest

https://github.com/silentbicycle/lunatest

v0.9.5   3 Apr 2014 

是(同時支持分文件管理) OOP & 函數 Upgrade to Lua 5.2, but keep compatible with Lua 5.1

luaspec

https://github.com/mirven/luaspec     BDD  

 

Lunity

 

 https://github.com/Phrogz/Lunity  v0.10 15 Feb 2014  否 函數 Lua 5.1 

 

從表中看出, luaUnit 最活躍, 支持lua版本最多, 測試用例的寫法最靈活。

lunatest其中測試用例能夠拆分爲子文件的方法,對龐大測試用例維護有好處。

BDD(行爲驅動開發)領域 BUSTED最爲成熟,和活躍, 文檔最完善。

 

luaUnit 測試DEMO

文檔:

http://luaunit.readthedocs.io/en/latest/   含有接口介紹

https://github.com/bluebird75/luaunit

 

說明:

一、 setup 和 teardown在沒個測試函數以前和以後都會執行。

二、 以test爲開頭的函數都會被執行。

#!/usr/bin/env lua


local lu = require('luaunit')

TestToto = {} --class

    function TestToto:setUp()
        -- set up tests
        self.a = 1
        self.s = 'hop' 
        self.t1 = {1,2,3}
        self.t2 = {one=1,two=2,three=3}
        self.t3 = {1,2,three=3}
    end

    function TestToto:test1_withFailure()
        -- print( "some stuff test 1" )
        lu.assertEquals( self.a , 1 )
        -- will fail
        lu.assertEquals( self.a , 2 )
        lu.assertEquals( self.a , 2 )
    end

    function TestToto:test2_withFailure()
        -- print( "some stuff test 2" )
        lu.assertEquals( self.a , 1 )
        lu.assertEquals( self.s , 'hop' )
        -- will fail
        lu.assertEquals( self.s , 'bof' )
        lu.assertEquals( self.s , 'bof' )
    end

    function TestToto:test3()
        -- print( "some stuff test 3" )
        lu.assertEquals( self.a , 1 )
        lu.assertEquals( self.s , 'hop' )
        lu.assertEquals( type(self.a), 'number' )
    end

    function TestToto:test4()
        -- print( "some stuff test 4" )
        lu.assertNotEquals( self.a , 1 )
    end

    function TestToto:test5()
        -- print( "some stuff test 5" )
        lu.assertTrue( self.a )
        lu.assertFalse( self.a )
    end

    function TestToto:test6()
        -- print( "some stuff test 6" )
        lu.assertTrue( false )
    end

    function TestToto:test7()
        -- assertEquals( {1,2}, self.t1 )
        -- assertEquals( {1,2}, self.t2 )
        lu.assertEquals( {1,2}, self.t3 )
    end

    function TestToto:test8a()
        -- failure occurs in a submethod
        self:funcWithError()
    end

    function TestToto:test8b()
        -- failure occurs in a submethod
        self:funcWithFuncWithError()
    end

    function TestToto:funcWithFuncWithError()
        self:funcWithError()
    end

    function TestToto:funcWithError()
        error('Bouhouhoum error!')
    end


-- class TestToto

TestTiti = {} --class
    function TestTiti:setUp()
        -- set up tests
        self.a = 1
        self.s = 'hop' 
        -- print( 'TestTiti:setUp' )
    end

    function TestTiti:tearDown()
        -- some tearDown() code if necessary
        -- print( 'TestTiti:tearDown' )
    end

    function TestTiti:test1_withFailure()
        -- print( "some stuff test 1" )
        lu.assertEquals( self.a , 1 )
        -- will fail
        lu.assertEquals( self.a , 2 )
        lu.assertEquals( self.a , 2 )
    end

    function TestTiti:test2_withFailure()
        -- print( "some stuff test 2" )
        lu.assertEquals( self.a , 1 )
        lu.assertEquals( self.s , 'hop' )
        -- will fail
        lu.assertEquals( self.s , 'bof' )
        lu.assertEquals( self.s , 'bof' )
    end

    function TestTiti:test3()
        -- print( "some stuff test 3" )
        lu.assertEquals( self.a , 1 )
        lu.assertEquals( self.s , 'hop' )
    end
-- class TestTiti

-- simple test functions that were written previously can be integrated
-- in luaunit too
function test1_withFailure()
    assert( 1 == 1)
    -- will fail
    assert( 1 == 2)
end

function test2_withFailure()
    assert( 'a' == 'a')
    -- will fail
    assert( 'a' == 'b')
end

function test3()
    assert( 1 == 1)
    assert( 'a' == 'a')
end

local runner = lu.LuaUnit.new()
runner:setOutputType("tap")
os.exit( runner:runSuite() )

 

LOG:

1..15# Started on 09/15/16 20:53:03# Starting class: TestTitinot ok 1    TestTiti.test1_withFailure    example_with_luaunit.lua:101: expected: 2, actual: 1not ok 2    TestTiti.test2_withFailure    example_with_luaunit.lua:110: expected: "bof"    actual: "hop"ok     3    TestTiti.test3# Starting class: TestTotonot ok 4    TestToto.test1_withFailure    example_with_luaunit.lua:21: expected: 2, actual: 1not ok 5    TestToto.test2_withFailure    example_with_luaunit.lua:30: expected: "bof"    actual: "hop"ok     6    TestToto.test3not ok 7    TestToto.test4    example_with_luaunit.lua:43: Received the not expected value: 1not ok 8    TestToto.test5    example_with_luaunit.lua:49: expected: false, actual: 1not ok 9    TestToto.test6    example_with_luaunit.lua:54: expected: true, actual: falsenot ok 10    TestToto.test7    example_with_luaunit.lua:60: expected: {1, 2, three=3}    actual: {1, 2}not ok 11    TestToto.test8a    example_with_luaunit.lua:78: Bouhouhoum error!not ok 12    TestToto.test8b    example_with_luaunit.lua:78: Bouhouhoum error!not ok 13    test1_withFailure    example_with_luaunit.lua:126: assertion failed!not ok 14    test2_withFailure    example_with_luaunit.lua:132: assertion failed!ok     15    test3# Ran 15 tests in 0.004 seconds, 3 successes, 8 failures, 4 errors

相關文章
相關標籤/搜索