Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

705 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