0

我喜欢 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()
4

1 回答 1

1

我创建了一个助手类来帮助......我不知道这是否是最好的主意,但它非常有效:

class MinorMock(object):
    """
    The Mock of Minor Programme.

    Use method `input` and `assertOutput` to set the input and the output want. Use
    `assertInputMeetOutput` to check if the input is meet its output by unittest's object.
    """
    def __init__(self, test_obj):
        self.popen = Popen(['python3', 'minor.py'], stdin=PIPE, stdout=PIPE, stderr=PIPE, text=True)
        self.input_text = ''
        self.output_text = ''
        self.err_text = ''
        self.test_obj = test_obj

    def input(self, text):
        self.input_text += text
        return self

    def assertOutput(self, text):
        self.output_text += text
        return self

    def assertError(self, text):
        self.err_text += text
        return self
    
    def assertInputMeetOutput(self):
        (out, err) = self.popen.communicate(self.input_text)
        self.test_obj.assertEqual(out, self.output_text)
        self.test_obj.assertEqual(err, self.err_text)
        self.popen.kill()

欢迎其他答案...

于 2020-08-21T11:22:31.537 回答