58import xml.etree.cElementTree
as etree
62from random
import shuffle
63from datetime
import datetime
80 __fieldValueTestCaseSet =
None
97 def __init__ (self, ctx, filename, testRunName=None, randomOrder=False):
106 self.
__testRunName = testRunName
if testRunName
else str(datetime.now())
163 def next (self, verbose=False, saveEachCase = True):
165 if not self.
__running or not self.
__testCase.verifyExpectedResults(failedFieldList=failedFieldList, findAll=
True, verbose=verbose):
166 return False, failedFieldList
188 print(
"finishing run")
198 def prev (self, verbose = False):
202 print(
"going to prev test case")
214 print(
"going to case with id ", id)
222 def __setTestCase (self):
256 __fieldValueTestCaseDict =
None
267 self.
__xmlRoot = etree.Element(
'FieldValueTestCaseSet')
280 mevis.MLAB.logError(
"There are already field-value test cases in this set. Loading not supported in this case!")
283 elif not os.path.isfile(filename):
284 mevis.MLAB.logError(
"File %s does not exist." % (filename))
288 xmlTree = etree.parse(filename)
289 xmlRoot = xmlTree.getroot()
296 for node
in self.
__xmlRoot.findall(
'FieldValueTestCase'):
297 testCaseName = node.get(
'name')
306 for node
in self.
__xmlRoot.findall(
'FieldValueTestCase'):
307 name = node.get(
'name')
326 def add (self, fieldValueTestCase):
328 name = fieldValueTestCase.getName()
330 mevis.MLAB.logError(
"Test with name %s already known!" % (name))
334 self.
__xmlRoot.append(fieldValueTestCase.save())
345 def copy (self, source, destination):
348 mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (source))
373 mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (name))
396 mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (name))
399 mevis.MLAB.logError(
"FieldValueTestCase with name %s already exists!" % (newName))
408 node.set(
'name', newName)
412 if not fieldValueTestCase
is None:
413 fieldValueTestCase.setName(newName)
425 fieldValueTestCase =
None
427 mevis.MLAB.logError(
"FieldValueTestCase with name %s not found!" % (name))
435 return fieldValueTestCase
439 def __verify (self, xmlRoot):
440 if not xmlRoot.tag ==
"FieldValueTestCaseSet":
443 for fieldValueTestCaseNode
in xmlRoot:
444 if not fieldValueTestCaseNode.tag ==
"FieldValueTestCase":
445 mevis.MLAB.logError(
"Only FieldValueTestCase nodes allowed.")
447 settingsNode = fieldValueTestCaseNode.find(
"Settings")
448 resultsNode = fieldValueTestCaseNode.find(
"Results")
449 if settingsNode
is None or resultsNode
is None:
450 mevis.MLAB.logError(
"Settings and Results node must be defined.")
452 if len(fieldValueTestCaseNode) != 2:
453 mevis.MLAB.logError(
"Wrong number of children (%d instead of 2)." % (len(fieldValueTestCaseNode)))
455 flList = [
False,
False,
False]
456 for fieldListNode
in settingsNode:
457 if fieldListNode.tag ==
"FieldValueList":
458 name = fieldListNode.get(
"name")
459 if name ==
"Parameterization" and not flList[0]:
461 elif name ==
"ExpectedResults" and not flList[1]:
464 mevis.MLAB.logError(
"Only FieldValueLists with name Parameterization or ExpectedResults allowed here.")
466 elif fieldListNode.tag ==
"FieldList":
467 name = fieldListNode.get(
"name")
468 if name ==
"ResultsToSave" and not flList[2]:
471 mevis.MLAB.logError(
"Only FieldLists with name ResultsToSave allowed here (not %s)." % (name))
474 mevis.MLAB.logError(
"Only FieldValueList and FieldList allowed here (not %s)." % (fieldListNode.tag))
476 for child
in fieldListNode:
477 if child.tag !=
"Field":
478 mevis.MLAB.logError(
"There must only be nodes of type Field.")
480 if not (child.get(
"module") !=
None and child.get(
"field") !=
None and child.get(
"type") !=
None):
481 mevis.MLAB.logError(
"Each field must have information on the module, field and type.")
483 if not flList[0]
or not flList[1]
or not flList[2]:
484 mevis.MLAB.logError(
"There must be FieldValueLists (with name Parameterization and ExpectedResults) and a FieldList (with name ResultsToSave).")
486 for fieldListNode
in resultsNode:
500 def __getFieldValueTestCaseNode (self, name):
501 for node
in self.
__xmlRoot.findall(
'FieldValueTestCase'):
502 if node.get(
'name') == name:
504 mevis.MLAB.logError(
"FieldValueTestCase %s not found in XML data-structure!" % (name))
524 __fieldValueListDict =
None
551 def add (self, fieldInfo, index=None):
553 if not index
is None:
556 if index > len(self):
560 return self._add(index, fieldInfo)
569 if index >= 0
and index < len(self):
581 def swap (self, indexA, indexB):
583 if indexA>=0
and indexA<len(self)
and indexB>=0
and indexB<len(self)
and indexA!=indexB:
585 self[indexA] = self[indexB]
596 for fieldNode
in xmlNode.findall(
'Field'):
597 self.append(self._loadField(fieldNode))
604 xmlNode = etree.Element(self.
_type, name=self.
__name)
605 for fieldInfo
in self:
606 self._saveField(fieldInfo, xmlNode)
619 FieldValueTestCase.FieldListBase.__init__(self, name)
634 def _add (self, index, fieldInfo):
635 self.insert(index, fieldInfo[0:3])
643 return (xmlNode.get(
"module"), xmlNode.get(
"field"), xmlNode.get(
"type"))
651 etree.SubElement(xmlNode,
'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
661 FieldValueTestCase.FieldListBase.__init__(self, name)
669 fieldType = fieldInfo[2]
671 self[index] = (fieldInfo[0], fieldInfo[1], fieldType, fieldValue)
678 def _add (self, index, fieldInfo):
679 self.insert(index, fieldInfo)
687 fieldType = xmlNode.get(
"type")
689 return (xmlNode.get(
"module"), xmlNode.get(
"field"), fieldType, fieldValue)
697 if fieldInfo[2]
not in (
'Trigger'):
698 etree.SubElement(xmlNode,
'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2]).text = str(fieldInfo[3])
700 etree.SubElement(xmlNode,
'Field', module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
708 def __convertValue (self, value, type):
709 if type ==
"Integer":
711 elif type
in (
'Float',
'Double'):
713 elif 'Vector' in type:
714 value = tuple([float(x)
for x
in value[1:-1].split(
',')])
716 value = value==
"True"
717 elif type ==
'Trigger':
720 if type
not in (
'Enum',
'String'):
721 mevis.MLAB.log(
"Maybe type %s should be supported?!" % (type))
769 xmlNode = etree.Element(
'FieldValueTestCase', name=self.
__name)
770 settingsNode = etree.SubElement(xmlNode,
'Settings')
771 resultsNode = etree.SubElement(xmlNode,
'Results')
773 settingsNode = xmlNode.find(
'Settings')
774 resultsNode = xmlNode.find(
'Results')
803 fieldName =
"%s.%s" % (item[0], item[1])
804 if item[2]
in (
"Trigger"):
805 self.
__ctx.field(fieldName).touch()
807 changeSet.setFieldValue(fieldName, item[3], verbose=verbose)
828 fieldName =
"%s.%s" % (item[0], item[1])
829 field = self.
__ctx.field(fieldName)
832 mevis.MLAB.logError(
"Unknown field: %s." % (fieldName))
835 if field.type
in (
"Float",
"Double"):
837 match = abs(field.value-item[3]) < 0.0001
839 match = field.value == item[3]
841 failedFieldList.append(fieldName)
843 mevis.MLAB.logError(
"Field %s: Value %s does not match expected value %s!" % (fieldName, field.value, item[3]))
866 mevis.MLAB.logError(
"Result with name %s available already." % (name))
871 fldName =
"%s.%s" % (item[0], item[1])
872 if item[2] ==
"Trigger":
873 self.
__ctx.field(fldName).touch()
876 fldValue = self.
__ctx.field(fldName).value
877 resList.append(tuple((item[0], item[1], item[2], fldValue)))
894 mevis.MLAB.logError(
"Result with name %s available already." % (name))
900 resToSaveList.append(
"%s.%s" % (item[0], item[1]))
901 for item
in resultList:
902 field =
"%s.%s" % (item[0], item[1])
903 if not field
in resToSaveList:
904 mevis.MLAB.logError(
"Field %s not in list of results to save. Ignoring it." % (field))
916 mevis.MLAB.logError(
'No result with name %s available.' % (name))
924 def __load (self, xmlNode):
925 node = xmlNode.find(
'Settings')
926 for fieldValueListNode
in node.findall(
'FieldValueList'):
928 for fieldListNode
in node.findall(
'FieldList'):
930 for resultNode
in xmlNode.findall(
'Results/FieldValueList'):
931 name = resultNode.get(
'name')
Class to handle field changes and make them revertable.
A class to iterate over the list of field-value test cases of a given set.
next(self, verbose=False, saveEachCase=True)
Change to the next field-value test case.
goToCaseWithId(self, id, verbose=False)
Go to test case with specified id.
getStatusString(self)
Return a string containing the current case id, current case name and number of cases.
getNumCases(self)
Return the number of cases of the loaded test case set.
getCurrentIndex(self)
Return the index of the current case.
prev(self, verbose=False)
Go back to the previous test case.
isFinished(self)
Are there any test cases left to iterate?
getCurrentCaseName(self)
Return the index of the current case.
__init__(self, ctx, filename, testRunName=None, randomOrder=False)
The default constructor.
finish(self, verbose=False)
Finish the current test run.
A class collecting a set of field-value test cases.
get(self, name)
Return the field-value test case with the given name.
save(self, filename)
Save the XML data structure.
load(self, filename)
Try to load the field-value test case set from the given XML file.
getList(self)
Return the list of names of the existing field-value test cases.
rename(self, name, newName)
Rename a given field-value test case.
copy(self, source, destination)
Create a copy of the given field-value test case with the given new name.
__getFieldValueTestCaseNode(self, name)
add(self, fieldValueTestCase)
Add the given field-value test case to the FieldValueTestCaseSet.
remove(self, name)
Remove the field-value test case with the given name.
__init__(self, context)
The default constructor.
Superclass for the field lists used in the field-value test case.
save(self)
Save the field information to XML.
add(self, fieldInfo, index=None)
Add the field with given info to the list.
remove(self, index)
Remove the element at the given index.
load(self, xmlNode)
Load the given xmlNode.
swap(self, indexA, indexB)
Swap the given two elements.
__init__(self, name)
Default constructor.
_type
The type of list used.
List of field information.
update(self, index, fieldInfo)
Update the values of the given fields.
_saveField(self, fieldInfo, xmlNode)
Save the given field information to the given XML node.
_loadField(self, xmlNode)
Load field information from the given XML node.
_add(self, index, fieldInfo)
Add the values of the given fields.
__init__(self, name)
Default constructor.
List of field information including field values.
__init__(self, name)
Default constructor.
_add(self, index, fieldInfo)
Add the values of the given fields.
update(self, index, fieldInfo)
Update the values of the given fields.
_loadField(self, xmlNode)
Load field information from the given XML node.
_saveField(self, fieldInfo, xmlNode)
Save the given field information to the given XML node.
__convertValue(self, value, type)
A class implementing the field-value test cases.
verifyExpectedResults(self, failedFieldList=[], findAll=False, verbose=False)
Return the expected result set.
__init__(self, name, ctx, xmlNode=None)
The default constructor.
addSavedResultList(self, name, resultList)
Add saved results.
setName(self, name)
Set the name of the field-value test case.
getSavedResultList(self)
Get a sorted list of names of the saved results.
getResultsToSave(self)
Return the results to save.
getExpectedResults(self)
Getter for the list of expected results.
getSavedResult(self, name)
Get the field-value list saved under the given name.
save(self, xmlNode=None)
Save the settings of this field-value test case to an XML node.
applyParameterization(self, changeSet, verbose=False)
Apply the parameterization to the context given in the constructor.
saveResults(self, name, overwrite=False)
Save the given field values to the xml data structure.
getParameterization(self)
Return the parameterization set.
getName(self)
Return the name of the field-value test case.