How do I Unit Test and mock a C project in Visual Studio - c++

I have a Project written in Visual Studio 2019 in C. I wish to Unit Test individual parts of my code and also be able to mock some functions off. What is the best way to go about this?
I have tried creating a new Unit Test Project within the same Solution as my production code, in the Native Unit Test framework for C++ in Visual Studio. The problem is I have to include my library files in every Unit Test project that I make which becomes tedious. I have tried the Fake Function Framework for C, but was unable to make it work.
I am open to new ideas, however it has to be in Visual Studio since my production code is placed in a project in Visual Studio.
Edit: A picture of my Solution Structure is shown in the picture here
I have to include the library files from my productionCode project inside my unit test project before it will build.

Not sure if this is an answer to my own question, but what I ended up doing was splitting my large project into smaller components and then write unit tests for each component instead of including library files from the large project. This way I could fake off the dependencies using the Fake Function Framework.
Thanks for the help

Related

test a C++ dll function

I am trying to test a dll that was created in C++, specifically to test certain functions. A few search results gave the solution as testing in visual studio by creating a simple unit test and referencing the dll as a project. But the solution is not very clear to me and there is no way to add the dll to the unit test project, as the only options are projects, solution, shared projects. I don't even see the browse button.
Does anyone have a solution or could you explain this solution provided here? I just want to be able to call the dll function, from a C++ class or project to test the input & output.
test dll
It is pretty straight forward actually.
In the DLL project you can create a native Unit test project and write test methods.
Here is the link to clear steps with screenshots - https://learn.microsoft.com/en-us/visualstudio/test/writing-unit-tests-for-c-cpp.
Edit: I am assuming you have access to DLL code.

How to add unit test for C++ in VS 2017?

I know I can use gtest and other frameworks but I am looking if there is built in solution.
Also please note that I would like to avoid any modifications to my project.
By this I mean that my project is main that uses some .h/.cpp pairs (or just .h in case of templated headers) and I would like to test the implementation of that code without making my project a shared library.
So ideally my current project would go from:
my_class.cpp
my_class.h
my_template_class.h
main.cpp // includes my_template_class.h and my_class_a.h
to
my_class.cpp
my_class.h
my_class_test.cpp
my_template_class.h
my_template_class_test.cpp
main.cpp // includes my_template_class.h and my_class_a.h, no changes compared to before
I know that tests run as "main" and that main needs to exist, but I hope VS is smart enough to generate boilerplate code for me (main that includes my_class_test.cpp and then runs the tests from it).
Also if you consider linking old documentation please note that it does not work for me in VS 2017( step
In Solution Explorer, in the shortcut menu of the project, choose Add,
New Item, and then choose C++ Unit Test.
does not work for me, I have no C++ Unit Test offered).
Try this one: C++ VS 2017 write unit tests in the same project
I found the link above here: C++ Unit Testing in Visual Studio. Which has some useful links at the end too.

How to setup seperate Boost Test project in Visual Studio 2010

I want to use Boost Test to unit test my code in Visual Studio 2010. I've downloaded and built the latest version of the library.
I've read a lot on the subject here and elsewhere on the internet and people seem to suggest having a second project within your solution exlusively for your tests.
Fine, sounds good. I'm having trouble actually setting this up however. I've yet to find a clear explanation of the best way to set this up.
Do I need to use a Project Reference to make my unit test project reference my main project?
If so, do I still need to add the Include & Source directories of my main project in the properties of my unit test project? If so, what's the advantage of using the Project Reference in the first place?
Do I have to have my main project output a library for my unit test project to link in? Again, I thought that Project References would make this unnecessary but it seems I don't really understand the Project References.
If at all possible could I get a very idiot proof, step by step procedure for setting up a Boost Test unit test project alongside a main project in VS2010?
Would I be better off going with the method laid out here (one project, different configurations to build tests or actual project exe):
http://blog.yastrebkov.com/2010/07/boost-test-setup-and-usage.html
Many thanks,
There is no magic behind setting up a Boost.Test project. Maybe because it's a regular C++ (executable) project in no way different from a "normal" application. This is what I do:
Create a new C++ project. I always choose Win32 Executable with precompiled headers. I have a naming convention, that all test projects using Boost.Test start with "tests.boost.testee_name..."
In "stdafx.h", add the include for <boost/unit_test.hpp> and define the BOOST_TEST_MODULE (I always choose the project name). Also, add all other includes for external components this project requires, e.g. other boost libraries, stl headers etc. This results in considerably faster compilation times.
The testee must be a library (dynamic or static). So "add reference" to all required dependencies. You can of course test header-only libraries, in that case do not add references.
Add source files to your test project, according to Boost.Test manual. The convention I enforce is one BOOST_FIXTURE_TEST_SUITE per file.
For convenience, I have a custom property sheet tailored for boost unit test, which I add to each boost test project. Among others it contains a post-build event, which runs the tests.
I have to add that, lately, I switched to MSTest with Visual Studio 2012 which allows a more comfortable way to manage the tests and test results. Nevertheless, for the most important parts of the software, I am still writing boost tests in order to ensure correctness with older toolsets and potentially other platforms.
Cheers,
Paul

Unit test for C++ win32 application

I have to write unit tests for C++ win32 console application developed on visual studio 2010.
Previously I wrote some unit test for a C++ DLL by adding dereference to test project.
But as you know cannot reference win32 exe files to test project.
So how can I use the 'methods in my project' inside the test project? I tired by adding whole project as reference. but not success.
please some guidance?
You need to create new project and add there source files of the code that you are planning to test. This means that for your testing you will have only executable that will contain both your dev and your test code.
Other way (not really recommended) is injecting test DLL into the process and looking for entry points into the destination code from inside this dll.

Visual Studio C++: Unit test exe project with google test?

Using Visual Studio 2010 C++. I'm experimenting with unit testing and decided to try Google Test (gtest). I have an existing project which compiles to an MFC executable (I'm also interested in how to test a project that compiles to a DLL). My understanding of the convention for unit testing is that you should create a new separate project for your tests. So I created a new project in the same solution for my unit tests. But how do I link the projects? Can I test arbitrary functions/methods of my exe project from my test project?
What is the conventional way to do this?
I think the best way to organize unitary test is:
Don't change your main project. The structure should be independent of your test actions. In my opinion, changing your project to a big static lib AND an executable is really not elegant.
Instead, add a post build action to aggregate all obj files into a static lib file that will be used ONLY by your test project.
Create a simple test project, linking to your test framework AND the static library you have previously generated.
Enjoy.
The main advantages is you don't touch the project you want to test and you don't include all source code to your test project.
To see how you can do that for visual studio, you can see this post: Linking to multiple .obj for unit testing a console application
Either put the functionality you want to test into a static library which is linked into both your test project and your MFC project, or put your files in both projects. The first is more complicated, but the second will cause you to compile everything twice....
I have prepared a github repo including Visual Studio 2015 solution in parralel of Billy's answer. You can use it directly without any additional requirement or dependency.
https://github.com/fuatcoskun/GoogleTestVS2015
I hope it helps...