Visual Studio Community Edition 2019 doesn't find any tests - c++

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.

Related

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?

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

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.

Why does this usage of boost test - single header variant crash?

I use the boost Unit Test Framework (version 1.59) using the Single Header Variant. By running the code underneath in Visual Studio 2013 I get a debug assertion. (Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Any idea why?
http://www.boost.org/doc/libs/1_59_0/libs/test/doc/html/boost_test/adv_scenarios/single_header_customizations/entry_point.html
#define BOOST_TEST_MODULE MyTest
#define BOOST_TEST_NO_MAIN
#define BOOST_TEST_ALTERNATIVE_INIT_API
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(boo)
{
}
int main(int argc, char* argv[])
{
return boost::unit_test::unit_test_main(init_unit_test, argc, argv);
}
So I left a command line parameter in the Project's Configuration Properties. For some reason the boost unit test framework crashes when running this program with command line parameter 'boo'...

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 . 

How do I implement c++ unit testing with googletest in Xcode 4.6?

I am strougling to set up c++ unit testing with googletest for Xcode 4.6. The instructions that come with the download of googletest were written in 2008 and don't correspond to the current Xcode interface. I think I got googletest to compile (which wasn't trivial,) but now I am having issues following this tutorial to get a unit test to work. How do I implement unit testing with googletest in Xcode 4.6?
When you have already got the gtest framework to compile its actually not that hard anymore.
Write your testcases using the macros as described here: https://github.com/google/googletest/blob/master/docs/primer.md#simple-tests
And then to run all your tests:
#include "gtest/gtest.h"
int main(int argc, const char * argv[])
{
testing::InitGoogleTest(&argc, (char**)argv);
return RUN_ALL_TESTS();
}
I've created a seperate build-target for Unit tests where this is the actual main.cpp
This works for me using the current Xcode version (4.6.3)