Link to home
Start Free TrialLog in
Avatar of IainHere
IainHere

asked on

Unit testing C++ code (using Python?)

I'm about to start a medium sized project using C++ throughout.  I want to use unit testing to verify that certain things work / fail as expected, but I wondered about the methodologies used (I've used unit testing in other environments, but never with C++).

My project will be split into libs, and of course I can link to the libs and call functions from there, but the GUI is projected to be contained in an exe which links to the other libs - how would I call the functions in there?  Can I link to the exe (forgive the ignorance :-)

Also, do I have to use C++ as the testing language?  I've recently used Python for a couple of things, and would like to use that, since I feel it would be quicker to write these kinds of tests in that language.  It should be possible, because I know we can link them together, but will it be easier?  Has anyone tried it?

Finally, what frameworks do people use for this?  I'd prefer free, because I could have difficulty persuading my employer to spend money on this - I'm already expecting a job on my hands getting them to agree to unit testing (and automated builds, a decent bug tracking database, code reviews etc.)

I should point out that what I'm currently considering is use of either the Boost Test Library or CppUnit, if the testing must be done in C++, or PyUnit if I can use Python.  Other suggestions are of course welcome! Sadly, I've not seen any mention of people using PyUnit for C++ projects anywhere, so I guess it might not be wise...

Thanks for your help!
Avatar of RJSoft
RJSoft

I take it that you want to write your exe in python and then create test librarie/s (dll's) that your python exe can use with C++?

Out of curiosity are you looking for more speed? I am wondering why C++?

Anyway,  I was looking for game programming languages and I believe I recall Python was able to accept C++ routines. I believe you could mix.

Then if that is truley the case the best way to create dll's (the easy way) is to use _declspec(dllexport)

With _declspec(dllexport) all you have to do is create a .h file that declares what you want in your library and then a .cpp file that defines it.

Then in your Python application you simply include the .h file which allows you access to the functions in your test unit library.

I like it cause it's much simpler to use than creating a def file with exports statement etc...

// in .h file you declare the function

/////////////Test.h///////////////////
#ifndef Test_h
#define Test_h

_declspec(dllexport) int Test(int In);

#endif
///////////////////////////////////////



// in .cpp file you define the function

////////Test.cpp///////////////////////
#include "test.h"

_declspec(dllexport) int Test(int In)
{
if(In<10)return 1;
else
return 0;
}
////////////////////////////////////////


// Then use it in your python program

#include "test.h"

Test(2);
Avatar of IainHere

ASKER

Sorry, I obviously managed to describe that really badly :-(

The exe is also in C++ (because we're using the Stingray Toolkit - we need to interoperate with Excel and look pretty, with smart graphs etc).  The libs (they'll not be dlls, because we're compiling them all into the final exe) are also in C++ for several reasons - speed (complicated calculations), it's what the programmers know - actually, I don't know why we're doing the database connection in C++.  Consistency and knowing what you face, I guess.  Eg - what happens to a thrown exception when it travels through Python code to get back into a C++ lib?  How well do Python compilers work on Windows?  And then link into a VC++ project?  Note that those questions are not the subject of this one - I'm just explaining why we're not using other languages.  The difficult things will benefit from being done in C++, and the easy things are easy anyway.

Anyway, it was just the testing code I was thinking of putting in Python, since I know that they can be linked easily enough.  I was hoping for other people's experience of unit testing, and what they used.

It looks like either no-one bothers, or no-one wants to tell me.  Boo hoo!
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial