Thursday, November 16, 2006

First test case using get_median

Here is a simple test script that will run get_median seven times, passing in a variety of parameters along the way. The only valid case occurs when the first parameter of "1" is passed and parameters two and three are valid. This seemingly mindless test case (which many astute and seasoned programmers will scoff at) actually does a lot for me. First, it helps to ensure that any future changes I make to this or similar functions will run when unexpected parameters are passed in. Second, it allowed me to create a few useful functions that can be re-used on other test cases. Third, it forms the basis of a testing project that can be built upon as time and resources permit. The premise is that all my test cases can be chained together (preferably in a table) and run just before the project reaches QA or system testing.

What is left to do? For starters, this test script can be enhanced to not only pass in a string of values, but also an array or cursor. It is possible that doing so will introduce other issues that will need to be fixed. The end result, nevertheless, will be one bullet-proof get_median function that is as flexible as it is safe! In addition, this code will form the basis of a test class and be added to a testing project. For now, I’ll leave it in PRG form for simplicity.

Tomorrow, I'll post an updated get_median that fixes many of the bugs, while incorporating many of my boundary testing examples from prior entries.



**************************************************************
* The following code is my first test of get_median *
**************************************************************

ON ERROR ;
STRTOFILE(capture_error(ERROR( ), MESSAGE( ), MESSAGE(1),;
PROGRAM( ), LINENO( )),"test_err.log",1)


* What if the driver is unexpected, or para 2 and 3 are invalid?
FOR x = -2 TO 4
ePara2 = get_string(8)
epara3 = ","
eRetVal = get_median_clean(x,ePara2,ePara3)

STRTOFILE(TTOC(DATETIME())+ " get_median " + ;
PADL(TRANSFORM(x),4) + CHR(9) + ;
TRANSFORM(ePara2) + CHR(9) + ;
TRANSFORM(epara3) + CHR(9) + ;
TRANSFORM(eRetVal) + CHR(13) + CHR(10) ,"test_run.log",1)

NEXT


FUNCTION get_string()
LPARA tnSize

tnSize = IIF(VARTYPE(tnSize)<>"N",0,tnSize)
LOCAL lcString
lcString = ALLTRIM(STR(get_int()))
FOR n = 2 TO tnSize
lcString = lcString + "," + ALLTRIM(STR(get_int()))
NEXT
RETURN lcString
ENDFUNC

FUNCTION get_int
RETURN (RAND()*1000) - 500
ENDFUNC

FUNCTION capture_error (merror, mess, mess1, mprog, mlineno)
LOCAL cMess, cCRLF
cCRLF = CHR(13) + CHR(10)
cMess = 'Error number: ' + LTRIM(STR(merror)) + cCRLF + ;
'Error message: ' + mess + cCRLF + ;
'Line of code with error: ' + mess1 + cCRLF + ;
'Line number: ' + LTRIM(STR(mlineno)) + cCRLF + ;
'Program with error: ' + mprog + cCRLF + ;
REPLICATE("=",30) + cCRLF
RETURN cMess
ENDFUNC

No comments: