[D]python中如何mock一个类的静态方法
大家好,最近在项目中写集成测试的时候遇到一个比较棘手的问题。就是在程序中有一个工具类utils,里面大多都是写static方法,在其他类中直接使用类名(utils)来引用这些static方法。
utils类代码结果大致如下:
class utils(object): ''' Some common tool functions used by other scripts ''' @staticmethod def run_statement(hql, conf="", init_file=""): ''' Run an HQL statement through Hive cli ''' cmd_str = "" if init_file == "": cmd_str = "hive -e \"%s\" " % hql else: cmd_str = "hive -i \"%s\" -e \"%s\" " % (init_file, hql) if conf == "": pass else: conf_list = conf.split(',') for c in conf_list: c_pair = c.split('=') cmd_str = cmd_str + "-hiveconf %s=%s " % (c_pair[0], c_pair[1]) utils.run_os_cmd(cmd_str) @staticmethod def run_file(hql_file): ''' Run an HQL script through Hive cli ''' cmd_str = "hive -f %s" % hql_file utils.run_os_cmd(cmd_str) @staticmethod def run_os_cmd(cmd_str): (status, output) = commands.getstatusoutput(cmd_str) if status == 0: print ("%s finished\n%s" % (cmd_str, output)) else: raise Exception, ("%s failed" % cmd_str)