Created
June 7, 2015 19:09
-
-
Save Hammer2900/629c9db9e1bf68c8a964 to your computer and use it in GitHub Desktop.
When writing BDD code with behave, you may want to include a set of examples for scenario outline, or provide a table for setting up initial conditions. This snippet ease the pain of formatting the table properly as text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
def as_behave_table(data): | |
""" nosetests --with-doctest --with-coverage report_table.py | |
>>> from report_table import as_behave_table | |
>>> data = [('what', 'how', 'who'), | |
... ('lorem', 'that is a long value', 3.1415), | |
... ('ipsum', 89798, 0.2)] | |
>>> print as_behave_table(data) | |
| what | how | who | | |
| lorem | that is a long value | 3.1415 | | |
| ipsum | 89798 | 0.2 | | |
""" | |
table = [] | |
# max size of each column | |
sizes = map(max, zip(*[[len(str(elt)) for elt in member] | |
for member in data])) | |
num_elts = len(sizes) | |
start_of_line = '| ' | |
vertical_separator = ' | ' | |
end_of_line = ' |' | |
meta_template = vertical_separator.join(['{{{{{0}:{{{0}}}}}}}'.format(i) | |
for i in range(num_elts)]) | |
template = '{0}{1}{2}'.format(start_of_line, | |
meta_template.format(*sizes), | |
end_of_line) | |
for d in data: | |
table.append(template.format(*d)) | |
return '\n'.join(table) | |
data = [(1,2,3456465465),(4,5555,6)] | |
print as_behave_table(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment