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

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.

Related

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

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?

Can VS Code Test Explorer Catch2 extension automatically outdate tests when sources change

I'm using VS Code Test Explorer with python extension on a project. It is awesome. If I change any source files, the relevant tests in Test explorer will be highlighted as out of date so I know I have to run them again to check everything is ok.
I'm trying to do the same thing with a C/C++ project and the Test Explorer Catch2 extension. I have a simple meson project file to build my test executable(s).
It works well in that my tests are discovered and I can run them, HOWEVER if I change a source file it the tests wont show as out of date to indicate that they need to be rerun.
I know this is a dependency issue with meson (or what ever build system that VS Code understands).
Is there a Test Explorer (or VS Code) way of for determining which exes are out of date and need building (tests and build in general)?

Cannot add test in Netbean C++ project with existing code

So, this is what I have done.
I had a c++ project that I imported in NetBeans. Since the code had its old makefile I made NetBeans use it. I then added the logical folder Test Files (and included cppunit libraries in its linker). If I now try to add a cpp unit test Netbeans complains that I do not have a Makefile and does not let me create the unit test.
The exact error message is:
Makefile is not detected. Test targets will not be added to makefile.
How should I modify my old makefile so that Netbeans will recognize it?
Thanks for the help,
Michele
Additional information
I followed the instruction in http://www.oracle.com/technetwork/articles/servers-storage-dev/howto-add-unittests-ide-1731716.html#About on adding test to an unmanaged project but I cannot understand point 6. What should I add to my makefile? Can someone give me an example?
Right-click on the project name in the Projects pane - you'll get a pop-up menu with first line New, place cursor on this line - another pop-up menu will be opened, left-click on the CppUnit Test... in this menu. You'll get a pop-up window with some settings you can adjust.
After you click Finish in this window the NetBeans will create a directory with test skeletons and add new targets to your Makefile.
Good luck!
I ran into the same problem. It appears in NetBeans you MUST have the Test Package, which is where you would add your unit tests. If you create a C++ Application in NetBeans you will see the Test Package in your project. If you have a project that uses a Makefile that was not generated by NetBeans, then it appears that there is no way to add this Test Package to your project. I would be very happy to be wrong but this has been my experience and from all I have read it appears to be true.

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.