Best Python code snippet using behave
steps.py
Source:steps.py
...263 if matching_steps is None:264 assert step_definition.step_type is not None265 matching_steps = self.step_usage_database[step_definition] = []266 # -- AVOID DUPLICATES: From Scenario Outlines267 if not steps_contain(matching_steps, step):268 matching_steps.append(step)269 def update_usage_database_for_step(self, step):270 step_definition = self.step_registry.find_step_definition(step)271 if step_definition:272 self.update_usage_database(step_definition, step)273 # elif step not in self.undefined_steps:274 elif not steps_contain(self.undefined_steps, step):275 # -- AVOID DUPLICATES: From Scenario Outlines276 self.undefined_steps.append(step)277 def update_usage_database_for_feature(self, feature):278 # -- PROCESS BACKGROUND (if exists): Use Background steps only once.279 if feature.background:280 for step in feature.background.steps:281 self.update_usage_database_for_step(step)282 # -- PROCESS SCENARIOS: Without background steps.283 for scenario in feature.walk_scenarios():284 for step in scenario.steps:285 self.update_usage_database_for_step(step)286 # -- FORMATTER API:287 def feature(self, feature):288 super(StepsUsageFormatter, self).feature(feature)289 self.update_usage_database_for_feature(feature)290 # -- REPORT API:291 def report(self):292 self.report_used_step_definitions()293 self.report_unused_step_definitions()294 self.report_undefined_steps()295 self.stream.write("\n")296 # -- REPORT SPECIFIC-API:297 def report_used_step_definitions(self):298 # -- STEP: Used step definitions.299 # ORDERING: Sort step definitions by file location.300 compare = lambda x, y: cmp(x[0].location, y[0].location)301 step_definition_items = self.step_usage_database.items()302 step_definition_items = sorted(step_definition_items, compare)303 for step_definition, steps in step_definition_items:304 stepdef_text = self.describe_step_definition(step_definition)305 steps_text = [u" %s %s" % (step.keyword, step.name)306 for step in steps]307 steps_text.append(stepdef_text)308 max_size = compute_words_maxsize(steps_text)309 if max_size < self.min_location_column:310 max_size = self.min_location_column311 schema = u"%-" + str(max_size) + "s # %s\n"312 self.stream.write(schema % (stepdef_text, step_definition.location))313 schema = u"%-" + str(max_size) + "s # %s\n"314 for step, step_text in zip(steps, steps_text):315 self.stream.write(schema % (step_text, step.location))316 self.stream.write("\n")317 def report_unused_step_definitions(self):318 unused_step_definitions = self.select_unused_step_definitions()319 if not unused_step_definitions:320 return321 # -- STEP: Prepare report for unused step definitions.322 # ORDERING: Sort step definitions by file location.323 compare = lambda x, y: cmp(x.location, y.location)324 step_definitions = sorted(unused_step_definitions, compare)325 step_texts = [self.describe_step_definition(step_definition)326 for step_definition in step_definitions]327 max_size = compute_words_maxsize(step_texts)328 if max_size < self.min_location_column-2:329 max_size = self.min_location_column-2330 # -- STEP: Write report.331 schema = u" %-" + str(max_size) + "s # %s\n"332 self.stream.write("UNUSED STEP DEFINITIONS[%d]:\n" % len(step_texts))333 for step_definition, text in zip(step_definitions, step_texts):334 self.stream.write(schema % (text, step_definition.location))335 def report_undefined_steps(self):336 if not self.undefined_steps:337 return338 # -- STEP: Undefined steps.339 compare = lambda x, y: cmp(x.location, y.location)340 undefined_steps = sorted(self.undefined_steps, compare)341 steps_text = [u" %s %s" % (step.keyword, step.name)342 for step in undefined_steps]343 max_size = compute_words_maxsize(steps_text)344 if max_size < self.min_location_column:345 max_size = self.min_location_column346 self.stream.write("\nUNDEFINED STEPS[%d]:\n" % len(steps_text))347 schema = u"%-" + str(max_size) + "s # %s\n"348 for step, step_text in zip(undefined_steps, steps_text):349 self.stream.write(schema % (step_text, step.location))350# -----------------------------------------------------------------------------351# UTILITY FUNCTIONS:352# -----------------------------------------------------------------------------353def steps_contain(steps, step):354 for other_step in steps:355 if step == other_step and step.location == other_step.location:356 # -- NOTE: Step comparison does not take location into account.357 return True358 # -- OTHERWISE: Not contained yet (or step in other location)....
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!!