I am new to using Python and the following is the first script that I have written. I will execute a test complete project and will be started via team city. Once test execute has been called - the log file needs to be processed for consumption through TeamCity.
import sys
import argparse
import subprocess
import os.path
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--projectSuite", help=".pjs that you want to execute", type=str)
parser.add_argument("-p", "--project", help=".pj that you want to execute", type=str, default="ExploratoryTests")
parser.add_argument("-l", "--logDirectory", help="The directory to log the mht's too", type=str, default="C:\TestLogs")
parser.add_argument("-lf", "--logFile", help="The name of the log", type=str, default="TextExecuteLog.mht")
parser.add_argument("-ps", "--psexec", help="Absolute path to psexec", type=str, default="c:\Program Files (x86)\PsTools\PsExec.exe")
parser.add_argument("-te", "--testExecute", help="Absolute path to testExecute", type=str, default="c:\Program Files (x86)\SmartBear\TestExecute 9\Bin\TestExecute.exe")
parser.add_argument("-m", "--mhtExtractor", help="Absolute path to an mht extractor", type=str, default="C:\extract2htm\extractMHT.exe")
#Get the args that are passed in
args = parser.parse_args()
#Make sure we have an absolute path
suite = os.path.abspath(args.projectSuite)
#Prepare the project for consumption by TestExecute
project = "/p:" + args.project
#Get a path for the currenct mht
logFile = os.path.abspath(os.path.join(args.logDirectory, args.logFile))
#Tell TestExecute where to put the log
log = "/exportlog:" + logFile
#prepare TE Executables
psexec = os.path.abspath(args.psexec);
testExecute = os.path.abspath(args.testExecute)
#Execute testExecute through psexec
testExecuteCommand = [psexec,"-i" ,"-accepteula", testExecute, suite, project, "/exit", log, "/r", "/DoNotShowLog", "/SilentMode"]
#Blank array to hold the TE outcome
testExecuteError = [];
try:
result = subprocess.run(args = testExecuteCommand, stderr=subprocess.STDOUT, shell=True)
result.check_returncode()
testExecuteError.append(result)
except subprocess.CalledProcessError as e:
result = e.returncode
testExecuteError.append(e)
print("Error code: {0}".format(result))
extractor = os.path.abspath(args.mhtExtractor)
outputDir = os.path.join(args.logDirectory)
extractorExecuteCommand = [extractor, logFile, outputDir]
try:
result = subprocess.run(args = extractorExecuteCommand, stderr=subprocess.STDOUT, shell=True)
result.check_returncode()
except subprocess.CalledProcessError as err:
result = err.returncode
print("Error code: {0}".format(result))
#exit the script with the exit code from testexetute
sys.exit(testExecuteError[0])
The way in which I handle my exceptions from testExecute are the part that I'm not happy about but it is working as intended. Any suggestions are welcome.