Revision: 4661
Author: jprantan
Date: Thu Mar 3 15:33:07 2011
Log: Improved library.
http://code.google.com/p/robotframework/source/detail?r=4661
Added:
/trunk/proto/parallel/Parallel.py
Deleted:
/trunk/proto/parallel/parallel.py
Modified:
/trunk/proto/parallel/para.txt
=======================================
--- /dev/null
+++ /trunk/proto/parallel/Parallel.py Thu Mar 3 15:33:07 2011
@@ -0,0 +1,85 @@
+import subprocess
+from time import time
+from random import randint
+import os
+import re
+import sys
+from robot.libraries import BuiltIn
+from robot.utils import html_escape
+
+class Parallel(object):
+
+ def __init__(self, runner_script):
+ self._script = runner_script
+ self._processes = []
+
+ def run_parallel_robot(self, test_name, *args):
+ process = _ParaRobo(test_name, *args)
+ process.run(self._script)
+ self._processes.append(process)
+ return process
+
+ def wait_for_parallel_tests_to_be_ready(self, *processes):
+ failed = []
+ for process in processes:
+ rval = process.wait()
+ process.report()
+ if rval != 0:
+ failed.append(process.test)
+ if failed:
+ raise AssertionError("Following tests
failed:\n%s" % "\n".join(failed))
+
+ def wait_for_all_parallel_tests_to_be_ready(self):
+ self.wait_for_parallel_tests_to_be_ready(*self._processes)
+ self._processes = []
+
+
+class _ParaRobo(object):
+ def __init__(self, test, *args):
+ self._built_in = BuiltIn.BuiltIn()
+ id = "%s%s" % (time(), randint(0, 1000000))
+ self._output = 'output_%s.xml' % id
+ self._log = 'log_%s.html' % id
+ self.test = test
+ self._args = list(args)
+ self._output_dir = self._built_in.replace_variables("${OUTPUT
DIR}")
+ self._monitor_out =
os.path.join(self._output_dir, 'monitor_%s.txt' % id)
+ self._suite_name =
self._built_in.replace_variables("${SUITE_NAME}")
+
+ def run(self, script):
+ with open(self._monitor_out, 'w') as monitor_file:
+ cmd = [script,
+ '--outputdir', self._output_dir,
+ '--output', self._output,
+ '--report', 'None',
+ '--log', self._log,
+ '--monitorcolors', 'off',
+ '--test', self.test.replace(' ', '').replace('/', '?')]+\
+ self._args
+ print "Starting test execution: %s" % " ".join(cmd)
+ self._process = subprocess.Popen(cmd,
+ shell=os.sep == '\\',
+ stdout=monitor_file,
+ stderr=monitor_file)
+
+ def wait(self):
+ return self._process.wait()
+
+ def report(self):
+ with open(self._monitor_out, 'r') as monitor_file:
+ monitor_output = monitor_file.read()
+ try:
+ os.remove(self._monitor_out)
+ except:
+ pass
+ match = re.search('^Log: (.*)$', monitor_output, re.MULTILINE)
+ monitor_output = html_escape(monitor_output)
+ if match:
+ monitor_output = monitor_output.replace(match.group(1), '<a
href="%s#test_%s.%s">%s</a>' % (self._log, self._suite_name, self.test,
match.group(1)))
+ monitor_output = self._add_colours(monitor_output)
+ print "*HTML* %s" % monitor_output
+
+ def _add_colours(self, output):
+ for name, colour in [("PASS", "pass"), ("FAIL", "fail"),
("ERROR", "fail")]:
+ output = output.replace(' %s ' % name, ' <span
class="%s">%s</span> ' % (colour, name))
+ return output
=======================================
--- /trunk/proto/parallel/parallel.py Thu Mar 3 01:04:59 2011
+++ /dev/null
@@ -1,67 +0,0 @@
-import subprocess
-from time import time
-from random import randint
-import os
-import sys
-from robot.libraries import BuiltIn
-
-class parallel(object):
-
- def run_parallel_test(self, test_name):
- process = _ParaRobo(test_name)
- process.run()
- return process
-
- def wait_for(self, *processes):
- fail = False
- for process in processes:
- rval = process.wait()
- process.report()
- if rval != 0:
- fail = True
- if fail:
- raise Exception('Subprocess failure')
-
-class _ParaRobo(object):
- def __init__(self, test):
- self._built_in = BuiltIn.BuiltIn()
- id = "%s%s" % (time(), randint(0, 1000000))
- self._output = 'output_%s.xml' % id
- self._log = 'log_%s.html' % id
- self._test = test
- self._output_dir = self._built_in.replace_variables("${OUTPUT
DIR}")
- self._monitor_out =
os.path.join(self._output_dir, 'monitor_%s.txt' % id)
- self._suite = self._built_in.replace_variables("${SUITE_SOURCE}")
- self._suite_name =
self._built_in.replace_variables("${SUITE_NAME}")
-
- def run(self):
- monitor_file = open(self._monitor_out, 'w')
- cmd = ['pybot',
- '-l', self._log,
- '-o', self._output,
- '--monitorcolors', 'off',
- '--variable', 'PARALLEL:True',
- '--test', self._test,
- '--report', 'None',
- '--outputdir', self._output_dir,
- self._suite]
- print ' '.join(cmd)
- self._process = subprocess.Popen(cmd,
- shell=os.sep == '\\',
- stdout=monitor_file,
- stderr=monitor_file)
- monitor_file.close()
-
- def wait(self):
- self._rcode = self._process.wait()
- return self._rcode
-
- def report(self):
- if self._rcode == 0:
- LEVEL = '*INFO*'
- STATUS = 'PASS'
- else:
- LEVEL = '*ERROR*'
- STATUS = 'FAIL'
- print '%s[%s] "%s.%s" output (%s)' % (LEVEL, STATUS,
self._suite_name, self._test, self._output)
- print '*HTML* <a href="%s#test_%s.%s">Process log</a>' %
(self._log, self._suite_name, self._test)
=======================================
--- /trunk/proto/parallel/para.txt Thu Mar 3 00:45:24 2011
+++ /trunk/proto/parallel/para.txt Thu Mar 3 15:33:07 2011
@@ -1,5 +1,5 @@
*** Settings ***
-Library parallel.py
+Library Parallel.py pybot
*** Variables ***
${PARALLEL} False
@@ -12,11 +12,12 @@
Bar
: FOR ${i} IN RANGE 1 1000
\ Log hoi(${PARALLEL})
+ Fail
Run all
Log Variables
Log (${PARALLEL})
- ${foo}= Run Parallel Test Foo
- ${bar}= Run Parallel Test Bar
- Wait For ${foo} ${bar}
-
+ Run Parallel Robot Foo ${SUITE SOURCE}
+ Run Parallel Robot Bar ${SUITE SOURCE}
+ Wait For All Parallel Tests To Be Ready
+