Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: Slix on July 22, 2010, 05:42:54 am

Title: Using CodeBlocks projects with unit testing.
Post by: Slix on July 22, 2010, 05:42:54 am
I'm starting to unit test some of the code in my coding project, but I'm not sure how to set up the project files to make unit testing work.

I created a test project file in the root folder of my coding project, and I'm put the unit testing .cpp files in the test subfolder. (I'm using UnitTest++, but the advice here will probably apply to any unit testing framework.) Do I have to add all of my base project's headers and .cpp files (except the one with int main()) to the test project too? Also, how do I make it so that every time I build my base project, it also runs the unit tests?

Thanks!

EDIT: I am making a library, so that might make things simpler. However, I'd still like to know how to do this with console programs and stuff.
Title: Re: Using CodeBlocks projects with unit testing.
Post by: oBFusCATed on July 22, 2010, 09:14:39 am
http://wiki.codeblocks.org/index.php?title=UnitTesting
Title: Re: Using CodeBlocks projects with unit testing.
Post by: Slix on July 22, 2010, 08:35:29 pm
http://wiki.codeblocks.org/index.php?title=UnitTesting

I have already read that, oBFusCated. Unfortunately, it doesn't explain my question at all. It explains a lot about using unit testing with Code::Blocks, but it doesn't explain how to make your project work with it. The setup that the wiki explains only applies if you are making a project for ONLY unit testing. It doesn't explain how to set up two projects (one for your main project, one for your unit tests). The wiki uses one project file with a main() dedicated to unit testing, which isn't practical.
Title: Re: Using CodeBlocks projects with unit testing.
Post by: killerbot on July 22, 2010, 09:08:22 pm
this is quit simple, the wiki article describes on how to do it though, but you are right it doesn't show an example.

Typically your classes will be packaged in a library (static or dynamic), and in the end your application will use one or more libraries and will link with those libraries.

Well a unit test is nothing more then another application using one or more libraries (preferably only the one under test).

So in your unit test application add the include directory of your library, and link with the deliverable of the library project (aka the library binary).

So basically take the same approach as you would do when you have an application using a library.
Title: Re: Using CodeBlocks projects with unit testing.
Post by: oBFusCATed on July 23, 2010, 10:09:45 am
Slix:
The approach I take is a bit different, I don't create static/dynamic libraries. I just create the unit testing project then add all test_*.cpp files that do the tests, then I fix all undefined symbol errors by adding the relevant .cpp files.
This way I can set the unittest project as a dependency of the project I'm testing and when I build the main project the unit test project is build and the tests are run.
Title: Re: Using CodeBlocks projects with unit testing.
Post by: Slix on July 24, 2010, 02:09:37 am
this is quit simple, the wiki article describes on how to do it though, but you are right it doesn't show an example.

Typically your classes will be packaged in a library (static or dynamic), and in the end your application will use one or more libraries and will link with those libraries.

Well a unit test is nothing more then another application using one or more libraries (preferably only the one under test).

So in your unit test application add the include directory of your library, and link with the deliverable of the library project (aka the library binary).

So basically take the same approach as you would do when you have an application using a library.

But how would my unit test project know whether to look in the debug or the release folder of my base project?

Slix:
The approach I take is a bit different, I don't create static/dynamic libraries. I just create the unit testing project then add all test_*.cpp files that do the tests, then I fix all undefined symbol errors by adding the relevant .cpp files.
This way I can set the unittest project as a dependency of the project I'm testing and when I build the main project the unit test project is build and the tests are run.

But then I have to add all of my .cpp files to the project. :/
Title: Re: Using CodeBlocks projects with unit testing.
Post by: killerbot on July 24, 2010, 10:27:58 am
on your first remark. Yes, project dependencies is something that CB is still lacking a bit (compared for example to VS).

I usually do this (I assume always using the release build of unittest++ library) :

1) library project which has 2 targets : debug and release
2) unit test project which has 2 targets : debug and release

Here it comes :
* I save the workspace and in the workspace I set 2) to e dependent on 1)
* 2:debug:linker settings --> 1:debug:library delivery [meaning I already build 1 so I can select the lib or I manually type it in in case 1 is not build yet]
* 2:release:linker settings --> 1:release:library delivery
AND to really have full dependency checking :
* 2:debug [through project properties : build targets : external dependencies : external dependency files : specify again the 1:debug:library delivery]
* 2:release [through project properties : build targets : external dependencies : external dependency files : specify again the 1:release:library delivery]

Now all will go automatically :
* say the unit tests is the active project, and you changed some file of the library, when you build the unit tests it will check the dependencies and will rebuild it's buddy debug/release library and (recompile in case it was something of headers its including) and relink.

I admit, we should have something like project B depends on project A, and it targets have the same name all these dependency things come out of the box for free. But currently it is not. The 2 extra steps I mentioned after the "AND to really ..." are needed.

Note also : that project dependencies are stored within a workspace (just like VS), I personally would like this to be stored in the project itself, that way if I would use several workspaces I don't have to respecify this.
Title: Re: Using CodeBlocks projects with unit testing.
Post by: oBFusCATed on July 24, 2010, 09:00:09 pm
Note also : that project dependencies are stored within a workspace (just like VS), I personally would like this to be stored in the project itself, that way if I would use several workspaces I don't have to respecify this.
+1  8)
Title: Re: Using CodeBlocks projects with unit testing.
Post by: Slix on July 24, 2010, 10:42:28 pm
on your first remark. Yes, project dependencies is something that CB is still lacking a bit (compared for example to VS).

I usually do this (I assume always using the release build of unittest++ library) :

1) library project which has 2 targets : debug and release
2) unit test project which has 2 targets : debug and release

Here it comes :
* I save the workspace and in the workspace I set 2) to e dependent on 1)
* 2:debug:linker settings --> 1:debug:library delivery [meaning I already build 1 so I can select the lib or I manually type it in in case 1 is not build yet]
* 2:release:linker settings --> 1:release:library delivery
AND to really have full dependency checking :
* 2:debug [through project properties : build targets : external dependencies : external dependency files : specify again the 1:debug:library delivery]
* 2:release [through project properties : build targets : external dependencies : external dependency files : specify again the 1:release:library delivery]

Now all will go automatically :
* say the unit tests is the active project, and you changed some file of the library, when you build the unit tests it will check the dependencies and will rebuild it's buddy debug/release library and (recompile in case it was something of headers its including) and relink.

I admit, we should have something like project B depends on project A, and it targets have the same name all these dependency things come out of the box for free. But currently it is not. The 2 extra steps I mentioned after the "AND to really ..." are needed.

Note also : that project dependencies are stored within a workspace (just like VS), I personally would like this to be stored in the project itself, that way if I would use several workspaces I don't have to respecify this.

Thanks! This worked well. How do I make it output the unit tests to the build process like the wiki article described with this setup?
Title: Re: Using CodeBlocks projects with unit testing.
Post by: oBFusCATed on July 24, 2010, 11:21:59 pm
Run the test executable as a post build and make it print its output to stdout or stderr
Title: Re: Using CodeBlocks projects with unit testing.
Post by: Slix on July 25, 2010, 12:51:47 am
Run the test executable as a post build and make it print its output to stdout or stderr

Yeah, but which project's post build?
Title: Re: Using CodeBlocks projects with unit testing.
Post by: killerbot on July 25, 2010, 10:40:13 am
the unit test project build step, cause that on is producing an executable [$exe_output]
Title: Re: Using CodeBlocks projects with unit testing.
Post by: chocbudda on October 21, 2010, 01:59:51 am
OK:

I have followed the instructions laid out above and it all makes sense,

right up until I try to write a test.

i get the auto complete so im guessing ive linked things correctly ...however when i try to compile i get errors.

I am very new to C++ and i am sure i am missing something in how to reference the project I am trying to test...here's my code & the error message
(any googling has simply referred me to the link /search directories which i have already set, so perhaps it is simply a syntax problem on my end

any help appreciated (I've a feeling this is going to be one of those REALLY obvious problems once its pointed out to you situations.


\firsttests.cpp||In member function 'virtual void TestOneForTheRoad::RunImpl() const':|
obj\Debug\firsttests.o:\firsttests.cpp|8|undefined reference to `MyTic::getLimit() const'|

the code is:
Code

#include "firsttests.h"
#include "myTic.h"
#include "UnitTest++.h"

    TEST( OneForTheRoad)
    {
        MyTic theTick;
        float theLimit = theTick.getLimit();

    }

I've also tried
Code
        MyTic theTick = MyTic::MyTic();
        const float theLimit = theTick.getLimit();

Title: Re: Using CodeBlocks projects with unit testing.
Post by: chocbudda on October 21, 2010, 02:38:46 am
 :?

Ok...ummm I should edit up there, but I'm gonna be brave and admit...
firstly it was an assignment
secondly they gave us startup code ...with functions I never used...
(think i just gave the punchline away)

thirdly:
Well

I never really ummmm needed to write the... ummm well you probably get it

"undefined reference"..... makes PERFECT sense....NOW
 :oops: