I need to unit test some C++ objects that I've written that use a 3rd party C library. For reasons beyond the scope of this question, I can't call the 3rd party C library directly, and need to stub it out for the test suite.
For other parts of our unit test suite we use googlemock, but I don't think it can be used for C libraries. I can stub out the library manually, but prefer not to (partly due to laziness (its rather large), but mainly because its just a matter of principles).
So here's my question: is there a tool that generates stubbed code based on a C library header file? Once I have the stubbed-out code, I'll do some minor mods to it, then I'll link against it for the unit testing.
stubgen can generate stub members from header files, unless you have special requirements it should be able to do what you're looking for.
You cant wrap those calls in a class like described in http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Free_Functions
Then You can inject (in dynamic or static way) this class and set expectation on it.
Related
I work on a really old C++ project which has a huge DB I need to mock.
I am trying to mock ifstream library in order to test the control on files, the problem is that I can't change my legacy code so i am having a hard time finding the abilty to mock without changing any piece of the code
Thanks in advance
Mocking uses polymorphism (implements a given interface), so there are two choices:
use dynamic polymorphism when the library offers the option. For ifstream, this is not the case AFAIK.
use static polymorphism (templates), meaning that the legacy library offers the options to use something else than if stream.
I don't think your legacy application offers any of these options, so you will have to test it without mocks for now.
I am wondering what is the best/most common way to create a C++ library that I could create a wrapper for in another language.For example I if I create a library in C++ I'd like to create a wrapper for it in C# and then later on create a wrapper for the C++ library in Python.
I also want to be able to give the library to another person easily almost like a one file thing if that is possible? Also should I use a Dynamic Link Library or a Static Link Library? Extremely new to this sort of thing thanks.
A very common way that people simplify linking C or C++ code to other languages is through the Simplified Wrapper and Interface Generator. For the language runtimes it supports, it's a much easier to understand interface than integrating closely with each different language you wish to ship your library for.
In general, this will mean creating a dynamic library. Loading a dynamic library is a simple task for any runtime, but loading a static module would require modifying the language runtime. For that reason, it simply doesn't make sense to build a static module for either of your cited use cases.
For Python, with your build properly configured, you can ship your library module as a single dynamic library (barring licensing problems with libraries you link to). However, users will typically expect your module to be packaged using the standard Python setup-tools, as a .egg file.
You should be prepared to learn how each respective language community expects third-party packages to be packaged, to make the introduction of your library to them as easy as possible. Conforming to their expectations makes your library appear more professional, better designed, and easier to consider for their projects.
I would recommend, however, spending some time learning more about the Foreign Function Interface of a few different languages, to familiarize yourself with some of the peculiarities that SWIG sometimes can't hide perfectly. For example, passing a value to the other language requires "boxing" the C++ value into a value type, and then incrementing its reference count. SWIG does this for you, but it's sometimes the case where you have no choice but to write or debug some of that code yourself. Being unfamiliar with how those FFI interfaces work will hinder you substantially.
I have a C++ *.h file with three classes in it. The header file is for accessing a DLL. I have almost no C++ knowledge. However, I seem to recall from somewhere that you can't convert a *.h file to a Delphi unit that has classes in it. Is this true?
If it isn't true, and classes in header files aren't a problem, what is the general approach to converting the classes to Delphi?
C++ classes, just like Delphi classes, are not designed for binary interop.
A Delphi class can only be exported for consumption by other Delphi code, and then only in a package, and only when runtime packages are in use, and only when all modules use the same version of Delphi. In a similar vein, C++ classes can only be imported from a DLL by code compiled with the same tool chain that compiled the DLL.
So, it is not possible for your Delphi code to consume this DLL. As I see it you have the following options:
Persuade the supplier of the DLL to provide an interop friendly interface to the library. For instance, a plain functional C style interface, or a COM interface.
Write an adapter in C++, using the same compiler that was used to build the DLL. That would involve you importing the classes into your wrapper and exposing them to your Delphi code in an interop friendly manner. Again, plain C style interface or COM are the obvious choices.
In the sense of allowing you to use the DLL from Delphi code? Yeah, good luck with that. You know how you can't use Delphi classes in a DLL unless the client code is written in the same version of Delphi and even then it's usually a bad idea due to shared memory management gotchas? C++ poses exactly the same problem, only exponentially worse because there's no standardized ABI and there's all sorts of C++-language screwed-uppedness making problems for you.
The only real way to make it work reliably is with an interface that uses a standard ABI. If you have the source, try making a C interface that wraps the C++ interface. If not, ask the person who wrote the DLL to provide a C interface, and ask whoever made the decision to use this DLL why you're using a 3rd party library with no source available. :P
As commented in a previous answer the solution is using SWIG in order to generate the pascal binding. I started the development of the SWIG's pascal module but I had not time to complete it. Basically it works but it lacks all the test cases to be integrated into SWIG.
I used it in my personal projects and I was able to import complex library as GDAL.
I'm new to unit testing. I want to code unit tests in Qt, but my functions(login, request etc...) heavily depend on other resources such as a server.
How can I supply a block box in this situation?
Do you know any open source project which I can examine unit test classes for similar situations?
There's a linker trick that you can use. You know which methods and classes your code uses. Get a header files and declarations of those classes and make a small implementation of each that returns values you would like. Then compile those and link in right order. This will then use your own implementation of those methods and you don't need to have the right implementations that require server access.
See for example:
http://en.wikipedia.org/wiki/Mock_object
http://martinfowler.com/articles/mocksArentStubs.html
for more details.
PS. Advantage of this linker behaviour is that you don't need to declare your classes as interface first like for example, google mocking framework requires.
You need to use mocking. Google have a C++ mocking framework. You will also need to re-design your code to use interfaces in place of socket code, etc. which are replaced with mock objects when you run your tests.
Take a look at QTestLib. This is the unit testing framework that comes with Qt. Qt allows you to simulate events like mouse click and keyboard events (signals) as well as to snoop on events (signals) generated by your application.
Is there a unit testing framework for Microsoft Visual C++ 6.0?
I'm not a C++ programmer, but I think CppUnit does the trick. I'm sure there are others.
You can read this article for list of all C++ unit testing frameworks, and check which one is working with VS6.0
There's a VC6 port of Phil Nash' Catch test framework.
Catch is easy to start with as it is packaged up as a single header and has no external dependencies other than the C++ standard library.
Test cases are written as self-registering functions or methods, optionally divided into sections. Catch needs only one core assertion macro for comparisons, using standard C operators for the comparison - yet decomposing the full expression and logging both lhs and rhs values.
CppUnit requires more work than necessary, particularly the work it takes to create a new test case. The original author, Michael Feathers, published a simplified framework which has been used as the basis for others. I've used UnitTest++, and am very interested in googletest. Either one will let you create a new test case without having to declare it and manually add it to a suite; the frameworks do that for you.