How to use tapItem method in fMBT

Best Python code snippet using fMBT_python

tap.py

Source:tap.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2'''3This module provide test runners for JS in Django.4'''5from __future__ import unicode_literals6import re7from django.utils import termcolors8from django.utils.encoding import python_2_unicode_compatible9green = termcolors.make_style(fg='green', opts=('bold',))10red = termcolors.make_style(fg='red', opts=('bold',))11# TAP regex12TAP_MODULE_REGEX = re.compile(r'^(?P<indent>\s*)# module: (?P<name>.*)$')13TAP_TEST_REGEX = re.compile(r'^(?P<indent>\s*)# test: (?P<name>.*)$')14TAP_ASSERTION_REGEX = re.compile(r'^(?P<indent>\s*)(?P<type>(?:not )?ok) (?P<num>\d+)(?: - (?P<details>.*))?$')15TAP_STACK_REGEX = re.compile(r'^(?P<indent>\s*)#\s+at\s+(?P<stack>.*)$')16TAP_END_REGEX = re.compile(r'^(?P<indent>\s*)(?P<start>\d+)\.\.(?P<end>\d+)(?: - (?P<details>.*))?$')17TAP_DETAILS_REGEX = re.compile(18 r'''^(?:(?P<message>(?!expected)(?!got)(?!matcher)(?!source).+?)(?:, )?)?'''19 r'''(?:expected: '(?P<expected>.+)', got: '(?P<got>.+?)'(?:, )?)?'''20 r'''(?:matcher: '(?P<matcher>.+?)'(?:, )?)?'''21 r'''(?:source:\s+at\s+(?P<source>.+?))?$''')22# Output format23INDENT = 424DEFAULT_ENCODING = 'utf-8'25class TapItem(object):26 parent = None27 parsed_indent = ''28 @property29 def indent(self):30 if self.parent and isinstance(self.parent, TapItem):31 if isinstance(self.parent, TapModule):32 return self.parent.indent + ' ' * INDENT33 else:34 return self.parent.indent35 return ''36@python_2_unicode_compatible37class TapGroup(list, TapItem):38 def __init__(self, name='', parent=None, parsed_indent='', *args, **kwargs):39 super(TapGroup, self).__init__(*args, **kwargs)40 self.name = name41 self.parent = parent42 self.parsed_indent = parsed_indent43 def __str__(self):44 return '# Groupe %s' % self.name45 def __nonzero__(self):46 return True47 def __bool__(self):48 return True49 def append(self, item):50 if isinstance(item, (TapGroup, TapAssertion)) and not item.parent:51 item.parent = self52 super(TapGroup, self).append(item)53 def get_all_failures(self):54 failures = []55 for item in self:56 if isinstance(item, TapGroup):57 failures.extend(item.get_all_failures())58 elif isinstance(item, TapAssertion) and not item.success:59 failures.append(item)60 return failures61@python_2_unicode_compatible62class TapModule(TapGroup):63 def display(self):64 return '%s%s' % (self.indent, self.name)65 def __str__(self):66 return '# module: %s' % self.name67 @classmethod68 def parse(cls, line):69 match = TAP_MODULE_REGEX.match(line.rstrip())70 if match:71 return cls(72 match.group('name').strip(),73 parsed_indent=match.group('indent')74 )75 else:76 return None77@python_2_unicode_compatible78class TapTest(TapGroup):79 def display(self):80 assertions = [result.display(True) for result in self]81 if assertions:82 return '%s%s (%s)' % (self.indent, self.name, ' '.join(assertions))83 else:84 return '%s%s' % (self.indent, self.name)85 def __str__(self):86 return '# test: %s' % self.name87 @classmethod88 def parse(cls, line):89 match = TAP_TEST_REGEX.match(line.rstrip())90 if match:91 return cls(match.group('name').strip(), parsed_indent=match.group('indent'))92 else:93 return None94@python_2_unicode_compatible95class TapAssertion(TapItem):96 def __init__(self, num, success=True, message=None, parsed_indent='', *args, **kwargs):97 super(TapAssertion, self).__init__()98 self.num = num99 self.success = success100 self.message = message101 self.expected = None102 self.got = None103 self.matcher = None104 self.stack = []105 self.parsed_indent = parsed_indent106 def display(self, inline=False):107 if inline:108 return green('ok') if self.success else red('ko')109 else:110 text = self.indent111 if self.success:112 text += green('ok %s' % self.num)113 else:114 text += red('not ok %s' % self.num)115 text = '%s - %s' % (text, self.message) if self.message else text116 if self.expected is not None and self.got is not None:117 text = '\n'.join([text, '# expected: %s' % self.expected, '# got: %s' % self.got])118 if self.stack:119 text = '\n'.join([text] + ['# stack: %s' % line for line in self.stack])120 return text121 def __str__(self):122 return 'ok %s' % self.num if self.success else 'not ok %s' % self.num123 @classmethod124 def parse(cls, line):125 match = TAP_ASSERTION_REGEX.match(line.rstrip())126 if match:127 assertion = cls(128 int(match.group('num')),129 match.group('type') == 'ok',130 parsed_indent=match.group('indent')131 )132 if match.group('details'):133 details_match = TAP_DETAILS_REGEX.match(match.group('details'))134 if details_match and details_match.group('message'):135 assertion.message = details_match.group('message')136 if details_match and details_match.group('expected'):137 assertion.expected = details_match.group('expected')138 assertion.got = details_match.group('got')139 if details_match and details_match.group('matcher'):140 assertion.matcher = details_match.group('matcher')141 if details_match and details_match.group('source'):142 assertion.stack = [details_match.group('source')]143 return assertion144 else:145 return None146HIERARCHY = (147 TapModule,148 TapTest,149 TapAssertion,150)151def hierarchy(item):152 if not isinstance(item, TapItem):153 raise ValueError('Item should be a TapItem')154 return HIERARCHY.index(item.__class__)155class TapParser(object):156 '''157 A TAP parser class reading from iterable TAP lines.158 '''159 def __init__(self, yield_class=TapTest, debug=False):160 if not issubclass(yield_class, TapItem):161 raise ValueError('yield class should extend TapItem')162 self.suites = TapGroup()163 self.current = self.suites164 self.yield_class = yield_class165 self.debug = debug166 def parse(self, lines):167 for line in lines:168 for item in self.parse_line(line):169 yield item170 for item in self.get_lasts():171 yield item172 def parse_line(self, line):173 item = TapModule.parse(line) or TapTest.parse(line) or TapAssertion.parse(line)174 if item is not None:175 return self.set_current(item)176 match = TAP_STACK_REGEX.match(line.rstrip())177 if match:178 self.current.stack.append(match.group('stack'))179 return []180 match = TAP_END_REGEX.match(line.rstrip())181 if match and self.debug:182 print('# end %s-%s' % (match.group('start'), match.group('end')))183 return []184 if line and self.debug:185 print('not matched: %s' % line)186 return []187 def set_current(self, item=None):188 if item and not isinstance(item, TapItem):189 raise ValueError('Should be a TAP item')190 ended = []191 while self.current.parent and hierarchy(item) < hierarchy(self.current):192 if isinstance(self.current, self.yield_class):193 ended.append(self.current)194 self.current = self.current.parent195 while self.current.parent \196 and hierarchy(item) == hierarchy(self.current) \197 and len(item.parsed_indent) <= len(self.current.parsed_indent):198 if isinstance(self.current, self.yield_class):199 ended.append(self.current)200 self.current = self.current.parent201 self.current.append(item)202 self.current = item203 if hierarchy(self.current) < HIERARCHY.index(self.yield_class):204 ended.append(self.current)205 return ended206 def get_lasts(self):207 lasts = []208 while isinstance(self.current, TapItem) and self.current.parent:209 if isinstance(self.current, self.yield_class):210 lasts.append(self.current)211 self.current = self.current.parent...

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 fMBT 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