10

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?

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
Federico Fissore
  • 712
  • 4
  • 18
  • Maybe you can `mock` them and define `return_value` too. – Farhadix Nov 13 '13 at 17:22
  • *"`select.select()` says there is something to read even if nothing it's actually sent"* -- what does `os.read` return in this case? Have you tried `pexpect`, `pty` modules? – jfs Nov 13 '13 at 21:08
  • Regarding the not working PIPE: did you check thoroughly? Low level pipes work well with select on Linux (side note: this combination is used in a so called self-pipe trick to reliably handle signals). And select returns also for EOF. – VPfB Jul 28 '21 at 05:56

1 Answers1

2

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.

andersonvom
  • 11,701
  • 4
  • 35
  • 40
  • Code is https://github.com/arduino/YunBridge, packet.py in particular https://github.com/arduino/YunBridge/blob/master/bridge/packet.py [1] – Federico Fissore Nov 14 '13 at 14:08