Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 200
Federico Fissore Avatar asked Nov 13 '13 16:11

Federico Fissore


1 Answers

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.

like image 144
andersonvom Avatar answered Sep 26 '22 05:09

andersonvom