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?
You could use expect.
Basically, your class should be able to use random input stream, not only stdin (and you have to refactor it, if it's unable yet).
After that you'll be able to simply put a mock stream with your custom data and to simulate any user input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With