"}%
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.
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
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
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
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
TestCase:Init
init test from string.
syntax
function TestCase:Init(textString)
parameters
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
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
CommonCtrl.UnitTestDlg.OnClickRun
run the current test.
syntax
function CommonCtrl.UnitTestDlg.OnClickRun(sCtrlName)
parameters
CommonCtrl.UnitTestDlg.OnClickShowLastResult
open the last test run result for the selected test case.
syntax
function CommonCtrl.UnitTestDlg.OnClickShowLastResult(sCtrlName)
parameters
CommonCtrl.UnitTestDlg.OnClickClearLog
clear log result box
syntax
function CommonCtrl.UnitTestDlg.OnClickClearLog(sCtrlName)
parameters
CommonCtrl.UnitTestDlg.OnClickRefreshLog
refresh log result box
syntax
function CommonCtrl.UnitTestDlg.OnClickRefreshLog(sCtrlName)
parameters
CommonCtrl.UnitTestDlg.OnSelectTestCase
user selected a test case.
syntax
function CommonCtrl.UnitTestDlg.OnSelectTestCase(treenode)
parameters