I need to write tests(using google testing framework) for small study program that was written not by me. (it’s just small console game which can get modes from command line or just get it in runtime)
There is a problem: I can’t change the souce code but there is in almost all methods used cout and cin. and my question is “how to answer on requests (cin) of programm while testing (something like get data for cin from string )?“.
I need to write tests(using google testing framework) for small study program that was
Share
Assuming you can control
main()(or some other function called before the functions to be tested) you can change wherestd::cinreads from and wherestd::coutwrites to:(likewise for
std::cout)This example saves the original stream buffer of
std::cinso it can be replaced before leavingmain(). It then sets upstd::cinto read from a string stream. It can be any other stream buffer as well.