C++ 增压单元测试:捕获未成功的测试
我正在运行一个测试,打开usb设备,发送和接收数据包,然后再次关闭。 看起来是这样的:C++ 增压单元测试:捕获未成功的测试,c++,unit-testing,boost,C++,Unit Testing,Boost,我正在运行一个测试,打开usb设备,发送和接收数据包,然后再次关闭。 看起来是这样的: void TestCase1(void) { int recv; BOOST_REQUIRE(initDevice()); BOOST_REQUIRE(openDevice()); BOOST_REQUIRE_EQUAL(receiveData(), 5); BOOST_REQUIRE(closeDevice()); BOOST_REQUIRE(uninitDevice()); } 现在,无论何
void TestCase1(void)
{
int recv;
BOOST_REQUIRE(initDevice());
BOOST_REQUIRE(openDevice());
BOOST_REQUIRE_EQUAL(receiveData(), 5);
BOOST_REQUIRE(closeDevice());
BOOST_REQUIRE(uninitDevice());
}
现在,无论何时在receiveData()
调用中出现错误并且检查“5”失败,都不会再调用closeDevice()
和uninitDevice()
,并且我无法在下一次测试中使用该设备。有办法解决这个问题吗?可能捕获一个异常并关闭并取消该捕获范围内的设备?或者这是一个完全错误的方法?
我对单元测试相当陌生。因此,我们非常感谢您的帮助。谢谢 当您要报告未满足的条件,但仍要继续测试时,应使用
BOOST\u CHECK
和BOOST\u CHECK\u EQUAL
。在这种情况下,前两项可能应该是“要求”d,后三项应该是“检查”ed。当您要报告不满足的条件,但仍要继续测试时,您应该使用BOOST\u CHECK
和BOOST\u CHECK\u EQUAL
。在这种情况下,前两项可能应该是“需要”d,后三项应该是“检查”ed。在夹具设置中,您最好先做一些必须发生的事情,然后在拆卸功能中进行整理。显然,将OO与RAII一起使用并将receiveData
作为类方法可以避免这种情况。
或者,
BOOST\u CHECK
将检查条件并在失败时继续测试,这将避免您遇到的问题,BOOST\u REQUIRE
停止执行其余测试。您最好先在夹具设置中执行必须发生的事情,然后在拆卸功能中进行整理。显然,将OO与RAII一起使用并将receiveData
作为类方法可以避免这种情况。
或者,代码> BooStChestCuth/Cuth>将检查条件并继续测试,如果失败,将避免您所遇到的问题。<代码> BooStUy要求停止测试的其余部分。 < P>我将使用现代C++中的一个关键概念,要帮助将initDevice/uninitDevice和openDevice/closeDevice捆绑在一起:
class USBDeviceHandler
{
public:
USBDeviceHandler()
: initDeviceHandle { ::initDevice()), &::uninitDevice },
openDeviceHandle { ::openDevice()), &::closeDevice }
{
}
using init_handle = std::unique_ptr<void, decltype(&::uninitDevice)>;
using open_handle = std::unique_ptr<void, decltype(&::closeDevice)>;
init_handle initDeviceHandle;
open_handle openDeviceHandle;
};
void TestCase1(void)
{
int recv;
USBDeviceHandler device; //init/open is called upon construction
BOOST_REQUIRE_EQUAL(receiveData(), 5);
}//close/uninit is called upon destruction
类USBDeviceHandler
{
公众:
USBDeviceHandler()
:initDeviceHandle{::initDevice(),&::uninitDevice},
openDeviceHandle{::openDevice(),&::closeDevice}
{
}
使用init_handle=std::unique_ptr;
使用open_handle=std::unique_ptr;
初始化句柄初始化设备句柄;
打开手柄打开设备手柄;
};
无效测试用例1(无效)
{
int recv;
USBDeviceHandler device;//在构造时调用init/open
BOOST_REQUIRE_EQUAL(receiveData(),5);
}//一旦被破坏,就需要关闭/取消连接
这是基于./P>> P>中的例子,我会使用现代C++中的一个关键概念,帮助将iTime/unNITIONE和OpenDelp/CouthEngy捆绑在一起:
class USBDeviceHandler
{
public:
USBDeviceHandler()
: initDeviceHandle { ::initDevice()), &::uninitDevice },
openDeviceHandle { ::openDevice()), &::closeDevice }
{
}
using init_handle = std::unique_ptr<void, decltype(&::uninitDevice)>;
using open_handle = std::unique_ptr<void, decltype(&::closeDevice)>;
init_handle initDeviceHandle;
open_handle openDeviceHandle;
};
void TestCase1(void)
{
int recv;
USBDeviceHandler device; //init/open is called upon construction
BOOST_REQUIRE_EQUAL(receiveData(), 5);
}//close/uninit is called upon destruction
类USBDeviceHandler
{
公众:
USBDeviceHandler()
:initDeviceHandle{::initDevice(),&::uninitDevice},
openDeviceHandle{::openDevice(),&::closeDevice}
{
}
使用init_handle=std::unique_ptr;
使用open_handle=std::unique_ptr;
初始化句柄初始化设备句柄;
打开手柄打开设备手柄;
};
无效测试用例1(无效)
{
int recv;
USBDeviceHandler device;//在构造时调用init/open
BOOST_REQUIRE_EQUAL(receiveData(),5);
}//一旦被破坏,就需要关闭/取消连接
这是基于中给出的示例。USBWrapper应该是USBDeviceHandler的构造函数吗?@doctorlove-oops,已修复。我在写答案的中途更改了helper类的名称。USBWrapper应该是USBDeviceHandler的构造函数吗?@doctorlove-oops,已修复。在写答案的中途,我更改了helper类的名称。在单元测试中打开实际usb端口是明智之举吗?在单元测试中打开实际usb端口是明智之举吗?