Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize constant string for multiple tests in google test?

Tags:

tdd

googletest

I'm using google test and I have a cpp-file containing several tests. I would like to initialize a string with the current date and time when starting the first test. I would like to use this string in all other tests, too. How can I do this.

I've tried the following (m_string being a protected member of CnFirstTest), but it didn't work (since the constructor and SetUp will be called before each test):

CnFirstTest::CnFirstTest(void) {
    m_string = currentDateTime();
}

void CnFirstTest::SetUp() {
}



TEST_F(CnFirstTest, Test1) {
    // use m_string
}

TEST_F(CnFirstTest, Test2) {
    // use m_string, too
}
like image 827
Semjon Mössinger Avatar asked Jun 02 '15 10:06

Semjon Mössinger


People also ask

What is Test_p in Gtest?

TEST_P() is useful when you want to write tests with a parameter. Instead of writing multiple tests with different values of the parameter, you can write one test using TEST_P() which uses GetParam() and can be instantiated using INSTANTIATE_TEST_SUITE_P() .

Does Google Test run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

How do you test a Gtest function?

To create a test: Use the TEST() macro to define and name a test function. These are ordinary C++ functions that don't return a value. In this function, along with any valid C++ statements you want to include, use the various googletest assertions to check values.


1 Answers

You can use a gtest testing::Environment to achieve this:

#include <chrono>
#include <iostream>

#include "gtest/gtest.h"

std::string currentDateTime() {
  return std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
}

class TestEnvironment : public ::testing::Environment {
 public:
  // Assume there's only going to be a single instance of this class, so we can just
  // hold the timestamp as a const static local variable and expose it through a
  // static member function
  static std::string getStartTime() {
    static const std::string timestamp = currentDateTime();
    return timestamp;
  }

  // Initialise the timestamp.
  virtual void SetUp() { getStartTime(); }
};

class CnFirstTest : public ::testing::Test {
 protected:
  virtual void SetUp() { m_string = currentDateTime(); }
  std::string m_string;
};

TEST_F(CnFirstTest, Test1) {
  std::cout << TestEnvironment::getStartTime() << std::endl;
  std::cout << m_string << std::endl;
}

TEST_F(CnFirstTest, Test2) {
  std::cout << TestEnvironment::getStartTime() << std::endl;
  std::cout << m_string << std::endl;
}

int main(int argc, char* argv[]) {
  ::testing::InitGoogleTest(&argc, argv);
  // gtest takes ownership of the TestEnvironment ptr - we don't delete it.
  ::testing::AddGlobalTestEnvironment(new TestEnvironment);
  return RUN_ALL_TESTS();
}
like image 110
Fraser Avatar answered Oct 14 '22 20:10

Fraser