Advertisement
Guest User

behave feature updated

a guest
Aug 17th, 2022
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. class BehaveFeature(pytest.Item):
  2.     """Represents feature element in pytest derived from basic test item"""
  3.  
  4.     def __init__(self, name, parent, feature, scenario):
  5.         super().__init__(name, parent)
  6.         self._feature = feature
  7.         self._scenario = scenario
  8.         self._working_folder_path = Path(self.fspath).parent.parent
  9.  
  10.         # the following characters cause trouble in shell, so we need to escape them
  11.         name_escaped = str(self._scenario.name).replace('"', '\\"').replace('(', '\\(').replace(')', '\\)').replace('*', '\\*')
  12.         self._cmd = split(f"""behave
  13.            --format json
  14.            --no-summary
  15.            --include {self._feature.filename}
  16.            --name "{name_escaped}"
  17.        """)
  18.  
  19.     def runtest(self):
  20.         proc = subprocess.run(self._cmd, stdout=subprocess.PIPE, cwd=self._working_folder_path, check=False)
  21.         stdout = proc.stdout.decode("utf8")
  22.         if proc.returncode != 0:
  23.             raise BehaveException(self, stdout)
  24.         result = json.loads(stdout)
  25.         for feature in result:
  26.             if feature['status'] == "failed":
  27.                 raise BehaveException(self, stdout)
  28.  
  29.     def repr_failure(self, excinfo, style=None):
  30.         """Called when self.runtest() raises an exception."""
  31.  
  32.         if isinstance(excinfo.value, BehaveException):
  33.             feature = excinfo.value.args[0]._feature
  34.             results = excinfo.value.args[1]
  35.             summary = ""
  36.             try:
  37.                 data = json.loads(results)
  38.             except json.decoder.JSONDecodeError:
  39.                 summary += f'Unexpected error while running tests from behave feature {feature}\n'
  40.                 summary += f'Command was {self._cmd}'
  41.                 summary += results
  42.                 return summary
  43.  
  44.             for feature in data:
  45.                 if feature['status'] != "failed":
  46.                     continue
  47.                 summary += f"\nFeature: {feature['name']}"
  48.                 for element in feature["elements"]:
  49.                     if element['status'] != "failed":
  50.                         continue
  51.                     summary += f"\n  {element['type'].title()}: {element['name']}"
  52.                     for step in element["steps"]:
  53.                         try:
  54.                             result = step['result']
  55.                         except KeyError:
  56.                             summary += f"\n    Step [NOT REACHED]: {step['name']}"
  57.                             continue
  58.                         status = result['status']
  59.                         if status != "failed":
  60.                             summary += f"\n    Step [OK]: {step['name']}"
  61.                         else:
  62.                             summary += f"\n    Step [ERR]: {step['name']}"
  63.                             summary += "\n      " + "\n      ".join(result['error_message'])
  64.  
  65.             return summary
  66.         else:
  67.             raise BehaveException(f'Unknown error happened while running tests, excinfo={excinfo}')
  68.  
  69.     def reportinfo(self):
  70.         return self.fspath, 0, f"Feature: {self._feature.name}  - Scenario: {self._scenario.name}"
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement