When there is a failure in ASSERT_ macro of gtest, the TearDown function will be called or not? - assert

Will the TearDown function be called when the ASSERT_EQ fails in google test? Or it just cancel that test case and move to the next one without TearDown? This is because in my TearDown function, I need to do something to properly shutdown the test function, so I'm afraid that this ASSERT will make my test not independent.

It's not hard to satisfy yourself that TearDown is always run, regardless
of whether an ASSERT_... macro fails:
testcase.cpp
#include <gtest/gtest.h>
#include <iostream>
struct foo : ::testing::Test
{
void SetUp()
{
std::cout << ">>>" << __PRETTY_FUNCTION__ << " was run " << std::endl;
}
void TearDown()
{
std::cout << ">>>" << __PRETTY_FUNCTION__ << " was run " << std::endl;
}
};
TEST_F(foo,bar)
{
ASSERT_EQ(1,0);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Compile and link:
g++ -o testcase testcase.cpp -lgtest -pthread
Run:
$ ./testcase
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from foo
[ RUN ] foo.bar
>>>virtual void foo::SetUp() was run
testcase.cpp:19: Failure
Expected equality of these values:
1
0
>>>virtual void foo::TearDown() was run
[ FAILED ] foo.bar (0 ms)
[----------] 1 test from foo (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] foo.bar
1 FAILED TEST

Related

Is there a way to detect command line arguments not running tests?

Using the googletest framework I want to write my own main function. Basically some custom initialization step needs to happen before RUN_ALL_TESTS is called. I'd like to skip this step, if the command line parameters for googletest indicate, no tests should be run (e.g. if --gtest_list_tests is passed).
Is it possible to retrieve this kind of information from the test framework without the need to parse the parameters myself?
What I'd like to accomplish:
#include <gtest/gtest.h>
bool RunAllTestsDoesNotRunTests()
{
// should return false, if and only if RUN_ALL_TESTS() runs any test cases
// implementation?
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
if (!RunAllTestsDoesNotRunTests())
{
DoExpensiveInitialization();
}
return RUN_ALL_TESTS();
}
Is this even possible? I've tried to identify members of ::testing::UnitTest, that would allow me to retrieve this kind of information, without any success so far though.
Am I going at this the wrong way? Preferrably I'd like to avoid lazily doing the initialization via fixture or similar logic requiring me to adjust every test case.
There are various flags that might prevent google test from running any tests. I can think of at least the following:
--gtest_list_tests is passed.
--gtest_repeat=0 is passed.
--help or other forms of it like -h or any unrecognized flag with gtest prefix is passed See here.
You can test these cases by:
The first can be checked using ::testing::GTEST_FLAG(list_tests). See here.
The second can be tested using ::testing::GTEST_FLAG(repeat).
The third can be checked by reading the global variable g_help_flag. While practical, this is not ideal because it's in the internal namespace and might change in future releases.
Another alternative is to parse the command line arguments yourself.
So assuming you want to be practical, one way to get to what you want is this:
// Defining g_help_flag as an extern variable
namespace testing {
namespace internal {
extern bool g_help_flag;
}
}
bool RunAllTestsDoesNotRunTests()
{
// should return false, if and only if RUN_ALL_TESTS() runs any test cases
// implementation?
return ( ::testing::GTEST_FLAG(list_tests) ||
::testing::GTEST_FLAG(repeat) == 0 ||
::testing::internal::g_help_flag);
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
if (!RunAllTestsDoesNotRunTests())
{
// DoExpensiveInitialization();
std::cout << "Expensive initialization!" << std::endl;
} else {
std::cout << "No Expensive initialization!" << std::endl;
}
return RUN_ALL_TESTS();
}
Live example: https://godbolt.org/z/P36fde11T
command line arguments are detected using the GTEST_FLAG macro. An example of what you're trying to do might look like:
#include <gtest/gtest.h>
TEST(equality, always_passes) {
EXPECT_TRUE(true);
}
bool RunAllTestsDoesNotRunTests()
{
return ::testing::GTEST_FLAG(list_tests);
}
void DoExpensiveInitialization() {
std::cout << "Boop Boop, Beep Beep" << std::endl;
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
if (!RunAllTestsDoesNotRunTests())
{
DoExpensiveInitialization();
}
return RUN_ALL_TESTS();
}
When compiled and linked appropriately, it can be run as:
$ ./a.out
Boop Boop, Beep Beep
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from equality
[ RUN ] equality.always_passes
[ OK ] equality.always_passes (0 ms)
[----------] 1 test from equality (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
$ ./a.out --gtest_list_tests
equality.
always_passes
i.e. you don't see the Boop Boop, Beep Beep as the function was not called.
Now if you have something that you want to have run once, if you're running tests, then adding it to the testing::Environment would also do the trick:
#include <gtest/gtest.h>
class MyEnvironment: public ::testing::Environment
{
public:
virtual ~MyEnvironment() = default;
// Override this to define how to set up the environment.
virtual void SetUp() { std::cout << "Env Beep Beep" << std::endl; }
// Override this to define how to tear down the environment.
virtual void TearDown() {}
};
TEST(equality, always_passes) {
EXPECT_TRUE(true);
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
auto* env = new MyEnvironment();
::testing::AddGlobalTestEnvironment(env);
return RUN_ALL_TESTS();
}
When executed, it will also cope with filters (the previous example would not be able to cope in that case):
$ ./a.out --gtest_filter=equality\*
Note: Google Test filter = equality*
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
Env Beep Beep
[----------] 1 test from equality
[ RUN ] equality.always_passes
[ OK ] equality.always_passes (0 ms)
[----------] 1 test from equality (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
$ ./a.out --gtest_filter=equality\* --gtest_list_tests
equality.
always_passes
Again, not that the Env Beep Beep does not appear if you don't run any tests (you can check with a filter like --gtest_filter=equality, and you won't see the Env output in that case.

GTest: How to pass variable to forked process in death test when using "threadsafe" style

I am trying to write a death test to verify that a log file contains an expected output after the program runs into a segmentation fault. Since the program is multi-threaded, I have to use the "threadsafe" death-test style (our team has observed deadlocks in similar tests otherwise). The problem is that when using this style, I cannot get the child process created for the death test to read a value that was set in the parent process, as it would normally be the case after a fork/clone in the "fast" style.
The following example illustrates the problem, and I am using the process PID as an example. The goal here is to get the same value printed in the "inside" as well as in the "outside after" messages.
#include <iostream>
#include <gtest/gtest.h>
#include <unistd.h>
using namespace std;
void makeSegfault() {
volatile int* x = nullptr;
int a = *x;
}
static int pid = getpid();
TEST(MyDeathTest, DeathTestExample)
{
// Comment this line to get the same pid in every message
testing::FLAGS_gtest_death_test_style = "threadsafe";
cout << "pid outside before " << pid << endl;
ASSERT_EXIT(
cout << "pid inside " << pid << endl;
makeSegfault(),
testing::KilledBySignal(SIGSEGV), ".*");
cout << "pid outside after " << pid << endl;
}
The output without "threadsafe" (behaves as expected -- same pid everywhere):
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from MyDeathTest
[ RUN ] MyDeathTest.DeathTestExample
pid outside before 96698
pid inside 96698
pid outside after 96698
[ OK ] MyDeathTest.DeathTestExample (0 ms)
[----------] 1 test from MyDeathTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[ PASSED ] 1 test.
and with "threadsafe" (path hidden with [...]):
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from MyDeathTest
[ RUN ] MyDeathTest.DeathTestExample
pid outside before 96854
Running main() from [...]/googletest/src/gtest_main.cc
pid outside before 96855
pid inside 96855
pid outside after 96854
[ OK ] MyDeathTest.DeathTestExample (3 ms)
[----------] 1 test from MyDeathTest (3 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (3 ms total)
I understand that this might be expected due to how the "threadsafe" mode runs the whole test again, as stated in the documentation:
the child process re-executes the unit test binary just as it was originally invoked, but with some extra flags to cause just the single death test under consideration to be run
But in that case, how can I make sure that the inner process and the outer process can use the same value for any given static variable?

Test Cases states Failed but value returned is true

I have written up a simple sample code to understand GMOCK used for unit testing:
#include <iostream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::AtLeast;
class A {
public:
void ShowPub1()
{
std::cout << "A::PUBLIC::SHOW..1" << std::endl;
}
int ShowPub2(int x)
{
std::cout << "A::PUBLIC::SHOW..2" << std::endl;
return true;
}
};
class MockA : public A{
public:
MOCK_METHOD0(ShowPub1, void());
MOCK_METHOD1(ShowPub2, int(int x));
};
Below is my test code - I just wish to call ShowPub2 method of class A. I was expecting the statement A::PUBLIC::SHOW..2 to get printed at console - but it just did not happen and the test case also failed though the method is hardcoded to return true:
TEST(FirstA, TestCall) {
MockA a;
EXPECT_CALL(a, ShowPub2(2))
.Times(AtLeast(1));
a.ShowPub2(2);
EXPECT_TRUE(a.ShowPub2(2));
}
GMOCK test code execution output - I am not sure why the output A::PUBLIC::SHOW..2 did not rendered in console and test case failed:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from FirstA
[ RUN ] FirstA.TestCall
c:\users\user1\documents\c and c++\gmock\gmock1\gmock1\gmock1.cpp(78): error:
Value of: a.ShowPub2(2)
Actual: false
Expected: true
[ FAILED ] FirstA.TestCall (0 ms)
[----------] 1 test from FirstA (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] FirstA.TestCall
1 FAILED TEST
Press any key to continue . . .
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
return 0;
}
Some clarification is needed here..
Creating a Mock class means that the Mock methods are generate with its own implementations. These line of code
MOCK_METHOD0(ShowPub1, void());
MOCK_METHOD1(ShowPub2, int(int x));
doesn't mean it will call parent implementation of ShowPub1 / ShowOub2. This only means that you'll get a function (Mock) with same signature as that of the class you're mocking.
The test fails because of this line
EXPECT_TRUE(a.ShowPub2(2));
Since the original implementation is not called, this function fails.
The test function should be written as
TEST(FirstA, TestCall) {
MockA a;
EXPECT_CALL(a, ShowPub2(2)) .Times(AtLeast(1));
a.ShowPub2(2);
}
Here you're testing the behavior that the function is called at least once and the test will be successful with that.
If you want to test the functionality of a function, write separate tests. Mocking will not solve the problem in this case.

googletest SetUp Method not called

I'm using Google Test to unit test my C++ project. The getting started guide says:
If necessary, write a default constructor or SetUp() function to prepare the objects for each test. A common mistake is to spell SetUp() as Setup() with a small u - don't let that happen to you.
SetUp() is spelled correctly, but I still can't get SetUp to work. Any ideas?
#include "gtest/gtest.h"
class SampleTest : public ::testing::Test {
protected:
virtual void SetUp() { std::cout << "SetUp called." << std::endl; }
};
TEST(SampleTest, OneEqualsOne) {
int one = 1;
ASSERT_EQ(1, one);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
g++ -g -Wno-deprecated -I gtest/include SampleTest.cpp gtest/libgtest.a -o SampleTest
Output:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN ] SampleTest.OneEqualsOne
[ OK ] SampleTest.OneEqualsOne (1 ms)
[----------] 1 test from SampleTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
Change TEST to TEST_F, as SetUp methods and such are called with TEST_F, but not with TEST alone.
Change your TEST macro to TEST_F. (It's listing in the documentation right underneath the quote you provided.)

c++ Google Tests run twice

I'm starting to use Google Test to run unit tests on my code. I use Eclipse Kepler over Ubuntu 12.04.
I'm using the following classes on this first test:
AllTests.cpp
#include "gtest/gtest.h"
#include "SerialManagerTest.cpp"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
SerialManagerTest.cpp
#include "gtest/gtest.h"
#include "SerialManager.h"
#include "SerialInterface.h"
#include "FakeSerialHandler.h"
namespace {
TEST(TestingSerialManager, FirstTest) {
SerialInterface *fakeSerialHandler=new FakeSerialHandler();
SerialManager* serialManager=new SerialManager(fakeSerialHandler);
ASSERT_TRUE(serialManager->OpenPort());
delete serialManager;
}
TEST(TestingSerialManager, SecondTest) {
SerialInterface *fakeSerialHandler=new FakeSerialHandler();
SerialManager* serialManager=new SerialManager(fakeSerialHandler);
ASSERT_FALSE(!serialManager->OpenPort());
delete serialManager;
}
}
When I run the tests I get this output
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from TestingSerialManager
[ RUN ] TestingSerialManager.FirstTest
[ OK ] TestingSerialManager.FirstTest (0 ms)
[ RUN ] TestingSerialManager.SecondTest
[ OK ] TestingSerialManager.SecondTest (0 ms)
[ RUN ] TestingSerialManager.FirstTest
[ OK ] TestingSerialManager.FirstTest (0 ms)
[ RUN ] TestingSerialManager.SecondTest
[ OK ] TestingSerialManager.SecondTest (0 ms)
[----------] 4 tests from TestingSerialManager (2 ms total)
[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (3 ms total)
[ PASSED ] 4 tests.
Why each test is being processed twice?
Why do you include a translation unit in a translation unit?
#include "SerialManagerTest.cpp"
It has its place in some cases, but usually is a bad practice.
What very likely happens (without seeing your command line), is that your SerialManagerTest code is linked twice because of the include in the final executable. That is, it is duplicated in AllTests.o and SerialManagerTest.o, and both objects are linked into the final test executable.