58 import xml.etree.cElementTree
as etree
62 from random
import shuffle
63 from datetime
import datetime
83 __fieldValueTestCaseSet =
None
100 def __init__(self, ctx, filename, testRunName=None, randomOrder=False):
109 self.
__testRunName__testRunName = testRunName
if testRunName
else str(datetime.now())
112 self.
__changeSet__changeSet.enableAutoRevert(
False)
173 def next(self, verbose=False, saveEachCase=True):
176 failedFieldList=failedFieldList, findAll=
True, verbose=verbose
178 return False, failedFieldList
201 print(
"finishing run")
216 print(
"going to prev test case")
229 print(
"going to case with id ", id)
238 def __setTestCase(self):
275 __fieldValueTestCaseDict =
None
284 self.
__ctx__ctx = context
286 self.
__xmlRoot__xmlRoot = etree.Element(
"FieldValueTestCaseSet")
301 "There are already field-value test cases in this set. Loading not supported in this case!"
305 elif not os.path.isfile(filename):
306 mevis.MLAB.logError(
"File %s does not exist." % (filename))
310 xmlTree = etree.parse(filename)
311 xmlRoot = xmlTree.getroot()
312 if not self.
__verify__verify(xmlRoot):
318 for node
in self.
__xmlRoot__xmlRoot.findall(
"FieldValueTestCase"):
319 testCaseName = node.get(
"name")
329 for node
in self.
__xmlRoot__xmlRoot.findall(
"FieldValueTestCase"):
330 name = node.get(
"name")
351 def add(self, fieldValueTestCase):
353 name = fieldValueTestCase.getName()
355 mevis.MLAB.logError(
"Test with name %s already known!" % (name))
359 self.
__xmlRoot__xmlRoot.append(fieldValueTestCase.save())
371 def copy(self, source, destination):
374 mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (source))
400 mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (name))
424 mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (name))
427 mevis.MLAB.logError(
"FieldValueTestCase with name %s already exists!" % (newName))
436 node.set(
"name", newName)
440 if not fieldValueTestCase
is None:
441 fieldValueTestCase.setName(newName)
454 fieldValueTestCase =
None
456 mevis.MLAB.logError(
"FieldValueTestCase with name %s not found!" % (name))
464 return fieldValueTestCase
469 def __verify(self, xmlRoot):
470 if not xmlRoot.tag ==
"FieldValueTestCaseSet":
473 for fieldValueTestCaseNode
in xmlRoot:
474 if not fieldValueTestCaseNode.tag ==
"FieldValueTestCase":
475 mevis.MLAB.logError(
"Only FieldValueTestCase nodes allowed.")
477 settingsNode = fieldValueTestCaseNode.find(
"Settings")
478 resultsNode = fieldValueTestCaseNode.find(
"Results")
479 if settingsNode
is None or resultsNode
is None:
480 mevis.MLAB.logError(
"Settings and Results node must be defined.")
482 if len(fieldValueTestCaseNode) != 2:
483 mevis.MLAB.logError(
"Wrong number of children (%d instead of 2)." % (len(fieldValueTestCaseNode)))
485 flList = [
False,
False,
False]
486 for fieldListNode
in settingsNode:
487 if fieldListNode.tag ==
"FieldValueList":
488 name = fieldListNode.get(
"name")
489 if name ==
"Parameterization" and not flList[0]:
491 elif name ==
"ExpectedResults" and not flList[1]:
495 "Only FieldValueLists with name Parameterization or ExpectedResults allowed here."
498 elif fieldListNode.tag ==
"FieldList":
499 name = fieldListNode.get(
"name")
500 if name ==
"ResultsToSave" and not flList[2]:
503 mevis.MLAB.logError(
"Only FieldLists with name ResultsToSave allowed here (not %s)." % (name))
507 "Only FieldValueList and FieldList allowed here (not %s)." % (fieldListNode.tag)
510 for child
in fieldListNode:
511 if child.tag !=
"Field":
512 mevis.MLAB.logError(
"There must only be nodes of type Field.")
514 if not (child.get(
"module") !=
None and child.get(
"field") !=
None and child.get(
"type") !=
None):
515 mevis.MLAB.logError(
"Each field must have information on the module, field and type.")
517 if not flList[0]
or not flList[1]
or not flList[2]:
519 "There must be FieldValueLists (with name Parameterization and ExpectedResults) and a FieldList (with name ResultsToSave)."
522 for fieldListNode
in resultsNode:
537 def __getFieldValueTestCaseNode(self, name):
538 for node
in self.
__xmlRoot__xmlRoot.findall(
"FieldValueTestCase"):
539 if node.get(
"name") == name:
541 mevis.MLAB.logError(
"FieldValueTestCase %s not found in XML data-structure!" % (name))
565 __fieldValueListDict =
None
593 def add(self, fieldInfo, index=None):
595 if not index
is None:
598 if index > len(self):
602 return self._add(index, fieldInfo)
612 if index >= 0
and index < len(self):
625 def swap(self, indexA, indexB):
627 if indexA >= 0
and indexA < len(self)
and indexB >= 0
and indexB < len(self)
and indexA != indexB:
629 self[indexA] = self[indexB]
641 for fieldNode
in xmlNode.findall(
"Field"):
642 self.append(self._loadField(fieldNode))
650 xmlNode = etree.Element(self.
_type_type, name=self.
__name__name)
651 for fieldInfo
in self:
652 self._saveField(fieldInfo, xmlNode)
667 FieldValueTestCase.FieldListBase.__init__(self, name)
684 def _add(self, index, fieldInfo):
685 self.insert(index, fieldInfo[0:3])
693 def _loadField(self, xmlNode):
694 return (xmlNode.get(
"module"), xmlNode.get(
"field"), xmlNode.get(
"type"))
702 def _saveField(self, fieldInfo, xmlNode):
703 etree.SubElement(xmlNode,
"Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
715 FieldValueTestCase.FieldListBase.__init__(self, name)
724 fieldType = fieldInfo[2]
725 fieldValue = self.
__convertValue__convertValue(fieldInfo[3], fieldType)
726 self[index] = (fieldInfo[0], fieldInfo[1], fieldType, fieldValue)
734 def _add(self, index, fieldInfo):
735 self.insert(index, fieldInfo)
743 def _loadField(self, xmlNode):
744 fieldType = xmlNode.get(
"type")
745 fieldValue = self.
__convertValue__convertValue(xmlNode.text, fieldType)
746 return (xmlNode.get(
"module"), xmlNode.get(
"field"), fieldType, fieldValue)
754 def _saveField(self, fieldInfo, xmlNode):
755 if fieldInfo[2]
not in (
"Trigger"):
756 etree.SubElement(xmlNode,
"Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2]).text = (
760 etree.SubElement(xmlNode,
"Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
769 def __convertValue(self, value, type):
770 if type ==
"Integer":
772 elif type
in (
"Float",
"Double"):
774 elif "Vector" in type:
775 value = tuple([float(x)
for x
in value[1:-1].split(
",")])
777 value = value ==
"True"
778 elif type ==
"Trigger":
781 if type
not in (
"Enum",
"String"):
782 mevis.MLAB.log(
"Maybe type %s should be supported?!" % (type))
798 self.
__ctx__ctx = ctx
808 self.
__load__load(xmlNode)
837 xmlNode = etree.Element(
"FieldValueTestCase", name=self.
__name__name)
838 settingsNode = etree.SubElement(xmlNode,
"Settings")
839 resultsNode = etree.SubElement(xmlNode,
"Results")
841 settingsNode = xmlNode.find(
"Settings")
842 resultsNode = xmlNode.find(
"Results")
873 fieldName =
"%s.%s" % (item[0], item[1])
874 if item[2]
in (
"Trigger"):
877 changeSet.setFieldValue(fieldName, item[3], verbose=verbose)
900 fieldName =
"%s.%s" % (item[0], item[1])
901 field = self.
__ctx__ctx.field(fieldName)
904 mevis.MLAB.logError(
"Unknown field: %s." % (fieldName))
907 if field.type
in (
"Float",
"Double"):
909 match = abs(field.value - item[3]) < 0.0001
911 match = field.value == item[3]
913 failedFieldList.append(fieldName)
916 "Field %s: Value %s does not match expected value %s!" % (fieldName, field.value, item[3])
942 mevis.MLAB.logError(
"Result with name %s available already." % (name))
947 fldName =
"%s.%s" % (item[0], item[1])
948 if item[2] ==
"Trigger":
952 fldValue = self.
__ctx__ctx.field(fldName).value
953 resList.append(tuple((item[0], item[1], item[2], fldValue)))
972 mevis.MLAB.logError(
"Result with name %s available already." % (name))
978 resToSaveList.append(
"%s.%s" % (item[0], item[1]))
979 for item
in resultList:
980 field =
"%s.%s" % (item[0], item[1])
981 if not field
in resToSaveList:
982 mevis.MLAB.logError(
"Field %s not in list of results to save. Ignoring it." % (field))
995 mevis.MLAB.logError(
"No result with name %s available." % (name))
1004 def __load(self, xmlNode):
1005 node = xmlNode.find(
"Settings")
1006 for fieldValueListNode
in node.findall(
"FieldValueList"):
1007 self.
__fieldValueListDict__fieldValueListDict[fieldValueListNode.get(
"name")].load(fieldValueListNode)
1008 for fieldListNode
in node.findall(
"FieldList"):
1010 for resultNode
in xmlNode.findall(
"Results/FieldValueList"):
1011 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.
def finish(self, verbose=False)
Finish the current test run.
def isFinished(self)
Are there any test cases left to iterate?
def getCurrentCaseName(self)
Return the index of the current case.
def next(self, verbose=False, saveEachCase=True)
Change to the next field-value test case.
def __init__(self, ctx, filename, testRunName=None, randomOrder=False)
The default constructor.
def getNumCases(self)
Return the number of cases of the loaded test case set.
def goToCaseWithId(self, id, verbose=False)
Go to test case with specified id.
def getCurrentIndex(self)
Return the index of the current case.
def getStatusString(self)
Return a string containing the current case id, current case name and number of cases.
def prev(self, verbose=False)
Go back to the previous test case.
A class collecting a set of field-value test cases.
def getList(self)
Return the list of names of the existing field-value test cases.
def remove(self, name)
Remove the field-value test case with the given name.
def __verify(self, xmlRoot)
def save(self, filename)
Save the XML data structure.
def rename(self, name, newName)
Rename a given field-value test case.
def __getFieldValueTestCaseNode(self, name)
def __init__(self, context)
The default constructor.
def load(self, filename)
Try to load the field-value test case set from the given XML file.
def add(self, fieldValueTestCase)
Add the given field-value test case to the FieldValueTestCaseSet.
def copy(self, source, destination)
Create a copy of the given field-value test case with the given new name.
def get(self, name)
Return the field-value test case with the given name.
Superclass for the field lists used in the field-value test case.
def remove(self, index)
Remove the element at the given index.
def __init__(self, name)
Default constructor.
def add(self, fieldInfo, index=None)
Add the field with given info to the list.
def swap(self, indexA, indexB)
Swap the given two elements.
def save(self)
Save the field information to XML.
def load(self, xmlNode)
Load the given xmlNode.
List of field information.
def __init__(self, name)
Default constructor.
def update(self, index, fieldInfo)
Update the values of the given fields.
List of field information including field values.
def update(self, index, fieldInfo)
Update the values of the given fields.
def __init__(self, name)
Default constructor.
def __convertValue(self, value, type)
A class implementing the field-value test cases.
def addSavedResultList(self, name, resultList)
Add saved results.
def getName(self)
Return the name of the field-value test case.
def getSavedResultList(self)
Get a sorted list of names of the saved results.
def saveResults(self, name, overwrite=False)
Save the given field values to the xml data structure.
def __load(self, xmlNode)
def getSavedResult(self, name)
Get the field-value list saved under the given name.
def getExpectedResults(self)
Getter for the list of expected results.
def __init__(self, name, ctx, xmlNode=None)
The default constructor.
def getParameterization(self)
Return the parameterization set.
def save(self, xmlNode=None)
Save the settings of this field-value test case to an XML node.
def applyParameterization(self, changeSet, verbose=False)
Apply the parameterization to the context given in the constructor.
def getResultsToSave(self)
Return the results to save.
def verifyExpectedResults(self, failedFieldList=[], findAll=False, verbose=False)
Use the given field-value list to verify the given context's field values.
def setName(self, name)
Set the name of the field-value test case.
def touch(fieldName, verbose=True)
Touch the given field.