文章目录

  • ​​1.简介​​
  • ​​2.用法​​

1.简介

示例#2显示了一个具有多个成员的类的更复杂的单元测试功能

2.用法

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(hello_gtest VERSION 0.1.0)

add_executable(${PROJECT_NAME} main.cc sample.cc sample_unitest.cc)
target_link_libraries(${PROJECT_NAME} gtest pthread)

main.cc

#include <gtest/gtest.h>

int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

sample.h

#ifndef GOOGLETEST_SAMPLES_SAMPLE2_H_
#define GOOGLETEST_SAMPLES_SAMPLE2_H_

#include <string.h>

// A simple string class.
class MyString
{
private:
const char *c_string_;
const MyString &operator=(const MyString &rhs);

public:
// Clones a 0-terminated C string, allocating memory using new.
static const char *CloneCString(const char *a_c_string);


//
// C'tors

// The default c'tor constructs a NULL string.
MyString() : c_string_(nullptr) {}

// Constructs a MyString by cloning a 0-terminated C string.
explicit MyString(const char *a_c_string) : c_string_(nullptr)
{
Set(a_c_string);
}

// Copy c'tor
MyString(const MyString &string) : c_string_(nullptr)
{
Set(string.c_string_);
}


//
// D'tor. MyString is intended to be a final class, so the d'tor
// doesn't need to be virtual.
~MyString() { delete[] c_string_; }

// Gets the 0-terminated C string this MyString object represents.
const char *c_string() const { return c_string_; }

size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }

// Sets the 0-terminated C string this MyString object represents.
void Set(const char *c_string);
};

#endif // GOOGLETEST_SAMPLES_SAMPLE2_H_

sample.cc

#include "sample.h"

#include <string.h>

// Clones a 0-terminated C string, allocating memory using new.
const char *MyString::CloneCString(const char *a_c_string)
{
if (a_c_string == nullptr)
return nullptr;

const size_t len = strlen(a_c_string);
char *const clone = new char[len + 1];
memcpy(clone, a_c_string, len + 1);

return clone;
}

// Sets the 0-terminated C string this MyString object
// represents.
void MyString::Set(const char *a_c_string)
{
// Makes sure this works when c_string == c_string_
const char *const temp = MyString::CloneCString(a_c_string);
delete[] c_string_;
c_string_ = temp;
}

sample_unitest.cc

// A sample program demonstrating using Google C++ testing framework.

// This sample shows how to write a more complex unit test for a class
// that has multiple member functions.
//
// Usually, it's a good idea to have one test for each method in your
// class. You don't have to do that exactly, but it helps to keep
// your tests organized. You may also throw in additional tests as
// needed.

#include "sample.h"

#include "gtest/gtest.h"
namespace
{
// In this example, we test the MyString class (a simple string).

// Tests the default c'tor.
TEST(MyString, DefaultConstructor)
{
const MyString s;

// Asserts that s.c_string() returns NULL.
//
// <TechnicalDetails>
//
// If we write NULL instead of
//
// static_cast<const char *>(NULL)
//
// in this assertion, it will generate a warning on gcc 3.4. The
// reason is that EXPECT_EQ needs to know the types of its
// arguments in order to print them when it fails. Since NULL is
// #defined as 0, the compiler will use the formatter function for
// int to print it. However, gcc thinks that NULL should be used as
// a pointer, not an int, and therefore complains.
//
// The root of the problem is C++'s lack of distinction between the
// integer number 0 and the null pointer constant. Unfortunately,
// we have to live with this fact.
//
// </TechnicalDetails>
EXPECT_STREQ(nullptr, s.c_string());

EXPECT_EQ(0u, s.Length());
}

const char kHelloString[] = "Hello, world!";

// Tests the c'tor that accepts a C string.
TEST(MyString, ConstructorFromCString)
{
const MyString s(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
EXPECT_EQ(sizeof(kHelloString) / sizeof(kHelloString[0]) - 1, s.Length());
}

// Tests the copy c'tor.
TEST(MyString, CopyConstructor)
{
const MyString s1(kHelloString);
const MyString s2 = s1;
EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
}

// Tests the Set method.
TEST(MyString, Set)
{
MyString s;

s.Set(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));

// Set should work when the input pointer is the same as the one
// already in the MyString object.
s.Set(s.c_string());
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));

// Can we set the MyString to NULL?
s.Set(nullptr);
EXPECT_STREQ(nullptr, s.c_string());
}
} // namespace
  • ref:gtest sample