Visual Studio, c++: run main() in dll? - c++

I'm building a .dll application in Visual Studio with C++. I'd like to be able to run some test code (using main() and std::cout) to the console as I write to ensure that the code actually does what it's supposed to do.
But apparently, you can only build a .dll application and not run it.
Surely there's gotta be a way around this?

Write a test driver, a real application (a dll is not an application, it's a library), that will link against your dll and that will execute your tests.
That's the usual pattern as well for Boost.test, GoogleTest and many others unit test frameworks.
(that's a big hint to use a unit test framework for what you are doing)

Related

C++ CppUnitTest changes DLL-Layout

I'm working on a C++ application with a vast amount of DLLs. Now I am trying to Unit test them with the Microsoft Visual Studio C++ Test Framework. I need to unit test the internal logic, as the DLL is rather big and only the most necessary functions are exported. So i followed this tutorial: https://learn.microsoft.com/en-us/visualstudio/test/how-to-write-unit-tests-for-cpp-dlls?view=vs-2019#sameProject and executing the tests works pretty fine but when starting the application for the next time, I figured out that the DLL did not get loaded anymore. The reason was that the DLL-Layout has been modified by the Unit Test.
Before adding the test:
After adding the test:
It's pretty clear why these methods appear, but is there
any possibility to move them to the bottom end of the dll?
any other possibility to add tests to a DLL that are able to test not exported functions without converting the .dll into a .lib?
Thanks in advance!

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.

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...

Organizing unit testing for existing code

I recently received as a new task to maintain and improve existing code written in C++ with MS Visual Studio. The code builds into an exe file (not a dll). I would like to add unit tests for the code and the problem I encountered is how to organize my testing projects. Basically I want to have 2 projects, one would be the original project I received and the second the testing project.
I saw on the Internet that usually when the subject being tested is built into a dll it's quite easy you have to statically link in your testing project the lib built from the main project and you have access to the function being tested. But how can this be done when the subject under test is an exe file?
Surely you can arrange the solution into projects that share code, where one project outputs to exe and the other(s) to DLL?
Whatever the project deliverable is, unit testing is testing the smallest units: the functions. A unit test typically follows the tripe A pattern: Arrange (create the environment for the test), Act (invoke the method under test), Assert (verify the method behaved as expected).
There are several possible project[s] structures: modify the project so that it compiles into a DLL, a production executable and a unit test program. The executable source has to be as small as possible, possibly just the main() function that create an Application object. It is also possible to have three projects, one for the DLL, one for the application and the third one for the tests.
An alternative is to embed the unit tests inside the executable and to have a mean to invoke them, e.g. with a special --unit-test parameter.