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.

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

单元测试是在开发过程中测试你的代码, 不是放在产品中测试。

典型步骤:

1 开始准备测试环境

2 写一些代码调用产品功能代码

3 检验期望的结果和实际结果是否相符

各类测试框架

Lua has several frameworks to do that:

  • [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

说明:

1、 setup 和 teardown在没个测试函数之前和之后都会执行。

2、 以test为开头的函数都会被执行。

#!/usr/bin/env lua

local lu = require('luaunit')

TestToto = {} --class

    function TestToto:setUp()
-- set up tests
self.a =
self.s = 'hop'
self.t1 = {,,}
self.t2 = {one=,two=,three=}
self.t3 = {,,three=}
end function TestToto:test1_withFailure()
-- print( "some stuff test 1" )
lu.assertEquals( self.a , )
-- will fail
lu.assertEquals( self.a , )
lu.assertEquals( self.a , )
end function TestToto:test2_withFailure()
-- print( "some stuff test 2" )
lu.assertEquals( self.a , )
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 , )
lu.assertEquals( self.s , 'hop' )
lu.assertEquals( type(self.a), 'number' )
end function TestToto:test4()
-- print( "some stuff test 4" )
lu.assertNotEquals( self.a , )
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( {,}, 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 =
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 , )
-- will fail
lu.assertEquals( self.a , )
lu.assertEquals( self.a , )
end function TestTiti:test2_withFailure()
-- print( "some stuff test 2" )
lu.assertEquals( self.a , )
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 , )
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( == )
-- will fail
assert( == )
end function test2_withFailure()
assert( 'a' == 'a')
-- will fail
assert( 'a' == 'b')
end function test3()
assert( == )
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: TestTiti
not ok 1    TestTiti.test1_withFailure
    example_with_luaunit.lua:101: expected: 2, actual: 1
not ok 2    TestTiti.test2_withFailure
    example_with_luaunit.lua:110: expected: "bof"
    actual: "hop"
ok     3    TestTiti.test3
# Starting class: TestToto
not ok 4    TestToto.test1_withFailure
    example_with_luaunit.lua:21: expected: 2, actual: 1
not ok 5    TestToto.test2_withFailure
    example_with_luaunit.lua:30: expected: "bof"
    actual: "hop"
ok     6    TestToto.test3
not ok 7    TestToto.test4
    example_with_luaunit.lua:43: Received the not expected value: 1
not ok 8    TestToto.test5
    example_with_luaunit.lua:49: expected: false, actual: 1
not ok 9    TestToto.test6
    example_with_luaunit.lua:54: expected: true, actual: false
not 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

lua unit test introduction的更多相关文章

  1. Unit Testing with NSubstitute

    These are the contents of my training session about unit testing, and also have some introductions a ...

  2. Lua Doc生成工具

    Luadoc http://keplerproject.github.io/luadoc/ Overview LuaDoc is a documentation generator tool for ...

  3. CG&Game资源(转)

    cg教程下载: http://cgpeers.com http://cgpersia.com http://bbs.ideasr.com/forum-328-1.html http://bbs.ide ...

  4. 面向小白的JS笔记 - #Codecademy#学习笔记

    前言 最初浏览过<JavaScript秘密花园>,前一段时间读过一点点<JavaScript语言精粹>和一点点<JavaScript高级程序设计>(一点点是指都只是 ...

  5. How to Avoid Producing Legacy Code at the Speed of Typing

    英语不好翻译很烂.英语好的去看原文. About the Author I am a software architect/developer/programmer.I have a rather p ...

  6. 基于 Cocos2d-x-lua 的游戏开发框架 Dorothy 简介

    基于 Cocos2d-x-lua 的游戏开发框架 Dorothy 简介 概述 Dorothy 是一个在 Cocos2d-x-lua 基础上发展起来的分支, 它去掉 Cocos2d-x-lua 那些过多 ...

  7. 斯坦福CS课程列表

    http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principl ...

  8. [Unit Test] Unit Test Brief Introduction

    Levels of Testing- Acceptance- Performance- Functional- Integration- Unit Why Unit Testing- Feedback ...

  9. Unit Tests Tool - <What is “Mock You”> The introduction to moq #Reprinted#

    From: http://www.cnblogs.com/wJiang/archive/2010/02/21/1670632.html Moq即Mock You Framework,故名思意是一个类似 ...

随机推荐

  1. 廖雪峰js教程笔记13 插入DOM

    当我们获得了某个DOM节点,想在这个DOM节点内插入新的DOM,应该如何做? 如果这个DOM节点是空的,例如,<div></div>,那么,直接使用innerHTML = '& ...

  2. Poj2479 & Poj 2593

    就是按着DP的思路来做的,结果还是想不到.T_T,行了,别玻璃心了,继续. 这道题目是求在一列数里,由两部分子段和组成的最大和.即对于连续整数组成的串 S1.S2,使 S1 + S2 的和最大. 题目 ...

  3. PHP-Redis扩展使用手册(二)

    /* 根据多个key获取多个value,不存在的key返回false getMultiple是别名? * @param array 包含key的数组 * @return array 返回key对应va ...

  4. Servlet和Struts2同时使用

    在做WEB项目时,要么是Struts+Spring+Hibernate,要是就直接使用servlet.这次碰到一个项目,经理想要把之前一个用servlet做的部分模块移植到当前项目下(Struts+S ...

  5. 移动手机端H5无缝间歇平滑向上滚动js代码

    在没结合css3的transform实现平滑过渡前,我都是用的jquery的animate方法,此方法在PC端基本看不出来有稍微卡顿的现象,但是在性能不高的手机上使用该方法,就会有明显的卡顿现象,不够 ...

  6. #20145205 《Java程序设计》第5周学习总结

    教材学习内容总结 1.java中的错误都会被包装为对象这是书上的一句原话,我的理解就是,在java这个大环境中,就像是流水线工厂一样,代码的编译就是进行流水线作业,代码输进来,开始的时候就是打包封装, ...

  7. UILabel笔记(待完善)

    UIlabel的换行由 numberOfLines 属性控制,当为0时,则会自动换到适合的行数: 换行的模式由 lineBreakMode 属性控制: public enum NSLineBreakM ...

  8. bzoj 4445 [SCOI2015] 小凸想跑步

    题目大意:一个凸包,随机一个点使得其与前两个点组成的面积比与其他相邻两个点组成的面积小的概率 根据题意列方程,最后求n条直线的交的面积与原凸包面积的比值 #include<bits/stdc++ ...

  9. 《Linux内核设计与实现》读书笔记 第五章 系统调用

    第五章系统调用 系统调用是用户进程与内核进行交互的接口.为了保护系统稳定可靠,避免应用程序恣意忘形. 5.1与内核通信 系统调用在用户空间进程和硬件设备间添加了一个中间层, 作用:为用户空间提供了一种 ...

  10. 【5集iCore3_ADP演示视频】5-1 iCore3应用开发平台开箱视频

    iCore3双核心应用开发平台基于iCore3双核心板,包含ARM.FPGA.7寸液晶屏.双通道数字示波器.任意波发生器.电压表等模块,是一款专为电子爱好者设计的综合性电子学习系统. [视频简介]本视 ...