Solved

Unit testing C++ code (using Python?)

Posted on 2003-11-03
4
1,036 Views
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!
0
Comment
Question by:IainHere
4 Comments
 
LVL 3

Expert Comment

by:RJSoft
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

/////////////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);
0
 
LVL 4

Author Comment

by:IainHere
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!
0
 
LVL 9

Expert Comment

by:tinchos
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

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

Computer101
E-E Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now