On hard-coding of test data (2)

Posted by Albert Gareev on Jan 22, 2007 | Categories: NotesProblems

On hard-coding of test data, continued

Parameterization is a structured programming approach

Starting with example.

Public Function SumAB()
   SumAB = 2 + 3
End Function

Such a function is useless when you need to sum numbers different than 2 and 3. Now let’s apply mapping.

Const A = 2
Const B = 3
Public Function SumAB()
  SumAB = A + B
End Function

It is a little bit better, but we still have to change the source code and restart the script, if we want to sum different numbers.

Let’s try with parameters now.

Public Function SumAB(ByVal A, ByVal B)
  SumAB = A + B
End Function
C1 = SumAB(2,3)
C2 = SumAB(4,5)
C3 = SumAB(-1,4)

Now same function can sum any numbers, and we didn’t have to change source code of the function, when we changed numbers (As you remember, changing source code means retesting all functionalities it was used in).

Parameterizing scripts

For the entire testing script, we can apply the same parameterization mechanism. We only have to code assignment of parameters as they can be received from various sources.

Example (VBScript).

Const FilePath = "c:\data\testcase.txt"
Dim Username, Password
boolResult = InitParameters(FilePath)
'Main Code
objUsername.Set Username
objPassword.Set Password

The role of InitParameters function is to retrieve data values from an external source (text file, in our example) and assign them to variables, used in the testing script (Username and Password, in our example).

This way, any change of test data values does not require source code change for testing script.

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.