Federico Fissore
Federico Fissore

Reputation: 872

Test a program that uses tty stdin and stdout

I have a software made of two halves: one is python running on a first pc, the other is cpp running on a second one. They communicate through the serial port (tty).

I would like to test the python side on my pc, feeding it with the proper data and see if it behaves as expected.

I started using subprocess but then came the problem: which stdin and stdout should I supply?

cStringIO does not work because there is no fileno()

PIPE doesn't work either because select.select() says there is something to read even if nothing it's actually sent

Do you have any hints? Is there a fake tty module I can use?

Upvotes: 10

Views: 1378

Answers (1)

andersonvom
andersonvom

Reputation: 11861

Ideally you should mock that out and just test the behavior, without relying too much on terminal IO. You can use mock.patch for that. Say you want to test t_read:

@mock.patch.object(stdin, 'fileno')
@mock.patch.object(stdin, 'read')
def test_your_behavior(self, mock_read, mock_fileno):
    # this should make select.select return what you expect it to return
    mock_fileno.return_value = 'your expected value' 

    # rest of the test goes here...

If you can post at least part of the code you're trying to test, I can maybe give you a better example.

Upvotes: 2

Related Questions