在这个case我们得到四个可能的错误:
load method's return value
getData method's return value
number structure member's value
string structure member's value
第二个用例也是相似的。但是困难点,我们需要使用已知的数据来填充fixture,把它存在磁盘临时文件里,然后打开两个文件(新的和辅助文件),读并比较内容,两者如一致正确
void DiskDataTestCase::storeTest()
{
DATA d;
DWORD tmpSize, auxSize;
BYTE *tmpBuff, *auxBuff;
TCHAR absoluteFilename[MAX_PATH];
DWORD size = MAX_PATH;
// configures structure with known data
d.number = FILE_NUMBER;
strcpy(d.string, FILE_STRING);
// convert from relative to absolute path
strcpy(absoluteFilename, AUX_FILENAME);
CPPUNIT_ASSERT( RelativeToAbsolutePath(absoluteFilename, &size) );
// executes action
fixture->setData(&d);
CPPUNIT_ASSERT( fixture->store("data.tmp") );
// Read both files contents and check results
// ReadAllFileInMemory is an auxiliar function which allocates a buffer
// and save all file content inside it. Caller should release the buffer.
tmpSize = ReadAllFileInMemory("data.tmp", tmpBuff);
auxSize = ReadAllFileInMemory(absoluteFilename, auxBuff);
// files must exist
CPPUNIT_ASSERT_MESSAGE("New file doesn't exists?", tmpSize > 0);
CPPUNIT_ASSERT_MESSAGE("Aux file doesn't exists?", auxSize > 0);
// sizes must be valid
CPPUNIT_ASSERT(tmpSize != 0xFFFFFFFF);
CPPUNIT_ASSERT(auxSize != 0xFFFFFFFF);
// buffers must be valid
CPPUNIT_ASSERT(tmpBuff != NULL);
CPPUNIT_ASSERT(auxBuff != NULL);
// both file's sizes must be the same as DATA's size
CPPUNIT_ASSERT_EQUAL((DWORD) sizeof(DATA), tmpSize);
CPPUNIT_ASSERT_EQUAL(auxSize, tmpSize);
// both files content must be the same
CPPUNIT_ASSERT( 0 == memcmp(tmpBuff, auxBuff, sizeof(DATA)) );
delete [] tmpBuff;
delete [] auxBuff;
::DeleteFile("data.tmp");
}
调用用户接口
后,我们看看用一个mfc 对话框(TestRunner.dll)用来说明。
我们需要在我们的初始化函数中做如下初始化
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
BOOL CMy_TestsApp::InitInstance()
{
....
// declare a test runner, fill it with our registered tests and run them
CppUnit::MfcUi::TestRunner runner;
runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
runner.run();
return TRUE;
}
只要定义一个test的实例,然后注册所有用例,再执行case