问题
I'm about to start a C++ project but I'm stuck at the basics.
I want to use the (linux) Code::Blocks IDE, and it's easy to create a normal project. However I want to do TDD using the UnitTest++ framework, and I don't know how to set everything up cleanly.
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.
How do I go about doing this in Code::Blocks? I think I need to create 2 projects:
- The "real" project with its own
main()
; - The unit testing project containing the tests and the
main()
withUnitTest::RunAllTests()
inside.
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:
- this is the right method?
- do I have to create also a project for the UnitTest++ framework, in order to let other people build it on other platforms? Or is dropping the complied library in the project's path enough?
- 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?
回答1:
I'll partly answer my own questions, as I've managed to get everything working.
- 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.
- Then I created a test project for all the unit testing, with a main function containing the famous
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. - 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?"
来源:https://stackoverflow.com/questions/7462752/beginning-codeblocks-and-unittest