John
John

Reputation: 971

create variables dynamically in python and use them within subprocess as args

I'm trying to create a number of args for mstest.

Basically I run all our automated tests and parse the trx results file to get all the failed tests and use this to rerun all the failed tests.

I've created a python script that runs our CodedUI GUI automation test cases via mstest.

Step1: Calls a subprocess like this:

test_exe = "C:\VS14\Common7\IDE\mstest.exe"
test_container = "/testcontainer:\"C:\GUIAutomation\CodedUIGUIAutomation.dll\""
test_settings = "/testsettings:\"C:\GUIAutomation\CodedUI.testsettings\""
test_results = "/results:\"C:\GUIAutomation\results_automated.trx\""

p = subprocess.call([test_exe, test_container, test_settings, test_category, test_results])

MSTEST Run Command_1: C:\VS14\Common7\IDE\mstest.exe /testcontainer:"C:\GUIAutomation\CodedUIGUIAutomation.dll /testsettings:"C:\GUIAutomation\CodedUI.testsettings" /category:Automated /resultsfile:C:\GUIAutomation\results_automated.trx

Step2: Parse trx results file to get a list of failed tests which i append to a list to rerun

fails_list.append(result.attrib['test1']
fails_list.append(result.attrib['test2']
fails_list.append(result.attrib['test3']

for x in fails_list:
    test_list = test_list + "/test:{0} ".format(str(x))

test_list output: "/test:test1 /test:test2 /test:test3"

Step3: Which I then try and rerun with this subprocess.call..

test_results = "/results:\"C:\GUIAutomation\results_automated_rerun.trx\""

p = subprocess.call([test_exe, test_container, test_settings, test_list, test_results])
MSTEST Run Command_1: C:\VS14\Common7\IDE\mstest.exe /testcontainer:"C:\GUIAutomation\CodedUIGUIAutomation.dll /testsettings:"C:\GUIAutomation\CodedUI.testsettings" /test:test1 /test:test2 /test:test3 /resultsfile:C:\GUIAutomation\results_automated_rerun.trx

This fails because /test:test1 /test:test2 is seen as one arg, but if i cut and paste it into a command prompt it works perfectly.

So the list of tests should be individual args for subprocess. So instead of

test_list = "/test:test1 /test:test2 /test:test3"
p = subprocess.call([test_exe, test_container, test_settings, test_list, test_results]

It should be

arg_1 = "/test:test1"
arg_2 = "/test:test2" 
arg_3 = "/test:test3"
p = subprocess.call([test_exe, test_container, test_settings, arg_1, arg_2, arg_3, test_results]

So how do i generate multiple args on the fly as on one run I might have only one test failure, but on another I could have 4 or 5 and then insert them into subprocess.call.

A few other notes:

Upvotes: 1

Views: 70

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

just generate test_list as a list of arguments:

test_list = ["/test:{}".format(str(x)) for x in fails_list]

then compose your argument list

p = subprocess.call([test_exe, test_container, test_settings] + test_list + [test_results])

note that

test_results = "/results:\"C:\GUIAutomation\results_automated_rerun.trx\""

is wrong because the path contains a \r: parsed as carriage return. Should be (if the quotes are really needed, else drop them):

test_results = r'/results:"C:\GUIAutomation\results_automated_rerun.trx"'

(using raw prefix + simple quotes to avoid escaping double quotes)

Upvotes: 0

Related Questions