Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Last active August 29, 2015 13:57
Show Gist options
  • Save igniteflow/9782788 to your computer and use it in GitHub Desktop.
Save igniteflow/9782788 to your computer and use it in GitHub Desktop.
Generate Python BDD style test stubs from a simple text file format
#!/usr/bin/env python
import sys
"""
Given a file of the following format:
MyClass
- My test case
- Another test case
- And another test case
AnotherClass
- My test case
generates a Python file of the format:
class MyClass:
def test_my_test_case(self):
pass
def test_another_test_case(self):
pass
def test_and_another_test_case(self):
pass
def test(self):
pass
class AnotherClass:
def test_my_test_case(self):
pass
Usage:
./bdd_helper.py input.md output.py
"""
input_file = sys.argv[1]
output_file = sys.argv[2]
def generate_structure():
classes = {}
with open(input_file) as f:
current_testcase = None
for line in f.readlines():
line = line.replace('\n', '')
if line is not '':
if '-' not in line[:1]:
current_testcase = line
classes[current_testcase] = []
else:
line = 'test' + '_'.join(line.lower().replace('-', '').split(' '))
classes[current_testcase].append(line)
return classes
def generate_python_stub_file(classes):
with open(output_file, 'w+') as f:
for class_name, method_names in classes.iteritems():
f.write('class %s:\n\n' % class_name)
for method_name in method_names:
f.write(' def %s(self):\n' % method_name)
f.write(' pass\n\n')
def main():
classes = generate_structure()
generate_python_stub_file(classes)
print('Python stub generated to: %s' % output_file)
if __name__ == '__main__': main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment