Getting started learning how to use GTest in Visual Studio 2022

Pages: 12
Looks OK.
Now you need to write a test for your fibonacci function.
In test.cpp I wrote the full path to Fibonacci.h so that the compiler recognizes it! :|
When running All Tests, I get the error: Unresolved External Symbol:
https://imgur.com/VelFgFM
1. Do not write the full path to the header file in your source code file! Just write #include "Fibonacci.h" , and make sure the "Additional include directories" in your project are set up correct! Also, use the placeholder $(SolutionDir) in order to avoid hard-coding full paths in your "Additional include directories".

(You should make sure that your project/solution builds correctly, regardless of where it is checked out!)

https://i.stack.imgur.com/VH1UA.png


2. As explained before, "Unresolved External Symbol" means that you did not link the library containing the actual implementation of your Fibonacci function into the project your are trying to build! As said before a couple of times, set up the dependencies between your projects and VS will take care of the rest!

https://i.imgur.com/RK81yu1.png


3. Look at the full example that I provided before, where all this is already done, pretty much:
https://www.mediafire.com/file/71hrb1vbavp0619/sample-project.zip/file
Last edited on
You need to add fibonacci.cpp to your test project.
Should look like this:
https://www.udrop.com/6ELy/Screenshot_1.png
BTW Your fibonacci function doesn't work.
make sure the "Additional include directories" in your project are set up correct
In FibGTest's properties -> C++ -> Additional include directories, the field is empty. What should I add, please?
...the directories where the required include files (header files) are located.

(See my previous post for details)

Furthermore: It generally is a good idea to have a sub-directory called "include" in each of your library projects. The "include" directory is supposed to contain all the public* header files, which are supposed to be #include'ed by other projects. Hence, that is the directory you'd add to "Additional include directories".


*the header files that represent the "public API" of your library
Last edited on
make sure the "Additional include directories" in your project are set up correct!
I added the path to Fibonacci.h in Additional include directories and now the compiler recognizes the header:
1
2
3
4
5
6
7
8
#include "pch.h"
#include "gtest/gtest.h"
#include "../Fibonacci/Fibonacci.h"

TEST(myTest, myFibonacciTest)
{
    EXPECT_EQ(Fibonacci(10), 34);
}


(You should make sure that your project/solution builds correctly, regardless of where it is checked out!)
When building, the errors come up.

set up the dependencies between your projects and VS will take care of the rest!
The FibGTest project has Fibonacci in its References subdirector and the project is also selected in FibGTest -> Project -> Project Dependencies:
https://imgur.com/reqz0Gp

you did not link the library containing the actual implementation of your Fibonacci function into the project your are trying to build!
My Fibonacci project is a typical console app not a .lib project, by the way. Is it now the source of the issue? :(

I wish to learn the process from scratch on my projects that's why I try to go step-by-stem with them.
Last edited on
Your "core" library containing the Fibonacci function (implementation) should be a static library (.lib).

Alternatively, it could be a DLL, but that is more complicated to work with.

Your "main" program as well as the GTest project can be console programs that link against the "core" library.


Once again, you can use the full example I posted before, an adjust/extend it to your needs:
https://www.mediafire.com/file/71hrb1vbavp0619/sample-project.zip/file

See also:
https://i.imgur.com/DvhIFzc.png
https://i.imgur.com/gU1gZaA.png
https://i.imgur.com/CRTg80A.png
Last edited on
So I should start again from scratch.
1) Create a Fibonacci project of type static library (.lib).
2) Add all three Fibonacci.h Fibonacci.cpp and main.cpp (with their code) to it.
3) Add a FibGTest project to the Solution Explorer and choose Fibonacci as the project for testing.
4) Add the path to the Fibonacci folder in FibGTest's Additional include directories.
5) Check FibGTest's Dependencies to see if Fibonacci has been selected.
6) Built the test.cpp project and then run "All Tests".

Have I forgotten something, please?

Again, thanks so much for your time and file as well. I will certainly use it too, but for now I'd like to walk through the process all so that I learn it completely for my project.
Last edited on
As for (2): I think you should not have a main() function or "main.cpp" in your static library.

Instead, create a separate console project (EXE file) for "main.cpp" that calls functions from the static library. Obviously, the "main" project will have to #include header files from the "library" project and link the actual library. For this to work, you will have to properly set up the "Additional library directories" in the "main" project and properly set up the project dependencies the ("main" project obviously depends on "library" project).

Then, in a similar way, you can add the GTest project, which also will use (link) the same "library" project.
Last edited on
kigar64551 wrote:
Your "core" library containing the Fibonacci function (implementation) should be a static library (.lib).

Alternatively, it could be a DLL, but that is more complicated to work with.

To reiterate for frek what I suggested/linked to earlier in this topic, how to create a static lib or DLL project with VS:
http://www.cplusplus.com/forum/general/283676/#msg1228261

Writing unit tests for C/C++ using VS:
http://www.cplusplus.com/forum/general/283676/#msg1228256

I don't just hand someone a "one size fits all" solution, they should learn how to fish on their own.

I'm gettin' too old (and grouchy) for repeatedly spoon-feeding someone, apparently. If this ancient and decrepit self-taught programming hobbyist can find info, most time quite easily, I get a bit annoyed when others seemingly don't want to make even a minimal effort to research a topic before they noisily clamor for answers.

Pax!
Instead, create a separate console project (EXE file) for "main.cpp"
I hoped it'd be possible to have a pack project (including header and the two source files) and a test project to test it. :( OK I will create the three projects the way above and will write the result here.

@George: I googled and youtubed a couple of days how to learn using Gtest on VS, but there is no complete tutorial to explain the steps from beginning to the end.
Created a static library project named Fibonacci and added Fibonacci.cpp and Fibonacci.h to it.
After that, created a console project named mainFib.cpp which contains main function and did the following for that:
- added the path-to-Fibonacci.h to its Additional Library Directories
- selected Fibonacci for the project's Dependencies.

After these built the projects. Faced the Unresolved External Symbol again. So right clicked on mainFib -> References (in Solution Explorer) and added Fibonacci as a reference. Now all three files build successfully!

But when running mainFib.cpp, I get this error: https://imgur.com/h8v4RUI
It's apparently because the project is a library!

Then created a google test project, named FibGTest and selected Fibonacci as the project for testing. Then added path-to-Fobonacci.h to its Additional Library Directories. Built successful.

Finally ran All Tests and it worked well. So added more EXPECT_EQ() and found out the function was not coded well. So changed Fibonacci.cpp and rebuilt it. Now for any build or running testes I once again face errors! :(
https://imgur.com/Sv6o5dy
But when running mainFib.cpp, I get this error: https://imgur.com/h8v4RUI
It's apparently because the project is a library!

Indeed. You can't run a static library (or DLL) on its own. It must be called from an executable.


Therefore the idea is to create:

1. A static library (.lib) containing the "shared" functions, such as your Fibonacci function.

2. A console application (.exe) containing the main() function, which links against the static library and which invokes the "shared" functions, such as your Fibonacci function, from that library.

3. The GoogleTest project containing the unit tests, which also links against the static library, so that it can call the functions to be tested, such as your Fibonacci function, from that library.


Note: You'll never run (1) directly. You just use (1) as an ingredient to build (2) and (3).


Finally ran All Tests and it worked well. So added more EXPECT_EQ() and found out the function was not coded well. So changed Fibonacci.cpp and rebuilt it. Now for any build or running testes I once again face errors! :(
https://imgur.com/Sv6o5dy

Make sure that the console application and the GoogleTest project are built using the exactly same header file that was used to build the static library. Also, make sure that the actual implementation matches exactly its declaration in the header file. Finally, make sure that you are linking the latest version of the library.

__________________

Full example, including GoogleTest:
https://www.mediafire.com/file/bjxzv8e4tbng190/sample_v2.zip/file

Looks as follows:
https://i.imgur.com/JfoBOVq.png
Last edited on
Thanks so much, it worked.
Two more questions! :)
First, why still can't I run my mainFib.cpp!? Whatever I try to run that exact file, the compiler just runs the .lib file and gives the error!
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Fibonacci.h"

int main()
{
    std::cout << fib(8) << '\n';
    system("pause");
    return 0;
}


Second, suppose that I have another function, named add2 with this definition right below the previous function that I want to run ALL Tests for, and its declaration in header:

1
2
3
4
void add2(auto& vec) {
    for (auto& v : vec)
        v += 2;
}

How to test that please?
Should I create a vector in test.cpp and run the function using it and then check the result!?
First, why still can't I run my mainFib.cpp!? Whatever I try to run that exact file, the compiler just runs the .lib file and gives the error!

You do not run "mainFib.cpp". You run the executable file that is created when building your "main" project. Your executable file is built from the "mainFib.cpp" source file and any additional libraries that you link in.

Also: When you click the "run" icon in Visual Studio (or press F5), then it will run the default project in the current solution. Be sure that the "main" project is selected as default project! (not the static library)

How to test that please?
Should I create a vector in test.cpp and run the function using it and then check the result!?

First of all, you add that new function to your static library, just like you did with the Fibonacci function. Again, with the implementation in the .cpp file and the corresponding declaration in the header (.h) file.

Then you can call this new function from your "main" project as well as form the GoogleTest project, just like you did with the Fibonacci function. And, yes, if that function expects an std::vector as parameter, then you'll need to pass one! Set up vector, pass it to the function, then check the results. Something like:

1
2
3
4
5
6
7
8
9
/* add2.h */
#ifndef _INC_ADD2_H
#define _INC_ADD2_H

#include <vector> // <-- note!

void add2(std::vector<int> &vec);

#endif 

1
2
3
4
5
6
7
8
/* add2.cpp */
#include "add2.h"

void add2(std::vector<int> &vec)
{
    for (auto& v : vec)
        v += 2;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "gtest/gtest.h"
#include "add2.h"

TEST(Add2Test, SampleTest)
{
    std::vector<int> vec;
    vec.push_back( 0);
    vec.push_back( 1);
    vec.push_back(40);

    EXPECT_EQ( 0, vec[0]);
    EXPECT_EQ( 1, vec[1]);
    EXPECT_EQ(40, vec[2]);

    add2(vec);

    EXPECT_EQ( 2, vec[0]);
    EXPECT_EQ( 3, vec[1]);
    EXPECT_EQ(42, vec[2]);
}


(BTW: I don't think you can use auto in the function declaration)
Last edited on
Thanks so much once again. :) You solved one my important issues (on how to use GTest for the first time)

What a pity that we can't test functions with arguments marked as auto! :(

And I don't thin it's needed! :)
#include "add2.h"

As well as, it's another pity that we can't send data structures to functions for testing. :(
What if our structure takes hundreds elements that we want to see each to know if its correct or not. :|
C/C++ still is a statically typed language.

You can use auto only where the actual type can be inferred unambiguously by the compiler, I think.

How, in void add2(auto& vec); should the actual (static) type be determined?
Last edited on
Topic archived. No new replies allowed.
Pages: 12