单元测试遗留 C++使用 CPPUnit 编写代码

发布于 2024-11-25 13:47:45 字数 275 浏览 6 评论 0原文

我的任务是管理用 vc++ 6.0 编写的大型代码库,我需要开始为部分代码构建单元测试。我已经设置了 CPPUnit,它与我的项目 DLL 一起工作,我面临的问题如下。遗留应用程序由 10 个静态库和一个包含 99% 代码的巨大可执行 MFC 应用程序组成。我的单元测试框架正在同一工作区中的另一个项目中运行,并将测试 10 个库,没问题,所有包含和引用都正常,当我尝试对大型 MFC 应用程序执行相同操作时,我收到链接器错误,因为我没有应用程序的 dll。有没有什么方法可以对应用程序进行单元测试,而无需将测试代码直接放入应用程序中。

I am tasked with managing a large code base written in vc++ 6.0, I need to start building unit test for portions of the code. I have set up CPPUnit and it works with my projects DLL's the problem I am facing is as follows. The legacy application is made up of 10 static libraries and one huge executable MFC application that contains 99% of the code. My unit test framework is running in another project within the same workspace and will test the 10 libraries no problem all include and references are ok, when I try to do the same for the large MFC application I get a linker error as I do not have a dll for the application. Is there any way to unit test the application without putting the test code directly inside the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜灵血窟げ 2024-12-02 13:47:45

您应该按原样继续:

  1. 您有一个引用库的测试应用程序。
  2. 您有一个主应用程序也引用了这些库。

将代码从主应用程序移动到现有库中,或者最好将代码移动到新库中。然后,您的测试应用程序可以访问更多代码,而无需引用该应用程序。

您知道何时完成,应用程序的源代码由一个定义 main() 的模块以及由测试应用程序测试的库中的其他所有内容组成。

You should carry on as you are:

  1. You have one test application that references libraries.
  2. You have one main application that also references those libraries.

Either move code from the main application into the existing libraries, or, preferably, move code into new libraries. Then your test application can access more code without ever referring to the application.

You know when you are done when the source for the application consists of one module which defines main() and everything else in in libraries which are tested by the test application.

我们的影子 2024-12-02 13:47:45

我在单元测试方面的经验通常是相反的。为您的测试创建一个项目,然后从其他项目导入代码。

您无法链接到 MFC 应用程序,可能是因为您的函数未导出。它们存在,但无法与 DLL 不同的其他应用程序进行通信。

My experience with unit testing is usually the opposite. Create a project for your test then import code from your other projects.

You can't link to the MFC application probably because your functions aren't exported. They exist, but have no mean to communicate with other applications unlike DLLs.

睡美人的小仙女 2024-12-02 13:47:45

我知道没有办法链接到可执行文件。通过将业务逻辑移至 DLL 并将应用程序保留为“前端”来重构代码将是最明显的解决方案。但是,由于它是遗留代码,因此出于单元测试的目的简单地复制代码可能更合适。这并不理想,而且由于它是一个 MFC 应用程序,因此可能并不容易。

I know of no way to link against an executable file. Refactoring the code by moving the business logic to a DLL and leaving the application as a "Front-end" would be the most obvious solution. However, since it is legacy code it is likely more appropriate to simply duplicate the code for purposes of unit testing. This is not ideal, and since it is an MFC applicaiton may not be trivially easy.

清浅ˋ旧时光 2024-12-02 13:47:45

为了测试你的主应用程序,你可以建立一个测试项目,其中包括你想要测试的源文件 - 不确定用 VC6 实现有多容易,手头没有它,但在 VS2005 及更高版本中这非常简单。

因此,在您的解决方案中,您最终会得到这样的结构:

MyLegacySystem.sln
  MyApplication.proj
    Main.cpp
    BusinessRules.cpp
  MyApplicationUnitTests.proj
    UnitTestsMain.cpp
    BusinessRules.cpp
    BusinessRulesTests.cpp

如果出于某种原因您无法将源文件包含在 2 个项目中,您可以通过调用预处理器魔法将源文件拉入您的测试项目:

BusinessRulesStub.cpp:
#include "..\src\BusinessRules.cpp"

但是,这本质上是一个临时修复。正如已经建议的,最终大部分代码应该提取到单独的库中。

To test your main application you can set up a test project which includes the source files you want to test - not sure how easy it is to achieve with VC6, do not have it at hand, but in VS2005 and later this is quite straightforward.

So in your solution you end up with a structure like this:

MyLegacySystem.sln
  MyApplication.proj
    Main.cpp
    BusinessRules.cpp
  MyApplicationUnitTests.proj
    UnitTestsMain.cpp
    BusinessRules.cpp
    BusinessRulesTests.cpp

If for whatever reason you cannot include your source files in 2 projects, you can pull the sources into your test project by invoking the preprocessor magic:

BusinessRulesStub.cpp:
#include "..\src\BusinessRules.cpp"

However, this is essentially a temporary fix. As already suggested, in the end most of the code should be extracted into separate libraries.

丢了幸福的猪 2024-12-02 13:47:45

如果您无法重构项目以将业务逻辑移至新的静态库中,请尝试将测试项目链接到项目的中间对象文件,您可能可以在 BigProject\debug 或 BigProject\debug\obj 中找到这些文件。正如您所发现的,您无法链接到 .EXE。

这实现了与 Chad 建议的复制过程相同的结果,同时避免了源代码的实际重复,这将是一件非常糟糕的事情。

If you can't refactor your project to move the business logic into a new static library, try linking your test project against your project's intermediate object files, which you can probably find in BigProject\debug or BigProject\debug\obj . You can't link to the .EXE as you've discovered.

This achieves the same results as the copy process that Chad suggested while avoiding the actual duplication of source code, which would be a really bad thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文