我们目前正在将在 C++Builder 5 下开发的源代码迁移到更新的 Embarcadero 的 XE5。展望 future ,我们希望在 C++Builder5 下编写我们的单元测试,理想情况下,它在迁移后可以完全正常运行,几乎不需要维护。
不过,我的问题很简单。是否可以使用 Embarcadero 在 C++Builder 5 上使用的相同 DUnit 框架?如果是这样,您能给我们一些提示吗?
谢谢。
最佳答案
DUnit 确实可以在 CppBuilder5 上使用。 为此:
- 从这里获取 DUnit 的源代码:http://sourceforge.net/projects/dunit/files/latest/download
使用以下命令行构建 DUNITRTL.lib,或者您可以制作一个 .bat 文件并从/dunit/src 文件夹中执行它:
SET NDC6=C:\PROGRA~2\Borland\CBUILD~1 %NDC6%\bin\dcc32.exe Dunit.dpr /O..\objs /DBCB /M /H /W /JPHN -$d-l-n+p+r-s-t-w-y- %2 %3 %4 %NDC6%\bin\tlib.exe DUNITRTL.lib /P32 -+dunit.obj -+DunitAbout.obj -+DUnitMainForm.obj -+GUITestRunner.obj -+TestExtensions.obj -+TestFramework.obj -+TestModules.obj -+TextTestRunner.obj
一旦完成,制作测试项目就变得容易了:
- 创建一个 VCL 表单应用程序。
- 从项目中删除 Unit1.cpp 及其表单。
- 将我们构建的 DUNITRTL.lib 文件添加到项目中(项目 > 添加到项目)。
- 将/dunit/src 路径添加到库和包含路径。 (项目 > 选项 > 文件夹/条件)。
- 转到 Project1.cpp 文件并确保它看起来像这样:
#include <vcl.h>
#pragma hdrstop
#include <GUITestRunner.hpp>
USERES("Project1.res");
USELIB("DUNITRTL.lib");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Guitestrunner::RunRegisteredTests();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
- 向项目添加一个新单元,用作测试套件。
MyTestCase.h
//---------------------------------------------------------------------------
#ifndef __TMYTESTCASE_H__
#define __TMYTESTCASE_H__
//---------------------------------------------------------------------------
#include <TestFramework.hpp>
class TMyTestCase : public TTestCase
{
public:
__fastcall virtual TMyTestCase(AnsiString name) : TTestCase(name) {}
virtual void __fastcall SetUp();
virtual void __fastcall TearDown();
__published:
void __fastcall MySuccessfulTest();
void __fastcall MyFailedTest();
};
#endif
MyTestCase.cpp
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#include "TMyTestCase.h"
//---------------------------------------------------------------------------
void __fastcall TMyTestCase::SetUp()
{}
void __fastcall TMyTestCase::TearDown()
{}
void __fastcall TMyTestCase::MySuccessfulTest()
{
int a = 1;
a = a + 1;
CheckEquals(2,a,"test adding");
}
void __fastcall TMyTestCase::MyFailedTest()
{
int a = 1;
a = a + 2;
CheckEquals(2,a,"test adding");
}
static void registerTests()
{
_di_ITestSuite iSuite;
TTestSuite* testSuite = new TTestSuite("Testing TMyTestCase.h");
if (testSuite->GetInterface(__uuidof(ITestSuite), &iSuite))
{
testSuite->AddTests(__classid(TMyTestCase));
Testframework::RegisterTest(iSuite);
}
else
{
delete testSuite;
}
}
#pragma startup registerTests 33
#pragma package(smart_init)
- 项目可以编译运行。测试应该顺利执行。
关于testing - 旧 C++Builder 版本上的 DUnit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22907507/