How to use test_regex method in localstack

Best Python code snippet using localstack_python

logparser.py

Source:logparser.py Github

copy

Full Screen

1#2# SPDX-License-Identifier: MIT3#4import sys5import os6import re7# A parser that can be used to identify weather a line is a test result or a section statement.8class PtestParser(object):9 def __init__(self):10 self.results = {}11 self.sections = {}12 def parse(self, logfile):13 test_regex = {}14 test_regex['PASSED'] = re.compile(r"^PASS:(.+)")15 test_regex['FAILED'] = re.compile(r"^FAIL:([^(]+)")16 test_regex['SKIPPED'] = re.compile(r"^SKIP:(.+)")17 section_regex = {}18 section_regex['begin'] = re.compile(r"^BEGIN: .*/(.+)/ptest")19 section_regex['end'] = re.compile(r"^END: .*/(.+)/ptest")20 section_regex['duration'] = re.compile(r"^DURATION: (.+)")21 section_regex['exitcode'] = re.compile(r"^ERROR: Exit status is (.+)")22 section_regex['timeout'] = re.compile(r"^TIMEOUT: .*/(.+)/ptest")23 # Cache markers so we don't take the re.search() hit all the time.24 markers = ("PASS:", "FAIL:", "SKIP:", "BEGIN:", "END:", "DURATION:", "ERROR: Exit", "TIMEOUT:")25 def newsection():26 return { 'name': "No-section", 'log': [] }27 current_section = newsection()28 with open(logfile, errors='replace') as f:29 for line in f:30 if not line.startswith(markers):31 current_section['log'].append(line)32 continue33 result = section_regex['begin'].search(line)34 if result:35 current_section['name'] = result.group(1)36 continue37 result = section_regex['end'].search(line)38 if result:39 if current_section['name'] != result.group(1):40 bb.warn("Ptest END log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))41 if current_section['name'] in self.sections:42 bb.warn("Ptest duplicate section for %s" % (current_section['name']))43 self.sections[current_section['name']] = current_section44 del self.sections[current_section['name']]['name']45 current_section = newsection()46 continue47 result = section_regex['timeout'].search(line)48 if result:49 if current_section['name'] != result.group(1):50 bb.warn("Ptest TIMEOUT log section mismatch %s vs. %s" % (current_section['name'], result.group(1)))51 current_section['timeout'] = True52 continue53 for t in ['duration', 'exitcode']:54 result = section_regex[t].search(line)55 if result:56 current_section[t] = result.group(1)57 continue58 current_section['log'].append(line)59 for t in test_regex:60 result = test_regex[t].search(line)61 if result:62 if current_section['name'] not in self.results:63 self.results[current_section['name']] = {}64 self.results[current_section['name']][result.group(1).strip()] = t65 # Python performance for repeatedly joining long strings is poor, do it all at once at the end.66 # For 2.1 million lines in a log this reduces 18 hours to 12s.67 for section in self.sections:68 self.sections[section]['log'] = "".join(self.sections[section]['log'])69 return self.results, self.sections70 # Log the results as files. The file name is the section name and the contents are the tests in that section.71 def results_as_files(self, target_dir):72 if not os.path.exists(target_dir):73 raise Exception("Target directory does not exist: %s" % target_dir)74 for section in self.results:75 prefix = 'No-section'76 if section:77 prefix = section78 section_file = os.path.join(target_dir, prefix)79 # purge the file contents if it exists80 with open(section_file, 'w') as f:81 for test_name in sorted(self.results[section]):82 status = self.results[section][test_name]83 f.write(status + ": " + test_name + "\n")84# ltp log parsing85class LtpParser(object):86 def __init__(self):87 self.results = {}88 self.section = {'duration': "", 'log': ""}89 def parse(self, logfile):90 test_regex = {}91 test_regex['PASSED'] = re.compile(r"PASS")92 test_regex['FAILED'] = re.compile(r"FAIL")93 test_regex['SKIPPED'] = re.compile(r"SKIP")94 with open(logfile, errors='replace') as f:95 for line in f:96 for t in test_regex:97 result = test_regex[t].search(line)98 if result:99 self.results[line.split()[0].strip()] = t100 for test in self.results:101 result = self.results[test]102 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))103 return self.results, self.section104# ltp Compliance log parsing105class LtpComplianceParser(object):106 def __init__(self):107 self.results = {}108 self.section = {'duration': "", 'log': ""}109 def parse(self, logfile):110 test_regex = {}111 test_regex['PASSED'] = re.compile(r"^PASS")112 test_regex['FAILED'] = re.compile(r"^FAIL")113 test_regex['SKIPPED'] = re.compile(r"(?:UNTESTED)|(?:UNSUPPORTED)")114 section_regex = {}115 section_regex['test'] = re.compile(r"^Testing")116 with open(logfile, errors='replace') as f:117 for line in f:118 result = section_regex['test'].search(line)119 if result:120 self.name = ""121 self.name = line.split()[1].strip()122 self.results[self.name] = "PASSED"123 failed = 0124 failed_result = test_regex['FAILED'].search(line)125 if failed_result:126 failed = line.split()[1].strip()127 if int(failed) > 0:128 self.results[self.name] = "FAILED"129 for test in self.results:130 result = self.results[test]131 self.section['log'] = self.section['log'] + ("%s: %s\n" % (result.strip()[:-2], test.strip()))...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful