单元测试使用gtest 1.6:如何检查什么是打印输出? [英] unit test using gtest 1.6 : how to check what is printed out?
本文介绍了单元测试使用gtest 1.6:如何检查什么是打印输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查打印到命令行的void函数?
How do i check a void function that print out sth to the command line?
例如:
void printFoo() {
cout << "Successful" < endl;
}
然后在test.cpp我把这个测试用例:
and then in the test.cpp i put this test case:
TEST(test_printFoo, printFoo) {
//what do i write here??
}
请解释清楚,因为我是新的单元测试gtest。谢谢
please explain clearly as i'm new to unit testing and gtest. Thank you
推荐答案
您必须更改函数才能使其可测试。最简单的方法是在你的单元测试中传递一个ostream(cout继承)到该函数,并使用一个字符串流(也继承ostream)。
You will have to change your function to make it testable. The easiest way to do this is to pass in an ostream ( which cout inherits ) to the function, and use a string stream ( also inherits ostream ) in your unit tests.
void printFoo( std::ostream &os )
{
os << "Successful" << endl;
}
TEST(test_printFoo, printFoo)
{
std::ostringstream output;
printFoo( output );
// Not that familiar with gtest, but I think this is how you test they are
// equal. Not sure if it will work with stringstream.
EXPECT_EQ( output, "Successful" );
// For reference, this is the equivalent assert in mstest
// Assert::IsTrue( output == "Successful" );
}
这篇关于单元测试使用gtest 1.6:如何检查什么是打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文