Unit Testing DLL project on Windows using Google Test in VS 2022 - c++

I am trying to write unit tests for a larger, old project using GTest. The project compiles as a dll. Here is what the setup looks like: There are several projects inside the solution, and I am trying to add a Test Project to unit test on of the several projects.
As a proof of concept, I created a HelloWorld project, and a HelloWorldTest project in a different solution. To emulate my larger project, I changed the Configuration Type from .exe to .dll and ran into some errors. But these errors were resolved exporting classes/functions using __declspec(dllexport) along with including .lib in the project, and I am able to build and run the exe of the HelloWorldTest project.
I am trying to follow the same approach for my larger project by painstakingly adding EXPORT statements before classes/functions. However, I run into errors when I include required header files in the Test Project since the header file has other include statements which the Test Project does not understand about. For example, if I include "..\larger_project\custom_math.h" but custom_math.h has something like include "..\..\algo\somefile.h". (I've seen posts about referencing one project to another and have already done the same for my project.)
How do I resolve the issue of chained header file includes? I believe if I can resolve this, I should be able to create objects and start testing in my Test Project.
is there a better way to do this than adding EXPORT statements before every class/function?

Related

How can I do C++ Unit Tests in Netbeans?

I attempted to follow the tutorial here to get Netbeans unit testing setup for C++. It talks about a "Select Elements" portion of the test setup wizard in which one selects the parts of the actual project that are available to the test. This stage in the wizard is absent with the wizard starting on "Name and Location":
When I create a unit test without this part of the wizard, I am unable to include any headers from my project unless I include it in the format #include "../Header.h" and, when I do that, g++ has problems linking the included header to the corresponding implementation.
What am I doing incorrectly and what do I need to do to have my unit tests work correctly?
NOTE: I am trying to use cppunit, but, the dialog is missing "Select Elements" for all 4 available testing formats (simple C, CUnit, simple C++, CPPUnit).
The "Select elements" part of the wizard comes up when you activate the wizard by right-clicking the .cpp-file or the .h-file and click "Create Test".
If you do "New .. / C/C++ tests/.." this part of the wizard isn't there.
I am unable to include any headers from my project unless I include it in the format #include "../Header.h"
There seems to be an oversight by the template/wizard writers to not address the fact that your tests lies in a directory tests. You can fix this by adding your project folder as an -I include directory switch. Use an absolute path.
Linking should not be a problem since the compiler targets the Build directory. If you do have problems with linking check the properties of the linker options in your CPPUnit test project folder.

Configuring Google test project for DLL library

I have DLL plug-in, which I'd like to test and launch on TeamCity: it contains .h and .cpp files
What is the right strategy to test this DLL:
Create a new Test Project in the same solution, configure 'include directory' to see sources and copy DLL project files to Test console project. In this case I have the same solution but in console mode, which I can test in normal way. But if my DLL project will change I need to synchronize source files.
Create export function in my DLL, like 'runTests()' which will produce XML file with results. TeamCity will process this? And how should it run this? And some stuff function appears in release DLL...
To unit test our libraries, we create standalone unit testing console executables. so:
For each library , we create a console executable testing each method in the API.
Each source file is obviously added to a SCM so modifying files will automatically be reflected into the unit testing program;
All this (source updates, compilation, unit testing and doc generation) is added to our CI server (Jenkins) so that all libraries, for all unit testing programs are always recompiled from scratch;
The documentation for the library API is constructed with Doxygen using snippet from this program. This has a nice side-effect: changing the API will break your unit tests. So you have to fix your unit tests so your documentation is always up to date.

Have visual studio 2012 automatically copy referenced project's references (C++)

I'm working on a VS2012 solution. This solution has a C++ project as a main project. The main project references several external libraries, and I might add a couple of extra libraries as well in the future.
The solution also has a C++ unit test project to test the code. I have a reference in the unit test project to the main project.
However, unlike with C# or other CLR-type projects, this somehow doesn't mean that the references in that project are copied. (might be that in those projects a .dll reference is all that seems to be necessary to get the job done)
I'm currently unable to include files from the main project, nor are the libraries that the main project has included or which it is linking to included in the test project.
I'd like to know if there's a way to access all the main project's files, include search directories and library references without editing the unit test project's properties all the time. Is there a way to do that? I've tried setting Use Library Dependency Includes to true in the unit test project's settings, but it keeps getting reset to false.
Will enabling copy local or copy local sattelite assemblies help solve this?
Copying output of native project referred in other projects does not work like in .NET because the mechanisms are different. The one solution that comes to my mind now is adding a post-build event action to copy the output to the desired folder.

Using sub projects in Visual Studio

I am quite used to Linux development and Makefiles, and started using (Windows and) Visual Studio not so long ago.
What I want to do is (I think) quite simple, but I can't seem to find how to do it using Visual Studio.
I have to write an application, which I can divide into several independent sub-parts. I want to work incrementally, and create several projects that together with a main file will end up with my full project.
What I basically want is to be able to write a small project, have a main for it so that I can fully test it, and use it as a dependency for the next project. In this case, the smaller main would be deactivated (or I can comment it), and I would just change the startup project.
If I find a bug in a subset while writing my bigger software, I could just change the startup project and solve it at a smaller scale.
Well, that's what I do all day long in Python and Java.
I tried to create new projects into my project, but I always end up having linking problems, where my main projects knows about the files in the sub projects, but not the smaller ones, etc. . .
So, is there a guide somewhere I can find to work this way ?
Thank you
For individual projects:
Every individual project property sheet has a C++ options page. Here you can specify the 'Addional Include Directories' in a comma separated form.
Similarly, there should be a property sheet for Linker where you can specify the 'Addional Include Dependencies' and the names of the libraries it depends on.
For linker dependencies of the main executable:
Go to that main project, go to its properties, go to common properties and select 'Framework and References'. This should give you a list of all the projecs that are in your solution. Keep adding them and Visual Studio should add the right linker flags automatically for you.
Sorry, have no access to the computer now else would have provided exact steps. Visual Studio can get tricky sometimes but once you use it, you'll be amazed by what it can do for you. Personally, I love it.
Hope this helps.
Thanks to Vaibhav, I was able to find a solution:
I had to :
change subproject type to lib instead of exe
Add the subprojects as project dependencies in the main project (this just sets the build order)
Comment out the main of my subprojects, to keep only one active.
Add each subproject include directory in the include repos of the main project, so that the compiler can find the header files
Add the general directory as a dependency for the linker (in this case, it is not the debug/release folder of the subprojects, but the output directory of the complete project).
Add the names of the lib files of the subprojects in additional dependencies of the linker of the main project.
To make it simple, the project dependencies capability of VS2010 just changes the order in which the projects are built. . . I miss Eclipse.
If I find a bug and want to test on of the subprojects, I have to :
change the startup project to be the subproject I want to change.
uncomment the corresponding main
change the project type to be exe instead of lib to be able to compile it.
Debug, and do everything back again to continue working on my main project.
Quite boring, don't you think ?
Looks like you trying to do manual unit testing. Use something like Google.Test. You need to make test project for every lib.
We have directory with static libs projects. Another directory with tests projects. Every test solution contains one exe project and few existing lib projects. Every project have configured dependencies. You dont need to set additional dependencies of the linker manually (paths are evil, out dir for the lib file will be taken from project settings), open project properties with right mouse button, Common properties, Add new reference and select lib project. You only need to set additional include dirs.
When you find new bug - just open test project for the library with bug, add code which cause the bug, fix it, and be happy (and sometimes run all test). And even better - use TDD.

Using Boost C++ unit tests with visual studio and can't find .cpp files in other project

I have a solution where I've added and setup boost unit tests. The problem is that I have another project I'd like to test that has some classes in it. In fact, that project is the main reason I added boost.
My project that needs testing is set to output as a .dll. And the problem is that, whenever my tests project needs to access code from the other projects, it can access the header just fine. However, if the header has unresolved code in it that's otherwise resolved in a .cpp file of the project with the objects, I receive a linking error. Is there a way around this? I'd ideally like to keep my objects in my other dll and then test them in my tests project.
You're probably not telling your test project where to find the symbols. Either link against your production code's .lib manually, or you can add the project as a reference and VS will link your projects automatically.
Go to your project's properties, under Common Properties choose Framework and References. Click the Add New References... button, and select your other project. Since it's a .dll, you then want to set Link Library Dependencies to False (save and reopen the dialog, that setting seems to be buggy).
The problem was that the visual studio compiler couldn't link to the CPP in the other files. I had to add the CPP files to the boost project as well using the existing files option.