-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_features.py
executable file
·56 lines (42 loc) · 1.56 KB
/
generate_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
"""
Parses Gherkin code from a Jupyter Notebook.
Takes the path to a Jupyter Notebook as an argument
Gherkin code should be marked up in Markdown cells in blocks of the form
```gherkin
Feature: ...
Scenario: ...
...
```
"""
import sys
import json
import os.path
if len(sys.argv) < 2:
raise ValueError('Please specify notebook file to parse.')
nb_file_path = sys.argv[1]
if not nb_file_path.endswith('.ipynb'):
raise ValueError('Specified file does not appear to be a Jupyter Notebook')
if not os.path.isfile(nb_file_path):
raise ValueError('Specified file does not exist')
with open(nb_file_path) as nb_file:
data = json.load(nb_file)
cells = data["cells"]
for i, cell in enumerate(cells):
if cell["cell_type"] == "markdown":
source = cell["source"]
gherkin_code = ""
in_gherkin_block = False
for line in source:
if in_gherkin_block and line.startswith("```"):
in_gherkin_block = False
elif in_gherkin_block:
gherkin_code += line
elif line.startswith("```gherkin"):
in_gherkin_block = True
if len(gherkin_code) > 0:
# Filename needs to reference Notebook and cell index
# This will allow generated test skeletons to be inserted into Notebook at correct locations
feature_file = open(nb_file_path+"-"+str(i)+".feature", "w")
feature_file.write(gherkin_code)
feature_file.close()