Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid errors when elements is not present in a feature #18

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions behave2cucumber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'''


def convert(json_file, remove_background=False, duration_format=False, deduplicate=False):
def convert(json_file, remove_background=False, duration_format=False, deduplicate=False, remove_empty=False):
# json_nodes are the scopes available in behave/cucumber json: Feature -> elements(Scnerios) -> Steps
json_nodes = ['feature', 'elements', 'steps']
# These fields doesn't exist in cucumber report, there-fore when converting from behave, we need to delete these
Expand Down Expand Up @@ -64,8 +64,23 @@ def format_level(tree, index=0, id_counter=0):
# Option to remove background element because behave pushes it steps to all scenarios already
if remove_background:
for feature in json_file:
if feature['elements'][0]['type'] == 'background':
feature['elements'].pop(0)
if feature.get('elements'):
if feature['elements'][0].get('type') == 'background':
feature['elements'].pop(0)

if remove_empty:
for feature in json_file:
# Create a working list
scenarios = []

# For each scenario in the feature
for scenario in feature['elements']:
# Add the scenario only if there are steps
if len(scenario['steps']) > 0:
scenarios.append(scenario)

# Replace the existing list with the working list
feature['elements'] = scenarios

if deduplicate:
def check_dupe(current_feature, current_scenario, previous_scenario):
Expand All @@ -89,7 +104,7 @@ def check_dupe(current_feature, current_scenario, previous_scenario):
scenarios = []

# For each scenario in the feature
for scenario in feature['elements']:
for scenario in feature.get('elements', []):
# Append the scenario to the working list
scenarios.append(scenario)

Expand Down
14 changes: 10 additions & 4 deletions behave2cucumber/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@


options = {
"short": "hd:i:o:rfD",
"short": "hd:i:o:rfDe",
"long": [
"help", "debug=", "infile=", "outfile=", "remove-background",
"format-duration","deduplicate"
"format-duration", "deduplicate", "remove-empty"
],
"descriptions": [
"Print help message",
Expand All @@ -36,7 +36,8 @@
"Specify the output JSON, otherwise use stdout",
"Remove background steps from output",
"Format the duration",
"Remove duplicate scenarios caused by @autoretry"
"Remove duplicate scenarios caused by @autoretry",
"Remove scenarios which contain no steps"
]
}

Expand Down Expand Up @@ -87,6 +88,7 @@ def main(argv):
remove_background = False
duration_format = False
deduplicate = False
remove_empty = False

for opt, arg in opts:
if opt in ("-i", "--infile"):
Expand All @@ -104,6 +106,9 @@ def main(argv):
if opt in ("-D", "--deduplicate"):
log.info("Deduplicate: Enabled")
deduplicate = True
if opt in ("-e", "--remove-empty"):
log.info("Remove Empty: Enabled")
remove_empty = True

if infile is None:
log.critical("No input JSON provided.")
Expand All @@ -114,7 +119,8 @@ def main(argv):
cucumber_output = convert(json.load(f),
remove_background=remove_background,
duration_format=duration_format,
deduplicate=deduplicate)
deduplicate=deduplicate,
remove_empty=remove_empty)

if outfile is not None:
with open(outfile, 'w') as f:
Expand Down
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
here = path.abspath(path.dirname(__file__))

setup(
name='behave2cucumber',
name='behave2cucumber-lawnmowerlatte',

version='1.0.3',
version='1.0.4.2',

description='Behave to Cucumber json converter',
description='Behave to Cucumber json converter (lawnmowerlatte fork)',
long_description='This project helps solving the incompatibilty of Behave\'s genereated json reports '
'to tools using Cucumber json reports. '
'Its done by reformatting the Behave json to Cucumber json.',
'Its done by reformatting the Behave json to Cucumber json. '
'This fork contains a fix by lawnmowerlatte which has not been pulled into the main repo.',

url='https://github.com/behalf-oss/behave2cucumber',
url='https://github.com/lawnmowerlatte/behave2cucumber',

author='Andrey Goldgamer, Zvika Messing',
author_email='andrey.goldgamer@behalf.com, zvika@behalf.com ',
Expand Down