给gtest测试用例增加延迟 [英] Add delay to gtest Test Case

查看:231
本文介绍了给gtest测试用例增加延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google测试框架来测试硬件以太网交换机.某些操作(例如启用RSTP)需要一些时间才能进行.因此,我需要在测试用例中实现某种 Sleep()函数:

I'm using google test framework for testing hardware Ethernet switch. Some operations (e.x. enabling RSTP) take time to proceed. So I need to implement some sort of a Sleep() function inside the test case:

TEST_F(RSTP, enableRSTP) {
    snmp.set(OID, Integer32(3));
    // after changing value switch is unavailable
    // so I need to wait before request
    auto result = snmp.get(OID);
    auto res = std::get<Integer32>(result);
    ASSERT_EQ(res, Integer32(3));
}

我如何做到这一点?

推荐答案

如其中一项注释中所述,您可以只使用(c ++ 14):

As mentioned in one of the comments, you could just use (c++14):

#include <chrono>
#include <thread>
TEST_F(RSTP, enableRSTP) {
  ...
  using namespace std::chrono_literals;
  std::this_thread::sleep_for(2s);
  ...
}

...或对于c ++ 11,将 2s 替换为:

... or for c++11, replace 2s with:

std::chrono::seconds(2)

如果您不使用> = c ++ 11,那么这将成为操作系统特定的问题(不是标准的c ++)

If you don't use >= c++11, then this becomes an OS specific question (not standard c++)

这篇关于给gtest测试用例增加延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆