Best Python code snippet using pytest-bdd_python
cucumber_json.py
Source:cucumber_json.py
...56 elif report.skipped:57 result = {"status": "skipped"}58 result['duration'] = long(math.floor((10 ** 9) * step["duration"])) # nanosec59 return result60 def _serialize_tags(self, item):61 """Serialize item's tags.62 :param item: json-serialized `Scenario` or `Feature`.63 :return: `list` of `dict` in the form of:64 [65 {66 "name": "<tag>",67 "line": 2,68 }69 ]70 """71 return [72 {73 "name": tag,74 "line": item["line_number"] - 175 }76 for tag in item["tags"]77 ]78 def pytest_runtest_logreport(self, report):79 try:80 scenario = report.scenario81 except AttributeError:82 # skip reporting for non-bdd tests83 return84 if not scenario["steps"] or report.when != "call":85 # skip if there isn't a result or scenario has no steps86 return87 def stepmap(step):88 error_message = False89 if step['failed'] and not scenario.setdefault('failed', False):90 scenario['failed'] = True91 error_message = True92 return {93 "keyword": step['keyword'],94 "name": step['name'],95 "line": step['line_number'],96 "match": {97 "location": "",98 },99 "result": self._get_result(step, report, error_message),100 }101 if scenario["feature"]["filename"] not in self.features:102 self.features[scenario["feature"]["filename"]] = {103 "keyword": "Feature",104 "uri": scenario["feature"]["rel_filename"],105 "name": scenario["feature"]["name"] or scenario["feature"]["rel_filename"],106 "id": scenario["feature"]["rel_filename"].lower().replace(" ", "-"),107 "line": scenario['feature']["line_number"],108 "description": scenario["feature"]["description"],109 "tags": self._serialize_tags(scenario["feature"]),110 "elements": [],111 }112 self.features[scenario["feature"]["filename"]]["elements"].append({113 "keyword": "Scenario",114 "id": report.item["name"],115 "name": scenario["name"],116 "line": scenario["line_number"],117 "description": "",118 "tags": self._serialize_tags(scenario),119 "type": "scenario",120 "steps": [stepmap(step) for step in scenario["steps"]],121 })122 def pytest_sessionstart(self):123 self.suite_start_time = time.time()124 def pytest_sessionfinish(self):125 if py.std.sys.version_info[0] < 3:126 logfile_open = py.std.codecs.open127 else:128 logfile_open = open129 with logfile_open(self.logfile, "w", encoding="utf-8") as logfile:130 logfile.write(json.dumps(list(self.features.values())))131 def pytest_terminal_summary(self, terminalreporter):132 terminalreporter.write_sep("-", "generated json file: %s" % (self.logfile))
mapping.py
Source:mapping.py
...3import ciso86014from .common import *5def serialize(point: Mapping, measurement=None, **extra_tags) -> bytes:6 """Converts dictionary-like data into a single line protocol line (point)"""7 tags = _serialize_tags(point, extra_tags)8 return (9 f'{_serialize_measurement(point, measurement)}'10 f'{"," if tags else ""}{tags} '11 f'{_serialize_fields(point)} '12 f'{_serialize_timestamp(point)}'13 ).encode()14def _serialize_measurement(point, measurement):15 try:16 return escape(point['measurement'], measurement_escape)17 except KeyError:18 if measurement is None:19 raise ValueError("'measurement' missing")20 return escape(measurement, measurement_escape)21def _serialize_tags(point, extra_tags):22 output = []23 for k, v in {**point.get('tags', {}), **extra_tags}.items():24 k = escape(k, key_escape)25 v = escape(v, tag_escape)26 if not v:27 continue # ignore blank/null string tags28 output.append(f'{k}={v}')29 return ','.join(output)30def _serialize_timestamp(point):31 dt = point.get('time')32 if not dt:33 return ''34 elif isinstance(dt, int):35 return dt...
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!!