【发布时间】:2018-12-05 20:40:36
【问题描述】:
假设一个 unittest 测试,其中通过 Python 脚本(使用 argparse)生成一个多行输出文件,并将该文件与预期结果的相等性进行比较。
def test_actual_vs_expected_output(self):
actual_inp = '/path_to/actu_inp.txt'
expect_otp = '/path_to/expe_otp.txt'
actual_otp = '/path_to/actu_otp.txt'
myScript = '/path_to/myScript.py'
cmd_list = ['python2', myScript,
'-i', actual_inp,
'-o', actual_otp]
try:
subprocess.check_output(' '.join(cmd_list), shell=True)
except subprocess.CalledProcessError as e:
print e.output
if os.path.isfile(actual_otp):
expect_str = open(expect_otp).read()
actual_str = open(actual_otp).read()
self.assertMultiLineEqual(expect_str, actual_str)
如何避免对python2 的调用进行硬编码(即在上述示例的cmd_list 中)?毕竟,Python2 解释器在不同系统上的调用方式可能不同。
【问题讨论】:
标签: python python-2.7 unit-testing command-line