首页 > 编程语言 > 详细

[Python] Use Python to generate Java Unit Test codes automatically

时间:2014-03-21 02:20:20      阅读:402      评论:0      收藏:0      [点我收藏+]

This code snippet is used to generate the unit test of Java codes.

import os
import string


def task(source,target):    
    files = os.listdir(source)
    target_files = {}
    
    for source_file in files:
        if source_file.endswith(‘.java‘):
            file_list = source_file.split(‘.‘)
            new_file = file_list[0]+‘Test.java‘
            new_class = file_list[0]+‘Test‘
            target_files.update({new_class:new_file})
            
    for new_class,target_file in target_files.items():
        absolute_file = target + target_file
        file_stream = open(absolute_file,‘a‘)        
        code_template = ‘‘‘
        public class $class {
            
            @Test
            public void test$source(){
            
            }
                                    
        }                        
        ‘‘‘        
        t = string.Template(code_template)
        file_stream.write(t.substitute({‘class‘:new_class,‘source‘:new_class.strip(‘Test‘)}));
        file_stream.close()      

if __name__ == ‘__main__‘:
    print ‘The task starts‘
    source_directory = ‘F:\eclipse\workspace\ZenJava\src\main\java\com\wonderfan\\‘
    target_directory = ‘F:\eclipse\workspace\ZenJava\src\test\java\com\wonderfan\\‘
    task(source_directory,target_directory)
    print ‘The task is over‘


[Python] Use Python to generate Java Unit Test codes automatically,布布扣,bubuko.com

[Python] Use Python to generate Java Unit Test codes automatically

原文:http://blog.csdn.net/wonderfan/article/details/21648449

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!