我喜欢 TDD,所以我一开始就尝试写我Black Box Test
的。
这是一个处理stdin
和输出stdout
这样的python程序(我尝试编写自己的语言来处理stdin和stdout):
$ python3 ./minor.py
>>> print, "hello\nthis is a good morning"
... hello
. this is a good morning
>>> $quit
但我不能模拟标准输入和标准输出。我尝试subprocess
在 Python 中使用,但它Popen.stdout.read()
正在挂起EOF
,这需要程序被杀死。或者communicate()
但是它会杀死我的程序并且它不能处理两个或更多的输入。
它让我不安了 2 多天,我找不到任何关于使用 stdin/stdout 进行模拟或黑盒测试的有用信息(看起来很奇怪,我可以使用浏览器进行测试,但不能轻松地使用 stdin/stdout 进行测试)。
谢谢。
*** First Editing ***
我创建了一个新的 unittest 类来处理我的类。它具有创建新 Popen 对象的功能。
我尝试写信stdin
并断言stdout
... 但它挂起只是因为它找不到EOF
.
我应该如何处理它才能做到?谢谢你的帮助!
class TestFunc(unittest.TestCase):
def run_minor(self):
return Popen(['python3', './minor.py'],
stdin = PIPE,
stdout = PIPE,
stderr = PIPE,
text = True,
)
def test_print(self):
prop = self.run_minor()
self.assertEqual(prop.stdout.read(), '>>> ')
prop.stdin.write("print, 'this'")
self.assertEqual(prop.stdout.read(), '... this\n>>> ')
prop.stdin.write("$quit")
self.assertEqual(prop.stdout.read(), '')
prop.kill()