[pytango] 229/483: PyTango Test Suite Framework
Sandor Bodo-Merle
sbodomerle-guest at moszumanska.debian.org
Thu Sep 28 19:14:44 UTC 2017
This is an automated email from the git hooks/post-receive script.
sbodomerle-guest pushed a commit to annotated tag bliss_8.10
in repository pytango.
commit 803842cbe3ed0a5ab2589dd5ccb6084f42938ea0
Author: trogucki <trogucki at 4e9c00fd-8f2e-0410-aa12-93ce3db5e235>
Date: Thu Jan 31 20:12:52 2013 +0000
PyTango Test Suite Framework
git-svn-id: http://svn.code.sf.net/p/tango-cs/code/bindings/PyTango/trunk@22011 4e9c00fd-8f2e-0410-aa12-93ce3db5e235
---
tests/TangoRunner.py | 2 +-
tests/TestSuite.py | 191 ++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 168 insertions(+), 25 deletions(-)
diff --git a/tests/TangoRunner.py b/tests/TangoRunner.py
index 32fe246..63cb9e2 100644
--- a/tests/TangoRunner.py
+++ b/tests/TangoRunner.py
@@ -9,6 +9,7 @@ from unittest.signals import registerResult
from unittest import TestCase, TestSuite, suite, util
from copy import deepcopy
+
__unittest = True
# loop parameters
@@ -116,7 +117,6 @@ class TangoTestCase(TestCase):
result.stream.writeln(" --> OK")
return returnResult
-
class _WritelnDecorator(object):
"""Used to decorate file-like objects with a handy 'writeln' method"""
def __init__(self,stream):
diff --git a/tests/TestSuite.py b/tests/TestSuite.py
index 1a2b9ec..cf4f6aa 100644
--- a/tests/TestSuite.py
+++ b/tests/TestSuite.py
@@ -5,33 +5,64 @@ import TangoRunner
import PyTango
from sets import Set
import types
+import os
-# restore points
-_restore_points = Set()
-def restore_hash(cls, name):
- if isinstance(cls, (type, types.ClassType)):
- # the tearDownClass method case
- return cls.__name__ + name
- else:
- # the test methods case
- return cls.__class__.__name__ + name
+# =====================================================================================================================
+# Test suites ---------------------------------------------------------------------------------------------------------
-def restore_set(cls, name):
- _restore_points.add(restore_hash(cls, name))
-
-def restore_unset(cls, name):
- # TODO: consider catching exceptions for silent execution
- _restore_points.remove(restore_hash(cls, name))
-
-def is_restore_set(cls, name):
- return restore_hash(cls, name) in _restore_points
+# Test Case example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+# this is a Test Case which aggregates Test Units of a similar domain
+# append a '__loop' suffix to the Test Case name to execute it multiple times - this will only be done if
+# a '--loopSuite=' command line parameter is defined and has a numeric value
+class TestCaseExample__loop(TangoRunner.TangoTestCase):
+ @classmethod
+ def setUpClass(self):
+ # get command line parameters
+ # parameters are defined by name, e.g. 'myparam' and provided in the command line as '--myparam=myparamvalue'
+ # you can provide description of the parameter but it is optional
+ # values of the parameters are returned as strings
+ # get_param() defines and returns value of a Mandatory Parameter which has to be provided in the command line,
+ # otherwise the execution terminates
+ #self.myparam = get_param('myparam','description of what myparam is')
+ # get_param_opt() defines and returns value of an Optional Parameter which may but does not have to be provided
+ # in the command line
+ #self.myparamopt = get_param_opt('loop','number of times the Unit Test suffixed with "__loop" will be executed')
+ # to correctly process command line parameters always append this line
+ validate_args()
+ @classmethod
+ def tearDownClass(self):
+ if is_restore_set(self, 'my_restore_point'):
+ # fix here what your unit test could break upon unpredicted termination
+ pass
+ # this is a Unit Test, to make the framework interpret a method as a Unit Test append the 'test_' suffix to its name
+ def test_MyUnitTest(self):
+ # set a restore point if you modify the configuration of the device on which the Test Suite is executed
+ # even if the Unit Test terminates unexpectedly, the configuration will be restored if you use
+ # is_restore_set() method in tearDownClass()
+ restore_set(self, 'my_restore_point')
+ # write your test here
+ # to get more information about available assertions refer to unittest tutorial
+ assert(1 == 1)
+ self.assertRegexpMatches('string to contain a word','word')
+ # if you bring all the device configuration to defaults, unset the restore point
+ restore_unset(self, 'my_restore_point')
+
+ # this Unit Test will be executed several times in a loop if the '--loop=' parameter is defined and has a numeric
+ # value; to declare a Unit Test to be executed in a loop append the '__looop' suffix to its name
+ def test_MyUnitTest1__loop(self):
+ assert(True)
+# Attr Misc Test Case ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class AttrMisc(TangoRunner.TangoTestCase):
@classmethod
def setUpClass(self):
- self.device1_name = 'dev/pytomasz/1'
+ # get parameters
+ self.device1_name = get_param('device1','device on which the Test Suite executes')
+ validate_args()
+
self.device1 = PyTango.DeviceProxy(self.device1_name)
@classmethod
@@ -44,9 +75,6 @@ class AttrMisc(TangoRunner.TangoTestCase):
ai.alarms.max_alarm = 'NaN'
self.device1.set_attribute_config(ai)
- def zzztest(self):
- self.device1.state()
-
def test_GetAttributeConfig(self):
ai = self.device1.get_attribute_config('Float_spec_attr_rw')
old_ai = PyTango.AttributeInfoEx(ai)
@@ -134,8 +162,104 @@ class AttrMisc(TangoRunner.TangoTestCase):
da = self.device1.read_attribute('Float_spec_attr_rw')
assert(da.quality == PyTango.AttrQuality.ATTR_VALID)
-
+# End of Test suites --------------------------------------------------------------------------------------------------
+# =====================================================================================================================
+
+
+
+# =====================================================================================================================
+# Restore points internal functions -----------------------------------------------------------------------------------
+
+_restore_points = Set()
+
+def restore_hash(cls, name):
+ if isinstance(cls, (type, types.ClassType)):
+ # the tearDownClass method case
+ return cls.__name__ + name
+ else:
+ # the test methods case
+ return cls.__class__.__name__ + name
+
+def restore_set(cls, name):
+ _restore_points.add(restore_hash(cls, name))
+
+def restore_unset(cls, name):
+ # TODO: consider catching exceptions for silent execution
+ _restore_points.remove(restore_hash(cls, name))
+
+def is_restore_set(cls, name):
+ return restore_hash(cls, name) in _restore_points
+
+# End of Restore points internal functions ----------------------------------------------------------------------------
+# =====================================================================================================================
+
+
+
+# =====================================================================================================================
+# Arguments parsing ---------------------------------------------------------------------------------------------------
+
+params = {}
+params_opt = {}
+args_valid = True
+
+def get_param(param,desc='user defined mandatory parameter'):
+ '''Get mandatory parameters'''
+ if param not in params:
+ params[param] = desc
+ return find_param(param)
+
+def get_param_opt(param,desc='user defined mandatory parameter'):
+ '''Get mandatory parameters'''
+ if param not in params:
+ params_opt[param] = desc
+ return find_param(param)
+
+def validate_args():
+ '''Validate parameters'''
+ global args_valid
+ if args_valid == False:
+ usage = 'Usage: ' + os.path.basename(__file__) + ' '
+ params_str = 'Mandatory Parameters:\n'
+ params_opt_str = 'Optional Parameters:\n'
+ for param in params:
+ usage += '--' + param + '= '
+ params_str += '\t--' + param + '= - ' + params[param] + '\n'
+ for param in params_opt:
+ usage += '[--' + param + '=] '
+ params_opt_str += '\t--' + param + '= - ' + params_opt[param] + '\n'
+ print(usage + '\n')
+ if len(params) != 0:
+ print(params_str)
+ if len(params_opt) != 0:
+ print(params_opt_str)
+ sys.exit(1)
+
+def find_param(param):
+ param_full = '--' + param + '='
+ for arg in sys.argv:
+ if arg[:len(param_full)] == param_full:
+ return arg[len(param_full):]
+ global args_valid
+ args_valid = False
+ return ''
+
+def get_param_if_registered(param):
+ if param in params:
+ return get_param(param)
+ elif param in params_opt:
+ return get_opt_param(param)
+ else:
+ return ''
+
+
+# End of Arguments parsing --------------------------------------------------------------------------------------------
+# =====================================================================================================================
+
+
+
+# =====================================================================================================================
+# Test suite execution ------------------------------------------------------------------------------------------------
if __name__ == '__main__':
# automatically detect tests (alphabetical order)
suites = []
@@ -148,6 +272,25 @@ if __name__ == '__main__':
# tests = [MyTest__loop]
# for test in tests:
# suites.append(unittest.TestLoader().loadTestsFromTestCase(test))
+
+ # check loop parameters
+ def get_loop(param):
+ param_full = '--' + param + '='
+ for arg in sys.argv:
+ if arg[:len(param_full)] == param_full:
+ try:
+ loop_value = int(arg[len(param_full):])
+ except:
+ loop_value = 1
+ return loop_value
+ return 1
+ loopSuite_param = get_loop('loopSuite')
+ loop_param = get_loop('loop')
+
+ # execute the Test Suite
suite = TangoRunner.TangoTestSuite(suites)
- TangoRunner.TangoTestRunner(loopSuite=2, loop=2).run(suite)
\ No newline at end of file
+ TangoRunner.TangoTestRunner(loopSuite=loopSuite_param, loop=loop_param).run(suite)
+
+# End of Test suite execution -----------------------------------------------------------------------------------------
+# =====================================================================================================================
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/pytango.git
More information about the debian-science-commits
mailing list