PEDN> Main Web>NPL>UnitTestFramework (2008-02-29, LiXizhi) Change language en zh-cn? EditAttach

CommonCtrl.CheckBox

Unit Test Module read me file

Title Unit Test Module read me file
Author(s) LiXizhi
Date 2008/3/5
File script/ide/UnitTest/readme.lua

Description

TIP Sample Code

NPL.load("(gl)script/ide/UnitTest/readme.lua");

Unit Test Module

The unit test module parses a test file, optionally replace test case input with user supplied ones, and finally output the test result to file and log.

Test file markup: A test file may contain a group of test cases. Test cases can be declared in NPL comment block with the following format:

   %TESTCASE{"<test_case_name>", func = "<test_function>", input = <default_input>, output="<result_file>"}%
The content inside TESTCASE is actually an NPL table, where
  • test_case_name is test case name, test_function is the name of the function to be called for the test case.
  • The test function is usually defined in the test file itself and should use global name.
  • default_input is input to the test case function.
  • result_file is where to save the test result. if this is nil, it will be saved to testfile.result

For an example test file, please see script/ide/UnitTest/sample_test_file.lua.

--[[
Title: Unit Test sample file
Author(s): LiXizhi
Date: 2008/3/5
Desc: 
use the lib:
------------------------------------------------------------
NPL.load("(gl)script/ide/UnitTest/sample_test_file.lua");
test.Example_Test_Function()

-- result at temp/test_result.txt
%TESTCASE{"Example Test", func="test.Example_Test_Function", input={varInt = 1, 
   varString = "this is a string"}, output="temp/test_result.txt"}%
-- result at script/ide/UnitTest/sample_test_file.lua.result
%TESTCASE{"Example Test 2", func="test.Example_Test_Function", input={varInt2 = 2, 
   varString2 = "Another input"}}%
-------------------------------------------------------
]]

if(not test) then test ={} end

-- passed by LiXizhi 2008.3.5
function test.Example_Test_Function(input)
   log(commonlib.serialize(input).." Test Succeed\n")
end

The result of "Example Test" will be saved to "temp/test_result.txt". It is in TWiki format which one can post to twiki topic.

---+++ case 1
_case_: Example Test

Input:
<verbatim>
{
  ["varString"] = "this is a string",
  ["varInt"] = 1,
}
</verbatim>

_Result_:
<verbatim>
{
  ["varString"] = "this is a string",
  ["varInt"] = 1,
}
 Test Succeed
</verbatim>

TIP To invoke unit test programmatically, use following code

-------------------------------------------------------
NPL.load("(gl)script/ide/UnitTest/unit_test.lua");
local test = commonlib.UnitTest:new();
if(test:ParseFile("script/ide/UnitTest/sample_test_file.lua")) then
   test:Run();
end
-- or one can call individually with user input. 
local test = commonlib.UnitTest:new();
if(test:ParseFile("script/ide/UnitTest/sample_test_file.lua")) then
   test:ClearResult();
   local i, count = 1, test:GetTestCaseCount()
   for i = 1, count do
      test:RunTestCase(i, {"any user input"});
   end
end
-------------------------------------------------------

TIP To invoke unit test from a graphically user interface, please install the DeveloperApp. Alternatively, one can integrate unit testing UI in any place by calling unit_test_dlg.lua.

]]

a unit test framework

Title a unit test framework
Author(s) LiXizhi
Date  
File script/ide/UnitTest/unit_test.lua

Description

It parses a test file (see sample_test_file), optionally replace test case input with user supplied ones, and finally output the test result to file and log. Test file markup: A test file may contain a group of test cases. Test cases can be declared in NPL comment block with the following format: %TESTCASE{"", func = "", input = , output=""}% The content inside %TESTCASE is actually an NPL table, where test_case_name is test case name, test_function is the name of the function to be called for the test case. The test function is usually defined in the test file itself and should use global name. default_input is input to the test case function. result_file is where to save the test result. if this is nil, it will be saved to testfile.result For an example test file, please see script/ide/UnitTest/sample_test_file.lua.

TIP Sample Code

NPL.load("(gl)script/ide/UnitTest/unit_test.lua");
local test = commonlib.UnitTest:new();
if(test:ParseFile("script/ide/UnitTest/sample_test_file.lua")) then
   test:Run();
end
-- or one can call individually with user input. 
local test = commonlib.UnitTest:new();
if(test:ParseFile("script/ide/UnitTest/sample_test_file.lua")) then
   test:ClearResult();
   local i, count = 1, test:GetTestCaseCount()
   for i = 1, count do
      test:RunTestCase(i, {"any user input"});
   end
end

Member Functions

UnitTest:ParseFile

Parse the test case file so that we can extract all test case info in the file, such as the number of test cases in the file, and the description and input of each test cases.

syntax

function UnitTest:ParseFile(testfilename)

parameters

testfilename  

UnitTest:GetTestCaseCount

  • return __ : the number test cases in the parsed test file. Always call ParseFile? before calling this.

syntax

function UnitTest:GetTestCaseCount()

parameters

return the number test cases in the parsed test file. Always call ParseFile? before calling this.

UnitTest:GetTestCase

get test case at index

syntax

function UnitTest:GetTestCase(index)

parameters

index  

UnitTest:RunTestCase

run a given test case with optionally user specified input

  • param i : the test case index. 1 based index.
  • param input : optionally user specified input to be be passed to the test function. if it is nil, the default input will be used.

syntax

function UnitTest:RunTestCase(i, input)

parameters

i the test case index. 1 based index.
input  

UnitTest:Run

run all test cases with default input for each test cases.

syntax

function UnitTest:Run()

UnitTest:ClearResult

clear all output result from previous test

syntax

function UnitTest:ClearResult()

a unit test case in unit test framework

Title a unit test case in unit test framework
Author(s) LiXizhi
Date  
File script/ide/UnitTest/unit_test_case.lua

Description

TIP Sample Code

NPL.load("(gl)script/ide/UnitTest/unit_test_case.lua");
local testcase = commonlib.TestCase:new();
testcase:Init("{\"Example Test\", func=\"test.Example_Test_Function\", input={varInt = 1, varString = \"this is a string\"}, output=\"temp/test_result.txt\"}");
testcase:Run();

Member Functions

TestCase:new

---------------------------------------------
 unit test case class 
---------------------------------------------
local TestCase = {
   -- unit test case name 
   name = "untitled", 
   -- test file name
   testfilename = nil,
   -- function pointer of the test function
   func = nil,
   -- default input to test function 
   input = nil,
   -- test result output file, if nil it will default to <testfilename>.result
   output = nil,
}
commonlib.TestCase = TestCase? ;

syntax

function TestCase:new (o)

parameters

o  

TestCase:Init

init test from string.

syntax

function TestCase:Init(textString)

parameters

textString  

TestCase:Run

run test

  • param input : input table to test case function or nil to use the default.

syntax

function TestCase:Run(input)

parameters

input input table to test case function or nil to use the default.

a UI for running test case file

Title a UI for running test case file
Author(s) LiXizhi
Date  
File script/ide/UnitTest/unit_test_dlg.lua

Description

TIP Sample Code

NPL.load("(gl)script/ide/UnitTest/unit_test_dlg.lua");
local ctl = CommonCtrl.UnitTestDlg:new{
   name = "UnitTestDlg",
   alignment = "_lt",
   left = 0,
   top = 0,
   width = 600,
   height = 450,
   parent = nil,
};
ctl:Show();

Member Functions

UnitTestDlg:UpdateHistoryTestFiles

load history test files

syntax

function UnitTestDlg:UpdateHistoryTestFiles()

UnitTestDlg.OnClickLoadTestFile

load the given test file and databind all test cases.

syntax

function UnitTestDlg.OnClickLoadTestFile(sCtrlName)

parameters

sCtrlName  

CommonCtrl.UnitTestDlg.OnClickRun

run the current test.

syntax

function CommonCtrl.UnitTestDlg.OnClickRun(sCtrlName)

parameters

sCtrlName  

CommonCtrl.UnitTestDlg.OnClickShowLastResult

open the last test run result for the selected test case.

syntax

function CommonCtrl.UnitTestDlg.OnClickShowLastResult(sCtrlName)

parameters

sCtrlName  

CommonCtrl.UnitTestDlg.OnClickClearLog

clear log result box

syntax

function CommonCtrl.UnitTestDlg.OnClickClearLog(sCtrlName)

parameters

sCtrlName  

CommonCtrl.UnitTestDlg.OnClickRefreshLog

refresh log result box

syntax

function CommonCtrl.UnitTestDlg.OnClickRefreshLog(sCtrlName)

parameters

sCtrlName  

CommonCtrl.UnitTestDlg.OnSelectTestCase

user selected a test case.

syntax

function CommonCtrl.UnitTestDlg.OnSelectTestCase(treenode)

parameters

treenode  
Topic revision: r1 - 2008-02-29 - 15:26:12 - LiXizhi
 

ParaEngine Developer Network

This site is powered by the TWiki collaboration platformCopyright © 2004-2009 ParaEngine Corporation
Ideas, requests, problems regarding ParaEngine platform Send feedback