Google Test C++:有没有办法在测试中读取当前控制台输出?

发布于 2024-10-31 06:09:21 字数 350 浏览 3 评论 0原文

让我们假设我有一个要测试的类,它具有以下方法:

void
MyClass::sayHello()
{
   std::cout << "Hello";
}

现在,在我的谷歌测试中,我想验证是否已生成此输出。下面我的伪代码中使用的 lastConsoleOutput 等效项是什么?

// Tests if sayHello() outputs Hello
TEST(MyClassTest, sayHello)\
{
  EXPECT_EQ(lastConsoleOutput,"Hello");
}

感谢您的任何反馈!

Let us assume I have a to be tested class that has the following method:

void
MyClass::sayHello()
{
   std::cout << "Hello";
}

Now, within my google test I would like to verify that this output has been made. What would be the lastConsoleOutput equivalent be as used in my pseudo code below?

// Tests if sayHello() outputs Hello
TEST(MyClassTest, sayHello)\
{
  EXPECT_EQ(lastConsoleOutput,"Hello");
}

Thank you for any feedback!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2024-11-07 06:09:21

在这种情况下,我将避免重定向或测试 stdout 或 stderr 中的值,因为对这些流的访问不是线程安全的,输出缓冲区可能不会像可能预测的那样被附加和刷新。

从测试的角度来看,我建议将该方法重构为无状态并将状态(又名 std::cout)保留在其他地方。在您的示例中,您开始测试外部 API 的行为,而不是对象中的实际修改。

class MyClass {

    std::sstream state;

public:

    void print(){ std::cout << state.str(); } // no need to test this method, only external API

    void changeState() {
        state << "Hello" << std::endl; // this should be tested 
    }

}

在您的测试代码中,您现在可以使用以下命令轻松执行测试

// Tests if sayHello() outputs Hello
TEST(MyClassTest, sayHello)
{
  myClass.changeState();
  EXPECT_STREQ(myClass.state.str().c_str(),"Hello");
}

In this case I would avoid redirecting or testing for values in stdout or stderr, since the access to those streams is not threads-safe in a way that output buffer may not be appended and flushed as possibly predicted.

From a testing perspective I would suggest refactoring the method to be stateless and keep the state (a.k.a. std::cout) somewhere else. In your example you start testing behavior of an external API and not the actual modification in your object.

class MyClass {

    std::sstream state;

public:

    void print(){ std::cout << state.str(); } // no need to test this method, only external API

    void changeState() {
        state << "Hello" << std::endl; // this should be tested 
    }

}

In your testing code you can now easily perform the test using

// Tests if sayHello() outputs Hello
TEST(MyClassTest, sayHello)
{
  myClass.changeState();
  EXPECT_STREQ(myClass.state.str().c_str(),"Hello");
}
御守 2024-11-07 06:09:21

我避免使用类似 sayHello() 方法的代码。我会将其重构为:

void MyClass::sayHello(std::ostream& out) {
    out << "Hello";
}

那么测试方法将如下所示:

TEST(MyClassTest, sayHello) {
  MyClass c;
  std::stringstream strm;
  c.sayHello(strm);
  EXPECT_STREQ("Hello", strm.str.c_str());
}

I avoid having code like your sayHello() method. I would refactor it to something like:

void MyClass::sayHello(std::ostream& out) {
    out << "Hello";
}

Then the test method would be like this:

TEST(MyClassTest, sayHello) {
  MyClass c;
  std::stringstream strm;
  c.sayHello(strm);
  EXPECT_STREQ("Hello", strm.str.c_str());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文