def count(self): """ Return the number of test cases to be created. """ if self._count is None: text = self.fetch_text(self._root, "Count") try: self._count = int(text) except Exception: message = "Text in Count element is not a number - '" + text + "'" raise SuiteDreamsException(message) if self._count < 0: message = "Count must not be a negative number - " + text raise SuiteDreamsException(message) return self._count
def title(self, content): """ Set the value of the title. Argument: content - the title of the test page """ if content is None: message = "Title of test case must not be None" raise SuiteDreamsException(message) if len(content) == 0: message = "Title of test case must not be empty" raise SuiteDreamsException(message) self._title = content return
def parse(self): """ Parse the product spec file """ if not ProductSpec.file_exists(self.spec_file_name): raise SuiteDreamsException("Product spec file does not exist - " + self.spec_file_name) try: tree = Et.parse(self.spec_file_name) root = tree.getroot() tag = root.tag if tag != "TestSuite": raise SuiteDreamsException("Root element is not TestSuite - " + tag) self._root = root except Exception as e: raise SuiteDreamsException(str(e)) return
def seed(self): """Return the seed for the random number generator""" if self._seed is None: value = self.fetch_text(self._root, "Seed") try: self._seed = int(value) except ValueError: message = "Value for seed is not a valid integer - " + value raise SuiteDreamsException(message) return self._seed
def is_coverable_created(coverage_element): """Return true if the coverage is to be created""" select = coverage_element.get("selection") if select is None: result = False elif select == "create": result = True elif select == "select": result = False else: message = "Invalid value for select attribute on <Coverable> element - " + select raise SuiteDreamsException(message) return result
def fetch_weight(element): """ Return the value of the weight attribute as an integer. This is an integer between 0 and 100 inclusive. Argument: element - an XML element that may or may not have a weight attribute """ assert element is not None, "fetch_weight: element must not be None" value = element.get("weight", default="100") try: weight = int(value) except ValueError: message = "Illegal value for weight attribute in element " message += element.tag message += " - " + value raise SuiteDreamsException(message) if weight < 0 or weight > 100: message = "Weight on element " message += element.tag message += " must be between 0 and 100 inclusive, not - " + value raise SuiteDreamsException(message) return weight
def process_coverage_term(self, coverage_term_element): """ Process the coverage term element. Argument: coverage_term_element - a <CoverageTerm> element """ coverage_term_code = self.product_spec.fetch_text(coverage_term_element, "CoverageTermCode") term_elements = self.product_spec.fetch_all_elements(coverage_term_element, "Term") if len(term_elements) == 0: message = "Coverage term must have terms - " + coverage_term_code raise SuiteDreamsException(message) value = self.process_values(coverage_term_element, coverage_term_code, "Term") self.add_with(coverage_term_code, value) return
def process_question_set(self, question_set_element): """ Output the questions from a questions set. A question set is a grouping of questions. Argument: question_set_element - a <QuestionSet> element """ question_set_code = self.product_spec.fetch_text(question_set_element, "QuestionSetCode") question_elements = self.product_spec.fetch_all_elements(question_set_element, "Question") if len(question_elements) == 0: message = "Question set does not have any questions - " + question_set_code raise SuiteDreamsException(message) for question_element in question_elements: self.process_question(question_element, question_set_code) return
def output(self): """ Output the HTML to its file. """ file = None try: file = open(self._filename, 'w') result = TestCase.prettify(self._html) file.write(result) except Exception as e: raise SuiteDreamsException(e) finally: if file is not None: file.close() return
def add_create_umbrella(self, entity_type, limit): """ Add a line for: create umbrella <entity_type> with <limit> Arguments: entity_type - one of: exposure, question, member, driver limit - the limit for the umbrella coverage """ values = ["exposure", "question", "member", "driver", "policy"] if entity_type not in values: raise SuiteDreamsException("Umbrella entity type is invalid -" + entity_type) row = ["create", "umbrella", entity_type, "with", limit] self.test_case.add_row(row) return
def process_coverables(self, product_element): """ Process the sset of coverables. Arguments: product_element - the <Product> element """ coverable_elements = self.product_spec.fetch_all_elements(product_element, "Coverable") if len(coverable_elements) == 0: message = "Product must have at least one coverable" raise SuiteDreamsException(message) for coverable_element in coverable_elements: selector = self.random_selector if FileBuilder.select_element(coverable_element, selector): self.process_coverable(coverable_element) return
def validate_test_suite_dir(test_suite_library, suite_name): """ Verify that the test suite directory does not exist yeet. Then create it. Arguments: test_suite_library - the diretory that holds test suites. suite_name - the name of the test suite. This will be the name of the subdirectory in the test suite library """ if suite_name is None or len(suite_name) == 0: raise SuiteDreamsException( "Test suite name must not be None or an empty string") test_suite_dir = test_suite_library + "/" + suite_name path = Path(test_suite_dir) if path.exists(): print("Test suite directory already exists - " + test_suite_dir) else: path.mkdir(mode=0o777, parents=False, exist_ok=False) return
def fetch_element(parent, tag): """ Return the single element named in the tag argument. This method should be used only when only one element with this name is used. If the element is not found, an exception is thrown. Argument: parent - the parent of the element being searched for tag - the name of the element to be retrieved Returns: The element being searched for. """ assert tag is not None, "fetch_element: Tag must not be None" assert len(tag) > 0, "fetch_element: Tag must not be an empty string" assert parent is not None, "fetch_element: Parent of element " + tag + " must not be None" element = parent.find(tag) if element is None: message = "fetch_element: Element " + tag + " was not found in element " + parent.tag raise SuiteDreamsException(message) return element
def process_values(self, property_element, property_name, element_name): """ Select a value from a list of values of a property. Argument: property_element - the property element containig the values property_name - the name of the property or question element_name - the name of the element containing values """ assert element_name is not None, "process_values: element name must not be None" assert len(element_name) > 0, "process_values: element name must not be an empty string" selector = self.random_selector value_elements = self.product_spec.fetch_all_elements(property_element, element_name) sum_weights = 0 for value_element in value_elements: weight = FileBuilder.fetch_weight(value_element) sum_weights += weight if selector <= sum_weights: return value_element.text message = "No values were selected for - " + property_name + ". Element being searched is - " + element_name raise SuiteDreamsException(message)