0

我正在使用 Python 的 unittest 编写一个测试框架。我有单独的模块用于测试应用程序的不同部分(part1 - part1.py、part2 - part2.py 等)。我想创建一个辅助模块,它将读取每个模块的配置设置,配置文件名与模块名相同(part1.py - part1.conf,part2.py - part2.conf)。配置文件将与模块位于同一文件夹中。当我运行 part1.py 时,我需要告诉测试它们的配置在 part1.conf 中。我怎么做?此帮助模块不一定与测试模块位于同一文件夹中。我不想使用鼻子,因为我希望尽可能少的外部依赖。

4

1 回答 1

1
def module_config(mod):
    '''Loads the config residing next to the module.'''
    import configparser, os.path
    cp = configparser.ConfigParser()
    cp.read_file(open(os.path.splitext(mod.__file__)[0] + '.conf'))
    return cp

# load config for some module
import some_module
module_config(some_module)

# load config for current module
module_config(sys.modules(__name__))
于 2011-11-29T08:16:05.603 回答