通过 google 测试测试异步调用
Test asynchronous call via google tests
我有一个class Communicator,测试它是否可以连接到测试服务器。我是这样称呼它的:
class CommunicatorTest
{
public:
CommunicatorTest() {}
bool doTest()
{
bool _success;
Parameters params;
Communicator communicator;
communicator.connect(params, [this, &_success](bool success)
{
statusCode = errorCode;
m_condition.notify_one();
});
std::unique_lock<std::mutex> uGuard(m_mutex);
m_condition.wait(uGuard);
return _success;
}
private:
std::mutex m_mutex;
std::condition_variable m_condition;
};
bool communicatorTest()
{
CommunicatorTest test;
bool success = test.doTest();
return success;
}
TEST(CommunicatorTest, test_eq)
{
EXPECT_EQ(communicatorTest(), true);
}
我尝试使用条件和互斥使这段代码同步,但它失败了,日志只说测试是 运行 并立即完成。
有没有办法使用 google 测试从回调中测试成功变量?
提前致谢。
在这些情况下,最好的解决方案是创建模拟服务器行为的模拟。当 运行 你的测试时,你不应该依赖(除非非常必要)外部状态。
测试可能会失败,因为服务器未连接、没有互联网连接或其他任何情况。
您可以使用 Google Mock 之类的东西,现在是 Google 测试套件的一部分来模拟行为:
class MockServer : public Server {
public:
MOCK_METHOD2(DoConnect, bool());
....
};
然后做这样的事情:
TEST(ServerTest, CanConnect) {
MockServer s;
EXPECT_CALL(s, DoConnect())
..WillOnce(Return(true));
EXPECT_TRUE(server.isConnected());
}
可以模拟错误处理:
TEST(ServerTest, CannotConnect) {
MockServer s;
EXPECT_CALL(s, DoConnect())
..WillOnce(Return(false));
EXPECT_FALSE(server.isConnected());
// ... Check the rest of your variables or states that may be false and
// check that the error handler is working properly
}
作为编写异步代码的人,我多次偶然发现这个问题 - 似乎大多数现有的 C/C++ 测试框架都没有真正支持测试异步代码。主要需要的是一个事件循环,您可以在其中安排要执行的事情(模拟定时外部事件等),以及一种注册响应并可选择检查响应发生顺序的机制。因此,我没有尝试以某种方式采用现有框架(这可能会导致更大的努力),而是创建了自己的框架。我一直在用它来测试我开发的 javascript-like promise class,它对我来说很有用。如果您有兴趣,我刚刚在 GitHub 上发布了它:
https://github.com/alxvasilev/async-test
我有一个class Communicator,测试它是否可以连接到测试服务器。我是这样称呼它的:
class CommunicatorTest
{
public:
CommunicatorTest() {}
bool doTest()
{
bool _success;
Parameters params;
Communicator communicator;
communicator.connect(params, [this, &_success](bool success)
{
statusCode = errorCode;
m_condition.notify_one();
});
std::unique_lock<std::mutex> uGuard(m_mutex);
m_condition.wait(uGuard);
return _success;
}
private:
std::mutex m_mutex;
std::condition_variable m_condition;
};
bool communicatorTest()
{
CommunicatorTest test;
bool success = test.doTest();
return success;
}
TEST(CommunicatorTest, test_eq)
{
EXPECT_EQ(communicatorTest(), true);
}
我尝试使用条件和互斥使这段代码同步,但它失败了,日志只说测试是 运行 并立即完成。
有没有办法使用 google 测试从回调中测试成功变量? 提前致谢。
在这些情况下,最好的解决方案是创建模拟服务器行为的模拟。当 运行 你的测试时,你不应该依赖(除非非常必要)外部状态。
测试可能会失败,因为服务器未连接、没有互联网连接或其他任何情况。
您可以使用 Google Mock 之类的东西,现在是 Google 测试套件的一部分来模拟行为:
class MockServer : public Server {
public:
MOCK_METHOD2(DoConnect, bool());
....
};
然后做这样的事情:
TEST(ServerTest, CanConnect) {
MockServer s;
EXPECT_CALL(s, DoConnect())
..WillOnce(Return(true));
EXPECT_TRUE(server.isConnected());
}
可以模拟错误处理:
TEST(ServerTest, CannotConnect) {
MockServer s;
EXPECT_CALL(s, DoConnect())
..WillOnce(Return(false));
EXPECT_FALSE(server.isConnected());
// ... Check the rest of your variables or states that may be false and
// check that the error handler is working properly
}
作为编写异步代码的人,我多次偶然发现这个问题 - 似乎大多数现有的 C/C++ 测试框架都没有真正支持测试异步代码。主要需要的是一个事件循环,您可以在其中安排要执行的事情(模拟定时外部事件等),以及一种注册响应并可选择检查响应发生顺序的机制。因此,我没有尝试以某种方式采用现有框架(这可能会导致更大的努力),而是创建了自己的框架。我一直在用它来测试我开发的 javascript-like promise class,它对我来说很有用。如果您有兴趣,我刚刚在 GitHub 上发布了它: https://github.com/alxvasilev/async-test