Author Topic: Unit testing in Code::Blocks  (Read 48571 times)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 4975
Unit testing in Code::Blocks
« on: September 09, 2009, 01:36:38 pm »
Dear All,

As a long lived promise I finally finished my first article on unit testing and Code::Blocks.

It can be found at : http://wiki.codeblocks.org/index.php?title=UnitTesting

There's much more to come, but for the moment it already provides a first introduction. It even contains a Test Driven Development example, extremely simple but the easier to grasp.
I am sure the text currently contains a lot of typos, just PM them to me, and I will fix them.

FYI : the unit test framework used is UnitTest++ : http://unittest-cpp.sourceforge.net/


Cheers.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 9649
Re: Unit testing in Code::Blocks
« Reply #1 on: September 09, 2009, 05:59:22 pm »
First, great job

I've read it diagonally, because it is too much for someone who knows how it works :)
I have a comment on something you've written there:
Quote
Pre-build and post-build steps are part of the build process ! Important consequence : if you want to debug something, the debugger will only kick off after Code::Blocks was able to carry out a successful build (including the pre/post builds steps). Therefor it is not wise to have the unit test executable being carried out as a post-bild step of the 'Debug' target. But as a post-build step of the Release target, our goal is met.
That is quite wrong. You 1000% want to have the tests as post-build step in the Debug target, because this is the primary target a developer is using.
And C::B should be fixed to allow this king of usage.
The fix is quite simple in the debugger if the build fails show an Annoying dialog with "Build failed, do you still want to debug the application?" (or something like that).
(I've started to do it but left it over :( )

p.s. you have many typos in the text  :lol:
p.s.s there is workaround at the moment "settings -> compiler & debugger -> debugger -> auto-build project to ensure up-to date" = off
« Last Edit: September 09, 2009, 06:01:05 pm by oBFusCATed »
<debugger plugin maintainer>
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 4975
Re: Unit testing in Code::Blocks
« Reply #2 on: September 09, 2009, 08:14:26 pm »
That is quite wrong. You 1000% want to have the tests as post-build step in the Debug target, because this is the primary target a developer is using.
And C::B should be fixed to allow this king of usage.
The fix is quite simple in the debugger if the build fails show an Annoying dialog with "Build failed, do you still want to debug the application?" (or something like that).
(I've started to do it but left it over :( )

To be honest I agree.

steve

  • Guest
Re: Unit testing in Code::Blocks
« Reply #3 on: September 10, 2009, 11:50:16 am »
Hi,

Well done on this article! It's great to encourage unit testing and show how it can be tightly integrated with code::blocks.

I use googletest (http://code.google.com/p/googletest/), another great framework for unit testing. The latest version of googletest, from the head of the repository, can be easily built with MinGW and Code::Blocks.

It would be nice if you could "generify" your article by separating out the UnitTest++ specific bits, and possibly indicating which parts of the project files would need to change if you want to use a different unit test framework.

Offline jimp

  • Multiple posting newcomer
  • *
  • Posts: 15
Re: Unit testing in Code::Blocks
« Reply #4 on: October 01, 2009, 11:39:15 pm »
Great job on the article. I have three questions.

In LeapYear-Step2 you said "it is good to put the tests in a nameless namespace (will be explained later on)". But you never explained it. Why is this a good idea?

What if you have a console program that has a main() function. This will conflict with the main() function in the test program used to call the unit tests. What are some ways to resolve this? (The ideal solution would be one that doesn't require modifying the original source.)

Do you plan on automating the UnitTest++ program in CodeBlocks? For instance a template that will generate the "main" test source and possibly a test source for each source or class in a project file. It could also link to the UnitTest++ library and compile the library file if needed. UnitTest++ also has the capability of GUI output. The test run can be activated from a toolbar, displayed in a GUI, and clicking on an error line can then display the source and indicate the line with the error (the CodeLite IDE has this capability). It would be a useful addition to CodeBocks.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 4975
Re: Unit testing in Code::Blocks
« Reply #5 on: October 10, 2009, 05:46:57 pm »
Great job on the article. I have three questions.

In LeapYear-Step2 you said "it is good to put the tests in a nameless namespace (will be explained later on)". But you never explained it. Why is this a good idea?

My mistake, I will add  this. Basically if you have 1 unit test project for a component, then you will be testing several classes, 1 test file per class.
Then you might have several :
Test(Constructor)
tests.

And if they are in each test file in a anonymous namespace, there will be no conflicts at linking time.

What if you have a console program that has a main() function. This will conflict with the main() function in the test program used to call the unit tests. What are some ways to resolve this? (The ideal solution would be one that doesn't require modifying the original source.)
I think it is best to have a dedicated project for the unit test(s), since it is intended to test units. When you have another main(), the main of the application, then the application might be to big to be a unit. The application itself can be tested. Unit tests focus on testing the building blocks, the smaller units.

Do you plan on automating the UnitTest++ program in CodeBlocks? For instance a template that will generate the "main" test source and possibly a test source for each source or class in a project file. It could also link to the UnitTest++ library and compile the library file if needed. UnitTest++ also has the capability of GUI output. The test run can be activated from a toolbar, displayed in a GUI, and clicking on an error line can then display the source and indicate the line with the error (the CodeLite IDE has this capability). It would be a useful addition to CodeBocks.
Good ideas, on my long term to-do list.


I use googletest (http://code.google.com/p/googletest/), another great framework for unit testing. The latest version of googletest, from the head of the repository, can be easily built with MinGW and Code::Blocks.

It would be nice if you could "generify" your article by separating out the UnitTest++ specific bits, and possibly indicating which parts of the project files would need to change if you want to use a different unit test framework.
As a first step I will do the article again, but then for the google-test. Thanks for pointing out google-test to me. I didn't know it.
In the mean time I already have done the first experiments, leading to a cbp file for it ;-)
« Last Edit: October 10, 2009, 05:49:38 pm by killerbot »

Offline Lefteris

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Unit testing in Code::Blocks
« Reply #6 on: December 12, 2009, 10:25:55 am »
Hello!
This was a very nice guide, I wanted to practice unit testing for a long time now in a structured and automated manner and this tutorial seems to be a very nice pointer to that direction.
But I happen to have a question. You say that we should have a separate project dedicated to unit testing, right? If our application is say..a big GUI project with different modules.. we would include the source of the main project from the unit-testing project and run all our tests, right? Is there anyway to make the tests run at building the main project and not the testing project?

In other words can we build the testing project (thus in effect performing all the tests again) every time we want to compile the main project?

Thanks again for witting the guide :)

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 4975
Re: Unit testing in Code::Blocks
« Reply #7 on: December 12, 2009, 12:42:16 pm »
There are several possible things to achieve your goal.

A first reflex of mine would be :

* GU app with several modules
* write unit tests for those modules (set of unit tests of the classes of those modules)
* when the app is build, we want a post build step that build all the unit tests too, and run them
  ** this can be done by creating a workspace, that consists out of the project file for the GUI app, and the project files of the unit tests. In CB you just choose to build the workspace (this can also be done by inoking CB on the command line, that way you can nicely incorporate it in a nightly build system)
  ** you could invoke as a post build step of the GUI app, to build the unit tests (which in their post build step will run), not sure exactly how to do this (calling CB again with command line parameters will do it), but I prefer the workspace approach.

Hope these from the top of my head ideas can but you on the right track ;-)

Offline lexis

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: Unit testing in Code::Blocks
« Reply #8 on: December 29, 2009, 04:01:01 pm »
Hello all.

It is good deal to describe how to utilize a common possibility for a particular purpose.
I am doing TDD in the same way too but with CxxTest. It requires an additional pre-build step to generate a cpp file from test definition in header.
But as I say above it is common tool: use pre/post build steps.

It would be really nice to have special Unit Test project type and wizard. It would provide developer with close integration of unit testing framework with CB like per test mapping to project targets for an example.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 9649
Re: Unit testing in Code::Blocks
« Reply #9 on: December 29, 2009, 06:49:34 pm »
You can make your own wizard (see the source of existing wizards) and then share it with us :)
<debugger plugin maintainer>
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

dirluca

  • Guest
Re: Unit testing in Code::Blocks
« Reply #10 on: January 18, 2010, 04:32:52 pm »
Dear All,

As a long lived promise I finally finished my first article on unit testing and Code::Blocks.

It can be found at : http://wiki.codeblocks.org/index.php?title=UnitTesting

There's much more to come, but for the moment it already provides a first introduction. It even contains a Test Driven Development example, extremely simple but the easier to grasp.
I am sure the text currently contains a lot of typos, just PM them to me, and I will fix them.

FYI : the unit test framework used is UnitTest++ : http://unittest-cpp.sourceforge.net/


Cheers.

Hi everybody,
I am pretty new to Code::Blocks (and to c++ as a whole) but I like it very much. I was expecially happy when I found out it was starting to embed the UnitTest++ facilities.

But I ran into some problems following the tutorial. Precisely I couldn't build the libUnitTest++ project. I got a
Code: [Select]
||=== UnitTest++, Debug ===|
||warning: command line option "-Wmissing-declarations" is valid for C/ObjC but not for C++|
||warning: command line option "-Wmain" is valid for C/ObjC but not for C++|
||=== Build finished: 0 errors, 2 warnings ===|
message (the same happens with the Release target). Running code::blocks 8.02 on windows 7 64bit.

Any help would be greatly appreciated. Please forgive me if it is a newbie silly question!

Luca

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 4975
Re: Unit testing in Code::Blocks
« Reply #11 on: January 18, 2010, 05:26:10 pm »
Yes, the compiler you are using (probably the one that came with CB 8.02) is too old, and can't cope with those compiler options.

I would suggest the following :
* uninstall CB 8.02
* get a nightly build from the forum : http://forums.codeblocks.org/index.php/board,20.0.html
* and get a more recent MinGW GCC (TDM's builds are very nice : http://www.tdragon.net/recentgcc/ )

An alternative is, keep using what you have, and remove those options from the UnitTest project.
But then you will miss out on all the new stuff of CB, and GCC ;-)

Offline eckard_klotz

  • Almost regular
  • **
  • Posts: 143
Re: Unit testing in Code::Blocks
« Reply #12 on: August 07, 2012, 12:55:33 pm »
Hello All.

Since a while I started to use UnitTest++ like described by killerbot in his tutorial. And the basics a working very well, especial that it is possible to jump directly into the code out of the build-log.

But for some reasons I have the need to get some more information somewhere. For example if I test a function with parameters it will be good to now them if there is a fail. The only possibility I know now is to have one single test-function for every set of test-parameters. But if I use a test-loop to iterate over an array of test-parameters, since the test-algorithm is always the same, I see currently no possibility to know which set of test-parameters result into the fails.

For example letís think about testing the LeapYear function in a loop where the given year will be changed from 1700 to 2700 we just have to provide a result-data array for the check in a small loop and all test can be done. But once we have a fail we have no information which test-value and which result-value where the problem.

Is there a possibility to provide UnitTest++ some additional information that will be defined while one test-cycle and that will be displayed together with the fail-statement in the build-log?

Best regards,
                       Eckard Klotz

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 4975
Re: Unit testing in Code::Blocks
« Reply #13 on: August 07, 2012, 03:01:34 pm »
I am not sure, but sometimes the test macros exist, with and optional message, and you can format/construct that message as you like.
But I am not sure if that is possible (I use different unit test frameworks from time to time, so I end up mixing information).

So in the example of the leapyear, you could have printed in the message the year you are testing.

Offline defyax

  • Single posting newcomer
  • *
  • Posts: 3
  • I'm Computer Science & Engineering dept Student :)
Re: Unit testing in Code::Blocks
« Reply #14 on: January 23, 2015, 03:05:25 am »
I really want to Unit text in code Blocks.
When I found this articles, very gratitude for your work.
But I can't get your project file in sourceforge.
How can I download your project?