Unit Testing in C++

Started by
19 comments, last by Washu 12 years, 2 months ago
I have developed a game engine in C++ in Visual Studio 2010. I decided to run unit tests on my Engine class and some of its sub classes. Unfortunately c++ unit tests in an unmanaged environment are apparantly impossible and as c++ is inherently an unmanaged language I do not know why this isnt by default supported. I was expecting a Nebteans/Java stlye unit test code generation practice. I do not believe that it is impossible to generate unit tests, I have just failed to find any logical explanation from MSDN or any practical application the closest being (http://unittest-cpp.sourceforge.net/) I have not looked at boosts incantation.

I do not care what incantation I use but I do not have all week to write out every test scenario for several classes. I am in a situation where time is most certainly against me and unit testing is the absolute bottom feeder on the list of things I had expected/can afford to encounter in a professional development environment as having appauling support. Can any one guide me toward a rapid unit testing solution for C++ or show me a guide to hooking up the unit tests through VS2010.

I don't (have time to) care about version control, I don't (have time to) care about massive flexibility in tests, I do care about rapidly demonstrating some simple test cases in VS2010 or otherwise with my .sln and C++ files.

Please help me if you know any way to generate dummy test cases or any test cases in VS2010 when using C++ I dont even care if I have to run it with specific settings to demonstrate the test (I know that its a bad thing to do) so long as I can make it run I am happy.


Many thanks,

EnlightenedOne
Advertisement

Unfortunately c++ unit tests in an unmanaged environment are apparantly impossible

Very much false.

The xUnit suite and clones have been implemented in many languages, including C++. A few of them are CxxUnit, CppUnit, and CppUnitLite. There is also CUnit which is for C but works just fine in C++.

Reflection, the ability for something to peek inside the program and figure out what is a test, is a feature in just a few languages. This is probably the missing feature that causes you to believe it isn't supported by default.

In most languages you need to explicitly list the functions needed for your unit tests. This applies to C++ unit tests.

[color="#1C2837"]I have not looked at boost ... [color="#1C2837"]I do not have all week to write out every test scenario ... unit testing is the absolute bottom feeder on the list of things I had expected/can afford to encounter ... [color="#1C2837"]I don't (have time to) care about version control, I don't (have time to) care about massive flexibility in tests ... [color="#1C2837"]EnlightenedOne [color="#1C2837"] [/quote]


They are not something you can rush.


[color="#1C2837"]Unit tests generally have a roughly 1:1 correspondence in code size. If you have a 5000 line class, you should expect to have roughly 5000 lines of test code to properly exercise that class. Adding tests as an afterthought tend to increase the size and complexity of the tests. Adding it after expect to have 6000+ lines to test it, or to have large blocks that are not tested.


It is well researched that writing unit tests takes roughly the same amount of time as the initial implementation, and also that implementing code and test at the same time causes nearly double the time of a test-free implementation. You will be hard pressed to back-fill tests in less time than it took to write initially.


[color="#1C2837"]I'm not sure what version control has to do with this, it seems irrelevant.

[color="#1C2837"]And as for flexibility in tests, well, that's very telling.

[color="#1C2837"]Good luck Enlightened one. I hope you become so.
What do you mean by "[color=#1C2837][size=2]Java style unit test code generation"?

What do you mean by "[color="#1c2837"]Java style unit test code generation"?
I'm intruiged too... How do you test classes without writing test cases? How does a code generator know the usage and expected behaviour of a class automagically, in order to write a test for you?

[quote name='EnlightenedOne' timestamp='1302040002' post='4794777']
Unfortunately c++ unit tests in an unmanaged environment are apparantly impossible

Very much false.

The xUnit suite and clones have been implemented in many languages, including C++. A few of them are CxxUnit, CppUnit, and CppUnitLite. There is also CUnit which is for C but works just fine in C++.

[/quote]

I use cxxtest which uses Perl to generate the test case.
I'm happy with it. But maybe it's not under active development.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

I have error handling code within all classes and threaded classes I wish to test (including handling of various threaded classes having to do an emergency shutdown and others taking over their behaviour as best as is possible). I can just switch the error handling components to report unit tests failing using the strings I already have being output to report errors on a per class basis. Having unit tests on top of this is all to give evidence that the error handling actually works.

Unit testing would just be a matter of generating an empty method for every function WITH ACCESS TO THE CLASSES USED IN MY PROJECT (Which I cannot do in Visual Studios testing project for god knows what reason) and then making them do a list of generic tasks to put the engine class into the right state to run the tested function. The engine is all wrapped up so getting the state/context right for functions would not be hard. Every class has an error integer so its state can be tracked by an error handling thread, RETURNSUCCESS or RETURNFAILURE already exist and I have a std::string which retains every error recorded, I basically have all the functionality of HRESULT for my engine in place. So yes unit testing is a serious buisness but with the error handling deeply in place I have expected to be able to rapidly assemble unit tests.

This is where I am:
I have managed to find something. Executing "Test->New Test->Unit Test" via the menu I end up with a blank test program which I linked up to have all the resources that my actual engine class has. I then made it dependent on all the other projects being compiled and tried to include the EngineClass.h. It reached the dependencies the engine class includes and threw out an error.


[size="1"][size="1"]1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vadefs.h(85): error C4956: 'va_list *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vadefs.h(86): error C4956: 'va_list *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vadefs.h(86): error C4956: 'void *(va_list *,...)' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vadefs.h(87): error C4956: 'va_list *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(543): error C4956: 'const wchar_t *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(543): error C4956: 'const wchar_t *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(543): error C4956: 'const wchar_t *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(550): error C4956: 'const wchar_t *' : this type is not verifiable

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\crtdefs.h(550): error C4956: 'const wchar_t *' : this type is not verifiable


Every seperate include gave errors. Everything from strings to my physics library are pumping out several thousand errors for anything that is remotely like a pointer. How can I test classes in this test project if I cannot include any of the classes dependencies for the thing I wish to test?

This is lame because Java provides an easy to build off of framework from your code at a class level for JUnitTests you can then provide produce the contextual state for the test case prior to the test. You do not have to worry about dependencies what-so-ever and you do not have to write out every method per class function you wish to test. You do have to provide the intial context before each test case but this is a game engine its all wrapped up and ready to rock, I just need a test method for every classes function I have to run and the rest will be rapid. In Java I would not be hassled to reproduce all of the includes, can anyone explain what would need changing to get these test cases to be capable of including the classes/dependencies my project uses, would I need a dummy version of the entire project with different compilation configuration?

This is the worst thing I have ever encountered in VS because getting to the point where test cases are ready to be written would be instaneously done for me by Netbeans.

Thank you wqking in lack of any idea of what VS wants me to do to test my classes I will try cxxUnit or one of the others after I try one or two more things with VS method to see if I can tempt it to let me test my classes.

... can anyone explain what would need changing to get these test cases to be capable of including the classes/dependencies my project uses, would I need a dummy version of the entire project with different compilation configuration?

You need to either redesign the C++ language to be interpreted and provide runtime reflection capabilities (like Java), or write a compiler that generates test code as a back end (and modify MSVS to use that compiler). The latter is the easier route, mostly because it's at least possible. Try starting with GCC, all you need to do is write a back end for it.

Sounds like an engaging, worthy project. Sorry it's not the solution you're looking for.

Stephen M. Webb
Professional Free Software Developer

Here is evidence that I am not going to get a fair trial for unit testing http://msdn.microsoft.com/en-us/library/ms243171.aspx no C++ libraries appear to be managed hence the errors I was being thrown earlier trying to use any libraries. Simply put I just found out what /clr does (ish) I am fresh out of university and the idea of any language not having unit testing capacity in it is moronic. Now I have shot myself in the foot in that I planned to do unit testing in my reports and I have no functionality that will enable me to do so therefore I have been skrewed by a technicality. I don't even know how to turn /clr behaviour on.

So in short I would have to use an external library to get some simple behaviour.
Why are you so insistent with this completely incorrect notion that unit testing only works in managed languages? The two are utterly orthogonal.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


Why are you so insistent with this completely incorrect notion that unit testing only works in managed languages? The two are utterly orthogonal.


Rereading his posts, it seems pretty clear:

First post, my summary:
I made a game in VS2010. I can't make VS2010 unit tests work on my code, and I heard Eclipse had these awesome tools. I don't know what I'm doing. It is impossible. I'm DOOMED. I need to get a full suite of unit tests written immediately or I'm DOOMED. Here are a bunch of things irrelevant to my problem: Boost, Version Control, VS2010, Java. It's urgent, I need a tool. Do it for me or I'm doomed. I don't care if I need weird settings in the tool, I need the tool to do it for me because I don't know what I'm doing.

[color="#1C2837"]Follow up post, seemed to confirm that panic:
[color="#1C2837"]I need to give evidence to somebody that my code actually works. [color="#1C2837"]I don't know what unit tests actually do, I clearly don't understand what I'm doing, and I need "to be able to rapidly assemble unit tests", urgently. [color="#1C2837"]I have compiler errors, I don't know what I'm doing, and I need a tool to do it for me. [color="#1C2837"]I have incorrect assumptions about how JUnit works: It is like magic and sex and drugs combined. In fact, I think JUnit and Java are the same thing. Why doesn't C++ work like my misunderstanding of Java? [color="#1C2837"]VS2010 is dumb because it doesn't do what I think it should. If the tool doesn't do it for me, I'm DOOMED. [color="#1C2837"]Now I'm going to insult people who tried to offer help.

Third post, more of the same:
It is their problem, not mine. I did everything right. "[color="#1C2837"]I am not going to get a fair trial for unit testing", [color="#1C2837"]it is their fault. I just graduated so I know everything, and "[color="#1C2837"]the idea of any language not having unit testing capacity in it is moronic[color="#1C2837"]. Now I have shot myself in the foot[color="#1C2837"]". [color="#1C2837"]I am doomed, "skrewed", and don't know how to be a programmer. I can't take any responsibility and it is their fault and I'm doomed.




Yes it is a bit jaded, but that's how I see it.

[color="#1C2837"]Unit testing is a parallel work effort that is equal in size to the initial development. It requires significant work, and significant engineering effort. It requires skills in the testing theory and tools, no different than graphics programming requires skill in math and graphics libraries, or database programming requires understanding of databases and database libraries.

[color="#1C2837"]Several languages have some features that make writing and running unit tests a little easier. Sometimes people merge the good features and ignore the problems combining them to create an idea of unit testing that is dramatically different than current technology.

[color="#1C2837"]He is not the first or last to assume that Unit Testing is a magic pixie dust that gets sprinkled over code by the IDE and some other tools, and you can apply it liberally at the end of a project. [color="#1C2837"]You see it every few months on the TDD and xUnit newsgroups. Usually people there don't have the same whining and "I'm DOOMED" overtones, but they sometimes do have the "it is urgent because I agreed to a contract and didn't know what unit testing is" viewpoint.

This topic is closed to new replies.

Advertisement