def _sortByLayers(self, suite, suiteClass): top = suiteClass() # first find all of the layers mentioned layers = OrderedDict() for test in self._flatten(suite): # split tests up into buckets by layer layer = getattr(test, 'layer', None) if layer: layers.setdefault(layer, LayerSuite(layer=layer)).addTest(test) else: top.addTest(test) # then organize layers into a tree remaining = list(layers.keys()) seen = set() tree = {} while remaining: ly = remaining.pop() if ly in seen: continue seen.add(ly) # superclasses of this layer if ly is None: deps = [] else: deps = [ cls for cls in util.bases_and_mixins(ly) if cls is not object ] deps.reverse() if not deps: # layer is top-level self._addToTree(tree, ly, None) else: outer = ly while deps: inner, outer = outer, deps.pop() self._addToTree(tree, inner, outer) if outer not in layers: remaining.append(outer) layers[outer] = LayerSuite(layer=outer) # finally build the top-level suite self._treeToSuite(tree, None, top, layers) # printtree(top) return top
def get_parent_layers(self, layers_dict): while True: missing_parents = [] for layer in layers_dict.keys(): for parent in layer.__bases__: if parent is object: continue if parent not in layers_dict: missing_parents.append(parent) if not missing_parents: break for parent in missing_parents: layers_dict[parent] = LayerSuite(self.session, layer=parent)
def get_layers_from_suite(self, suite, suiteClass): top_layer = suiteClass() layers_dict = OrderedDict() for test in self.flatten_suite(suite): layer = getattr(test, "layer", None) if layer: if layer not in layers_dict: layers_dict[layer] = LayerSuite(self.session, layer=layer) layers_dict[layer].addTest(test) else: top_layer.addTest(test) self.get_parent_layers(layers_dict) return top_layer, layers_dict
def make_suite(self, suite, suiteClass): top_layer, layers_dict = self.get_layers_from_suite(suite, suiteClass) tree = {} unresolved_layers = self.update_layer_tree(tree, layers_dict.keys()) while unresolved_layers: remaining = self.update_layer_tree(tree, unresolved_layers) if len(remaining) == len(unresolved_layers): raise exceptions.LoadTestsFailure( "Could not resolve layer dependencies") unresolved_layers = remaining for layer in tree.keys(): if layer and layer not in layers_dict: layers_dict[layer] = LayerSuite(self.session, layer=layer) self.tree_to_suite(tree, None, top_layer, layers_dict) return top_layer