37

I want to use google test to write a class that derives from ::testing::Test and adds functionalities to it using mostly the constructor, or SetUp and TearDown(). Looks like SetUp/TearDown is the way to go so far. My question is:

Let's say we have a small test fixture like this:

TEST_F (PerformanceTest, Inputs)
{
   EXPECT_EQ (0.0, performSaxpy(10, 4.0F, 3.0F, 2.0F));
   EXPECT_EQ (0.0, performSaxpy(1, 5.0F, 4.0F, 3.0F));
   EXPECT_EQ (0.0, performSaxpy(10, 12.0F, 2.0F, 1.0F));
}

What I want to do is, when the SetUp() function is called, I want code inside that to query the name of the test fixture class (PerformanceTest), and the name of the test object (Inputs).

Why do I need to do this? Because I want to write the results of my test fixture to an xml file. For various reasons, I cannot use the default XML output format that google test already provides. However, in google test's default XML output, we get the names of the test fixture class and test object, so hopefully we can do it by custom... Is this possible?

nirvanaswap
  • 859
  • 2
  • 11
  • 21
  • 1
    Are you looking for `::testing::UnitTest::GetInstance()->current_test_info()->name()` – Pawel Jun 30 '16 at 18:49
  • Great, is there an API reference for all the functions offered by ::testing::UnitTest? – nirvanaswap Jun 30 '16 at 18:50
  • I guess: https://github.com/google/googletest/blob/d225acc90bc3a8c420a9bcd1f033033c1ccd7fe0/googletest/include/gtest/gtest.h#L644 :trollface: I think if you look for TestInfo google test you can find more details – Pawel Jun 30 '16 at 18:52
  • 1
    I think you could start here: https://github.com/google/googletest/blob/d225acc90bc3a8c420a9bcd1f033033c1ccd7fe0/googletest/docs/AdvancedGuide.md (take a look at Getting the current test's name section) – Pawel Jun 30 '16 at 18:53
  • Great, that's exactly what I needed! – nirvanaswap Jun 30 '16 at 18:55

1 Answers1

59

You can get a test name using TestInfo class:

::testing::UnitTest::GetInstance()->current_test_info()->name()

You can find more details here

Pawel
  • 31,342
  • 4
  • 73
  • 104