How do I unit test a console input class?

c++, googletest, unit-testing, user-input

Solution

You could use expect.

Problem

In one of my applications I have a class which is responsible for user input. The default method of input is the console (keyboard), and I want to write some unit tests for it to make sure it is correct. I am considering using the google-test framework for my unit testing, which makes it easy to automate all the testing. However, I am not sure how I can automate testing of the console input. Is there any way to simulate user input on the keyboard? Or do I have to manually enter my test input? Or perhaps redirect `stdin` (either in code or by a pipe when running the unit test)? EDIT: I am planning on using GNU readline for user input. At the moment I can't see any way to redirect the input stream of this library - perhaps someone else has experience with this?

Original source