Best Python code snippet using stestr_python
subunit-trace.py
Source:subunit-trace.py  
...23import testtools24DAY_SECONDS = 60 * 60 * 2425FAILS = []26RESULTS = {}27def cleanup_test_name(name, strip_tags=True, strip_scenarios=False):28    """Clean up the test name for display.29    By default we strip out the tags in the test because they don't help us30    in identifying the test that is run to it's result.31    Make it possible to strip out the testscenarios information (not to32    be confused with tempest scenarios) however that's often needed to33    indentify generated negative tests.34    """35    if strip_tags:36        tags_start = name.find('[')37        tags_end = name.find(']')38        if tags_start > 0 and tags_end > tags_start:39            newname = name[:tags_start]40            newname += name[tags_end + 1:]41            name = newname42    if strip_scenarios:43        tags_start = name.find('(')44        tags_end = name.find(')')45        if tags_start > 0 and tags_end > tags_start:46            newname = name[:tags_start]47            newname += name[tags_end + 1:]48            name = newname49    return name50def get_duration(timestamps):51    start, end = timestamps52    if not start or not end:53        duration = ''54    else:55        delta = end - start56        duration = '%d.%06ds' % (57            delta.days * DAY_SECONDS + delta.seconds, delta.microseconds)58    return duration59def find_worker(test):60    for tag in test['tags']:61        if tag.startswith('worker-'):62            return int(tag[7:])63    return 'NaN'64# Print out stdout/stderr if it exists, always65def print_attachments(stream, test, all_channels=False):66    """Print out subunit attachments.67    Print out subunit attachments that contain content. This68    runs in 2 modes, one for successes where we print out just stdout69    and stderr, and an override that dumps all the attachments.70    """71    channels = ('stdout', 'stderr')72    for name, detail in test['details'].items():73        # NOTE(sdague): the subunit names are a little crazy, and actually74        # are in the form pythonlogging:'' (with the colon and quotes)75        name = name.split(':')[0]76        if detail.content_type.type == 'test':77            detail.content_type.type = 'text'78        if (all_channels or name in channels) and detail.as_text():79            title = "Captured %s:" % name80            stream.write("\n%s\n%s\n" % (title, ('~' * len(title))))81            # indent attachment lines 4 spaces to make them visually82            # offset83            for line in detail.as_text().split('\n'):84                stream.write("    %s\n" % line)85def show_outcome(stream, test, print_failures=False):86    global RESULTS87    status = test['status']88    # TODO(sdague): ask lifeless why on this?89    if status == 'exists':90        return91    worker = find_worker(test)92    name = cleanup_test_name(test['id'])93    duration = get_duration(test['timestamps'])94    if worker not in RESULTS:95        RESULTS[worker] = []96    RESULTS[worker].append(test)97    # don't count the end of the return code as a fail98    if name == 'process-returncode':99        return100    if status == 'success':101        stream.write('{%s} %s [%s] ... ok\n' % (102            worker, name, duration))103        print_attachments(stream, test)104    elif status == 'fail':105        FAILS.append(test)106        stream.write('{%s} %s [%s] ... FAILED\n' % (...test_utils.py
Source:test_utils.py  
...17        test_id_with_attrs = 'test.TestThing.test_thing[attr1,attr2,att3]'18        test_id_with_scenario = 'test.TestThing.test_thing(mysql)'19        test_id_with_attrs_and_scenario = ('test.TestThing.test_thing[attr]'20                                           '(mysql)')21        result_no_attrs = utils.cleanup_test_name(test_id_no_attrs)22        self.assertEqual(test_id_no_attrs, result_no_attrs)23        result_with_attrs = utils.cleanup_test_name(test_id_with_attrs)24        self.assertEqual(test_id_no_attrs, result_with_attrs)25        result_with_scenario = utils.cleanup_test_name(test_id_with_scenario)26        self.assertEqual(test_id_with_scenario, result_with_scenario)27        result_with_attr_and_scenario = utils.cleanup_test_name(28            test_id_with_attrs_and_scenario)29        self.assertEqual(test_id_with_scenario, result_with_attr_and_scenario)30    def test_cleanup_test_name_leave_attrs(self):31        test_id_no_attrs = 'test.TestThing.test_thing'32        test_id_with_attrs = 'test.TestThing.test_thing[attr1,attr2,att3]'33        test_id_with_scenario = 'test.TestThing.test_thing(mysql)'34        test_id_with_attrs_and_scenario = ('test.TestThing.test_thing[attr]'35                                           '(mysql)')36        result_no_attrs = utils.cleanup_test_name(test_id_no_attrs,37                                                  strip_tags=False)38        self.assertEqual(test_id_no_attrs, result_no_attrs)39        result_with_attrs = utils.cleanup_test_name(test_id_with_attrs,40                                                    strip_tags=False)41        self.assertEqual(test_id_with_attrs, result_with_attrs)42        result_with_scenario = utils.cleanup_test_name(test_id_with_scenario,43                                                       strip_tags=False)44        self.assertEqual(test_id_with_scenario, result_with_scenario)45        result_with_attr_and_scenario = utils.cleanup_test_name(46            test_id_with_attrs_and_scenario, strip_tags=False)47        self.assertEqual(test_id_with_attrs_and_scenario,48                         result_with_attr_and_scenario)49    def test_cleanup_test_name_strip_scenario_and_attrs(self):50        test_id_no_attrs = 'test.TestThing.test_thing'51        test_id_with_attrs = 'test.TestThing.test_thing[attr1,attr2,att3]'52        test_id_with_scenario = 'test.TestThing.test_thing(mysql)'53        test_id_with_attrs_and_scenario = ('test.TestThing.test_thing[attr]'54                                           '(mysql)')55        result_no_attrs = utils.cleanup_test_name(test_id_no_attrs,56                                                  strip_scenarios=True)57        self.assertEqual(test_id_no_attrs, result_no_attrs)58        result_with_attrs = utils.cleanup_test_name(test_id_with_attrs,59                                                    strip_scenarios=True)60        self.assertEqual(test_id_no_attrs, result_with_attrs)61        result_with_scenario = utils.cleanup_test_name(test_id_with_scenario,62                                                       strip_scenarios=True)63        self.assertEqual(test_id_no_attrs, result_with_scenario)64        result_with_attr_and_scenario = utils.cleanup_test_name(65            test_id_with_attrs_and_scenario, strip_scenarios=True)66        self.assertEqual(test_id_no_attrs,67                         result_with_attr_and_scenario)68    def test_cleanup_test_name_strip_scenario(self):69        test_id_no_attrs = 'test.TestThing.test_thing'70        test_id_with_attrs = 'test.TestThing.test_thing[attr1,attr2,att3]'71        test_id_with_scenario = 'test.TestThing.test_thing(mysql)'72        test_id_with_attrs_and_scenario = ('test.TestThing.test_thing[attr]'73                                           '(mysql)')74        result_no_attrs = utils.cleanup_test_name(test_id_no_attrs,75                                                  strip_scenarios=True,76                                                  strip_tags=False)77        self.assertEqual(test_id_no_attrs, result_no_attrs)78        result_with_attrs = utils.cleanup_test_name(test_id_with_attrs,79                                                    strip_scenarios=True,80                                                    strip_tags=False)81        self.assertEqual(test_id_with_attrs, result_with_attrs)82        result_with_scenario = utils.cleanup_test_name(test_id_with_scenario,83                                                       strip_scenarios=True,84                                                       strip_tags=False)85        self.assertEqual(test_id_no_attrs, result_with_scenario)86        result_with_attr_and_scenario = utils.cleanup_test_name(87            test_id_with_attrs_and_scenario, strip_scenarios=True,88            strip_tags=False)89        self.assertEqual('test.TestThing.test_thing[attr]',...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
