[英]Writing unittest for config reader Python unittest library
我是 Python 中的 unittest 库的新手。 我在下面有一个示例代码,我想测试变量是否正确填充了值。 首先我应该替换对配置阅读器的调用吗? 我正在考虑提供一个配置文本,而不是从该位置读取以隔离。 我怎样才能做到这一点? 一些起点可能会有所帮助。
示例代码:
import sys
import os.path
from configparser import ConfigParser
templates=""
testfile=""
runbook=""
def init():
global templates
global testfile
global runbook
if len(sys.argv) > 1:
currPath = sys.argv[1]
else: currPath = r"Directory path"
conf = os.path.join(currPath,"temp.properties")
config = ConfigParser()
config.read(conf)
templates = os.path.join(currPath, config.get("setting", "Templates"))
testfile = os.path.join(currPath, config.get("setting", "Testfile"))
runbook = os.path.join(currPath, config.get("setting", "Runbook"))
我的 temp.properties 文件
[setting]
Templates=temp\Templates.xml
Testfile=reports\TestCases.xml
Runbook=reports\Runbook.xml
我能够通过 mocking app.ConfigReader 解决这个问题。 我模拟了 ConfigReader.get 的返回值以返回列表中字符串的副作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.