How to read from standard input during unit tests

If a test relies on input from STDIN, it's not a unit test. Unit tests shouldn't have any dependency on external factors. They shouldn't consume stdin, make network requests, interact with a DB, communicate with hardware, etc., and they should have absolutely minimal requirements of their environment. E.g. testing a german localization shouldn't require your computer to be set to germen. Every such integration or dependency slows tests down, makes them brittle (e.g. now you can't test offline, without a DB, with a french computer, etc.).

What's the unit you're trying to test, and what is it about it that makes you want to manually provide input?

Tests are meant to be automatic. Sitting around and keying in the right input at the right time during a test run is ... not automated testing.

2 Likes