接着我们编写一个FileStatus类的骨架,使得这段测试代码可以被编译通过。
class FileStatusError
{};
class FileStatus
{
public:
FileStatus(const std::string& fileName)
{}
DWORD getFileSize() const
{
return 0;
}
bool fileExist() const
{
return false;
}
void setFileModifyDate( const FILETIME* )
{
}
FILETIME getFileModifyDate() const
{
return FILETIME();
}
std::string getFileName() const
{
return "";
}
};
下面给出完整的程序:
// UnitTest.cpp : Defines the entry point for the console application.
//
#include "CppUnit/TestCase.h"
#include "CppUnit/TestResult.h"
#include "CppUnit/TextOutputter.h"
#include "CppUnit/TestResultCollector.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/extensions/HelperMacros.h"
#include
#include
#include
#include
class FileStatusError
{};
class FileStatus
{
public:
FileStatus(const std::string& fileName)
{}
DWORD getFileSize() const
{
return 0;
}
bool fileExist() const
{
return false;
}
void setFileModifyDate( const FILETIME* )
{
}
FILETIME getFileModifyDate() const
{
return FILETIME();
}
std::string getFileName() const
{
return "";
}
};
class MyTestCase:public CPPUNIT_NS::TestFixture
{
std::string mFileNameExist;
std::string mFileNameNotExist;
std::string mTestFolder;
enum DUMMY
{
FILE_SIZE = 1011
};
public:
virtual void setUp()
{
mTestFolder = "c:justfortest";
mFileNameExist = mTestFolder + "exist.dat";
mFileNameNotExist = mTestFolder + "notexist.dat";
if( GetFileAttributes( mTestFolder.c_str() ) != INVALID_FILE_ATTRIBUTES )
throw std::exception( "test folder already exists" );
if( ! CreateDirectory( mTestFolder.c_str() ,NULL ) )
throw std::exception( "cannot create folder" );
HANDLE file = CreateFile( mFileNameExist.c_str(), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_NEW, 0, NULL );
if( file == INVALID_HANDLE_VALUE )
throw std::exception( "cannot create file" );
char buffer[FILE_SIZE];
DWORD bytesWritten;
if( !WriteFile( file, buffer, FILE_SIZE, &bytesWritten, NULL ) ||
bytesWritten != FILE_SIZE )
{
CloseHandle( file );
throw std::exception( "cannot write file" );
}
CloseHandle( file );
}
virtual void tearDown()
{
if( ! DeleteFile( mFileNameExist.c_str() ) )
文章来源于领测软件测试网 https://www.ltesting.net/