我有一个 Python 脚本。我使用 unittest 进行测试,但如何测试整个脚本。
我的想法是这样的:
def test_script(self):
output=runScript('test.py --a 5 --b 3')
self.assertEqual(output, '8')
test.py 接受参数 a 和 b 并打印 a+b,在本例中为 8
请您参考如下方法:
您可以使用 subprocess库来调用脚本并捕获输出。
import subprocess
p = subprocess.Popen(
['./test.py', '--a', ...],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
print p.stdout.read()
