Go Premium for a chance to win a PS4. Enter to Win


Unit testing C++ code (using Python?)

Posted on 2003-11-03
Medium Priority
Last Modified: 2006-11-17
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!
Question by:IainHere

Expert Comment

ID: 9675808
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

#ifndef Test_h
#define Test_h

_declspec(dllexport) int Test(int In);


// in .cpp file you define the function

#include "test.h"

_declspec(dllexport) int Test(int In)
if(In<10)return 1;
return 0;

// Then use it in your python program

#include "test.h"


Author Comment

ID: 9677027
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!

Expert Comment

ID: 10242470
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.

EE Cleanup Volunteer

Accepted Solution

Computer101 earned 0 total points
ID: 10300253
PAQed, with points refunded (250)

E-E Admin

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question