beautypg.com

MTS Multipurpose Elite User Manual

Page 289

background image

# these variables, it is recommended that you simulate their
# existence here by initializing them with some valid default
# values.
# =================================================
tsVarArray = [1, 2, 3, 4, 5]
# ================================
# 2) Define the custom function
# ==========================
++++++++++++++++++++++++
# ** NOTE: TestSuite's custom function editor will NOT
# include the iron python libs **
# Therefore, it is required to import the resources you
# will be referencing within your custom function. This is
# done using the "sys.path.append(resourcePath)" method.
#================================================
import sys
sys.path.append(r"C:\Program Files (x86)\IronPython 2.7\Lib")
import os.path
def readFileAsArray(filePath):
results = []
if os.path.exists(filePath):
# Open the file in read mode
infile = open(filePath, "r")
try:
while True:
# Read a line as text, removing newlines
aLine = infile.readline()
if not aLine:
break;
else:
# Convert the line to numbers and add to
# results
results.append(int(aLine.rstrip()))
except:
results = []
return results
def writeArrayToFile(array, filePath):
try:
# Open the file in write mode
FILE = open(filePath, "w")
# Write each number as a string to the file
for num in array:
FILE.write(str(num) + "\n")
FILE.close()
return True
except:
return False
# ==============================================
# 3) Execute custom function test environment
# ==============================================
file = r"testFileIO.txt"
if writeArrayToFile(tsVarArray, file):
print "Array successfully written to " + str(file)
else:

MTS TestSuite | 289

Working with Variables