Created
February 16, 2012 09:43
-
-
Save FGtatsuro/1843680 to your computer and use it in GitHub Desktop.
generate testcase sheets per class from javadoc
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import os.path | |
import sys | |
import lxml.html | |
import xlwt | |
def remove_file(filename): | |
if os.path.exists(filename): | |
try: | |
os.remove(filename) | |
except ex: | |
print "Sheet can't be removed" | |
print ex | |
sys.exit(1) | |
def remove_extention(filename): | |
return os.path.basename(filename).split('.')[0] | |
def is_method(method): | |
if method.find('(') == -1: | |
return False | |
if method.find(')') == -1: | |
return False | |
return True | |
def get_methods_list(doc_file, xpath_exp='//a/@name'): | |
root = lxml.html.parse(doc_file) | |
methods = [method for method in root.xpath(xpath_exp) if is_method(method)] | |
methods.sort() | |
return methods | |
def write_methods_list(sheet, targets, debug=True): | |
start = 0 | |
for clazz in targets: | |
sheet.write(start, 0, clazz) | |
for method in targets[clazz]: | |
if debug: | |
print method | |
sheet.write(start, 1, method) | |
start += 1 | |
def rename_overload_methods(names): | |
sep = '--' | |
for i in range(1, len(names)): | |
prev = names[i - 1].split(sep) | |
current = names[i] | |
if not current == prev[0]: | |
continue | |
prev[0] += sep | |
current += sep | |
if len(prev) == 1: | |
names[i - 1] = prev[0] + '1' | |
names[i] = current + '2' | |
else: | |
names[i] = current + str(int(prev[1]) + 1) | |
return names | |
def write_methods_per_class(targets, debug=True): | |
ext = ".xls" | |
column = ['Case No', 'Normal/Abnormal', 'Summary', 'Testcase existence', 'Note'] | |
for clazz in targets: | |
wb = xlwt.Workbook() | |
methods = targets[clazz] | |
sheetnames = [method.split('(')[0] for method in methods] | |
sheetnames = rename_overload_methods(sheetnames) | |
_style = xlwt.XFStyle() | |
_style.font.bold = True | |
row = 1 | |
col = 1 | |
for i in range(len(methods)): | |
#print sheetnames[i] | |
# write data | |
ws = wb.add_sheet(sheetnames[i]) | |
ws.write_merge(row, row, col, col + 2, methods[i], style=_style) | |
ws.write(row + 2, col, column[col - 1]) | |
ws.write(row + 2, col + 1, column[col]) | |
ws.write(row + 2, col + 2, column[col + 1]) | |
ws.write(row + 2, col + 3, column[col + 2]) | |
ws.write(row + 2, col + 4, column[col + 3]) | |
# width | |
unit = 493 | |
ws.col(0).width = unit | |
ws.col(1).width = unit * 6 | |
ws.col(2).width = unit * 12 | |
ws.col(3).width = unit * 60 | |
ws.col(4).width = unit * 12 | |
ws.col(5).width = unit * 6 | |
wb.save(clazz + ext) | |
def main(): | |
if len(sys.argv) < 2: | |
print 'Error' | |
sys.exit(1) | |
doc_files = sys.argv[1:] | |
bookname = "methods_list.xls" | |
sheetname = 'methods_list' | |
targets = {} | |
for doc_file in doc_files: | |
clazz = remove_extention(doc_file) | |
wb = xlwt.Workbook() | |
methods = get_methods_list(doc_file) | |
targets[clazz] = methods | |
write_methods_per_class(targets, debug=True) | |
''' | |
wb = xlwt.Workbook() | |
ws = wb.add_sheet(sheetname) | |
write_methods_list(ws, targets) | |
wb.save(bookname) | |
''' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment