简体   繁体   English

起始代码:: blocks和UnitTest ++

[英]Beginning Code::blocks and UnitTest++

I'm about to start a C++ project but I'm stuck at the basics. 我将要开始一个C ++项目,但我仍停留在基础知识上。

I want to use the (linux) Code::Blocks IDE, and it's easy to create a normal project. 我想使用(linux)Code :: Blocks IDE,创建普通项目很容易。 However I want to do TDD using the UnitTest++ framework, and I don't know how to set everything up cleanly. 但是我想使用UnitTest ++框架进行TDD,而且我不知道如何将所有内容进行干净设置。

I've already asked a question about where to put the UnitTest::RunAllTests() command, and they told me the best place is the main() of a separate program. 我已经问了一个关于将UnitTest::RunAllTests()命令放在哪里的问题 ,他们告诉我最好的地方是一个单独程序的main()

How do I go about doing this in Code::Blocks? 我该如何在Code :: Blocks中执行此操作? I think I need to create 2 projects: 我想我需要创建2个项目:

  • The "real" project with its own main() ; 具有其自己的main()的“实际”项目;
  • The unit testing project containing the tests and the main() with UnitTest::RunAllTests() inside. 包含测试和main()的单元测试项目,其中包含UnitTest::RunAllTests()

Then somehow have the first project build and run the second during its build process. 然后以某种方式构建第一个项目,并在其构建过程中运行第二个项目。 I don't know how to do that yet but I can find out on my own. 我还不知道该怎么做,但是我可以自己找到答案。

My questions are: 我的问题是:

  1. this is the right method? 这是正确的方法吗?
  2. do I have to create also a project for the UnitTest++ framework, in order to let other people build it on other platforms? 我是否还必须为UnitTest ++框架创建一个项目,以便让其他人在其他平台上构建它? Or is dropping the complied library in the project's path enough? 还是将已编译的库放在项目路径中就足够了?
  3. how can I organize the directories of these projects together? 如何一起组织这些项目的目录? It'd be nice to put the tests related to each package in the same directory as that package, but is it ok to have multiple projects in the same directory tree? 最好将与每个程序包相关的测试放在与该程序包相同的目录中,但是可以在同一目录树中包含多个项目吗?

I'll partly answer my own questions, as I've managed to get everything working. 我将部分回答自己的问题,因为我设法使所有工作正常进行。

  1. Following the instructions on the official documentation page , I've put the UnitTest++ folder with the compiled library and all the source files in my project's path. 按照官方文档页面上的说明 ,我已将UnitTest ++文件夹和已编译的库以及所有源文件放在项目的路径中。
  2. Then I created a test project for all the unit testing, with a main function containing the famous UnitTest::RunAllTests() . 然后,我为所有单元测试创​​建了一个测试项目,其主要功能包含著名的UnitTest::RunAllTests() I put $exe_output as a post-build process here, in order to have the tests executed automatically every time I build this project. 我在这里放置$exe_output作为构建后的过程,以使每次构建此项目时自动执行测试。
  3. I created the "real" project where my code to be tested will go. 我创建了一个“真实”项目,将要测试的代码将在该项目中进行。 In the build settings I specified the test project as a dependency of the real project, so that every time I build the real one, it also builds the test project first. 在构建设置中,我将测试项目指定为真实项目的依赖项,因此每次构建真实项目时,它也会首先构建测试项目。

With these settings I can work on my tests and on the real code, and I only have to build the real one to have the updated tests executed. 通过这些设置,我可以在测试和真实代码上进行工作,并且只需要构建真实的代码即可执行更新的测试。 Any failing test will also make the build fail. 任何失败的测试也会使构建失败。

Now two questions remain: "is this the best approach?" 现在还有两个问题:“这是最好的方法吗?” and "right now each project lives in a different directory. Is it wiser to leave it this way or should I put each test in the same folder as the real code to be tested?" 和“现在每个项目都位于不同的目录中。以这种方式保留它是否更明智?还是应该将每个测试与要测试的真实代码放在同一文件夹中?”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM