GoogleTest: main() in different lib -> test cases not found - c++

I have various executable projects in my VS solution that contain various GoogleTest cases. I tried to reduce the code by having a separate .lib project that contains nothing more than the main() function and GoogleTest's initialization code. I could then link this into all the exe projects that contain the actual tests:
So:
---main.cpp in static lib project TESTMAIN---
#include <gtest/gtest.h>
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And
---accounttest.cpp in exe project TESTACCOUNT
#include <gtest/gtest.h>
struct BankAccount
{
int m_iBalance;
BankAccount(){}
};
TEST(AccountTests, BankAccountStartsEmpty)
{
BankAccount account;
EXPECT_EQ(0, account.m_iBalance);
}
However, when running TESTACCOUNT.exe, the unit tests are not picked up:
Running main() from gmock_main.cc
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[ PASSED ] 0 tests.
When explicitly adding a main() (with gtest init code) to my exe project, it does work. My hunch is that it has to do with the linker, but I am not exactly sure why this isn't working. I still would like to avoid adding a main+init to all my test projects..
Any suggestions on good ways to doing this would be really appreciated.
Ben

As #David and #Michal Walenciak noted, it worked by linking to gtest_main. In the end, I think I didn't need to link to a static lib, but to a DLL.

Related

Visual Studio Community Edition 2019 doesn't find any tests

I have created a C++ project called Googletest in Visual Studio 2019 Community Edition. In the project I have installed Gmock as a nugget(gmock 1.11.0). I have two cpp files(Googletest.cpp and Test.cpp).
Googletest.cpp
#include "gtest/gtest.h"
#include <iostream>
int main(int argc, char** argv) {
if (strcmp("test", argv[1]) == 0)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
else
{
std::cout << "Hello!" << std::endl;
}
}
Test.cpp
#include "gtest/gtest.h"
TEST(FooTestSuite, Foo1) {
ASSERT_EQ(1, 1);
}
The executable works properly. It runs the test or just says "Hello". The problem is that VS doesn't find any test, so that I can't use the test explorer. Does anyone know how to fix the issue? I have uploaded the project on github: https://github.com/tellass567/vs-googletest
If you have installed Test Adapter for Google Test and it can't discover Google tests, be sure the test names end with Test or Tests, otherwise Test Adapter is not able to discover unit tests
TEST(FooTests, Foo1) {
ASSERT_EQ(1, 1);
}
You may add alternative test name RegExes in Tools > Options > Test Adapter for Google Test to help Test Adapter to discover unit tests, see Trait Regexes.

How can I make catch2 run tests that reside in a static library?

My usual workflow with catch2 is to have a single console application that contains all the test cases and the tests 'runner'.
For example:
file1.cpp, file2.cpp contains tests:
#include <catch2/catch.hpp>
TEST_CASE("test", "[test]")
{
CHECK(true);
}
A single file main.cpp would contain the runner code:
#define CATCH_CONFIG_RUNNER
#include <catch2/catch.hpp>
int main(int argc, char* argv[])
{
return Catch::Session().run(argc, argv); // run tests
}
In order to reduce compilation time, I tried to move all files containing the tests (file1, file2 etc) to a separate static library project (Visual Studio).
But after doing this, catch fails to find any test cases:
catch main started..
Filters: [test]
===============================================================================
No tests ran
I tried putting the runner code inside a function that resides inside the static library, but that didn't help.
Questions:
How exactly does catch finds its test cases?
Why is this failing?
How can I fix it?

Linking google test to your main project

I am new to the gtest. I have followed a tutorial how to set it up in the VS 2105.
But all that I could find talked about how to build and link gtest.
I am passed that level. The code below runs and passes the first dummy test.
#include "gtest/gtest.h"
TEST(VI, simple) {
EXPECT_EQ(false, false);
}
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::cin.get();
return 0;
}
My question:
How do I exactly hook it up to my project that I want to test?
Both gtest project and my "code" project are in the same solution.
As far as I understood from reading numerous tutorials, I need 2 things:
1) to include my .h of the class I am about to test (easy and done)
2) To compile my "code" project into a static lib and then hook up
the lib to gtest project so I can create and test objects from the
"code" project.
I am struggling with the point 2. How exactly do I go about it?
Thank you for help in advance.
Add a new empty Win32 project to your solution, in its properties select Project Type "static library (.lib)"
Move all your sources except the main() function to that project
Add a reference to the .lib project to both your main application project and the google test project

Can I make google test return 0 even when tests fail?

I am calling a googletest in the post-build step of a C++ project in VS 2012.
Naturally, when any of the tests fail, the googletest command returns failure (-1), and the entire project is marked as a failure by Visual Studio.
I do not want that. I want the googletest to be run, and I want to see the results in the output, but I do not want to fail the project if not all tests pass.
Is there any flag that I can pass into googletest so that it always returns success (zero)?
Yes, you can make the test return 0 if you write your own main function.
I imagine you're linking your test executable with the special gtest_main library which is a very basic helper to allow you to avoid having to write your own main function.
It's pretty much just doing:
int main(int argc, char **argv) {
printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
The RUN_ALL_TESTS macro is the culprit which is returning -1, so all you need to do is stop linking with gtest_main and write your own main more like:
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
return 0;
}
For more info on this topic, see the GoogleTest docs.

GoogleTest run_all_tests not finding test fixtures

I have a C++ project that is going to be made up of only google tests. This project references another project (the project it is testing). I have a include and source folder for the header and implementation files. I am creating google test fixtures classes and split the header and implementation into the include and source folders. I have a main.cpp that contains the following code:
//main.cpp
#include "../inc/zeroEstimatorTest.h"
#include "gtest/gtest.h"
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
The problem I am having is that the RUN_ALL_TESTS() call is not calling my google test fixture. The test fixture is located in the implementation of the test class. It looks like this:
//zeroEstimatorTest.cpp
class zeroEstimatorTest : public ::testing:Test
{
...
};
TEST_F(zeroEstimatorTest, zeroTest)
{
...
}
The project builds and runs but the output is the following:
[0;32m[==========] [mRunning 0 tests from 0 test cases.
[0;32m[==========] [m0 tests from 0 test cases ran. (0 ms total)
[0;32m[ PASSED ] [m0 tests.
I am currently using Eclipse (for the first time) and I am on a Linux 64 machine.
Things I have done:
The zeroEstimatorTest class includes the "zeroEstimatorTest.h" at the top.
The #include "gtest/gtest.h" is at the top of all three files (main.cpp, zeroEstimatorTest.h, and zeroEstimatorTest.cpp)
Can anyone help?
Thank you very much!
The problem is that you're not setting the tests filter name try initializing google test with --gtest_filter= it can be done using the main function parameters .