How to use Black Box Test with stdin/stdout with python

469 Views Asked by At

I like TDD, so I try to write my Black Box Test at first.

This is a python programme that deal with stdin and output to stdout like this (I try to write my own language that just deal with stdin and stdout):

$ python3 ./minor.py
>>> print, "hello\nthis is a good morning"
... hello
  . this is a good morning
>>> $quit

But I can not mock the stdin and stdout. I try to use subprocess in Python but the Popen.stdout.read() is hanging for a EOF, which need the programme killed. Or the communicate() but it will kill my programme and it cannot deal with two or more input.

It upset me for 2+ days, I cannot find anything useful about mock or black-box test with stdin/stdout (It looks strange that I can test with browser but not stdin/stdout easily).

Thanks.

*** First Editing ***

I create a new unittest class to handle my class. It have a function to create a new Popen object.

I try to write to stdin and assert the stdout... But it is hanging just because it cannot find the EOF.

How should I deal with it to make it? Thanks for your help!

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()
1

There are 1 best solutions below

0
Peterlits Zo On

I create a helper class to help... I do not know if it is the best idea, but it works greatly:

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()

Welcome for other answers...