Convert Figma logo to code with AI

google logogoogletest

GoogleTest - Google Testing and Mocking Framework

34,204
10,046
34,204
341

Top Related Projects

18,461

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)

5,809

The fastest feature-rich C++11/14/17/20/23 single-header testing framework

1,246

C++20 μ(micro)/Unit Testing Framework

20,408

A modern formatting library

Quick Overview

GoogleTest is a comprehensive C++ testing framework developed by Google. It provides a rich set of tools for writing and running unit tests, including assertions, test fixtures, and mock objects. GoogleTest is designed to be easy to use, efficient, and portable across various platforms.

Pros

  • Easy to set up and use, with a simple syntax for writing tests
  • Supports a wide range of assertions and matchers for flexible test creation
  • Provides powerful features like test fixtures, parameterized tests, and death tests
  • Actively maintained and widely adopted in the C++ community

Cons

  • Can be overwhelming for beginners due to its extensive feature set
  • Requires some initial setup and configuration, which may be time-consuming
  • May have a steeper learning curve compared to simpler testing frameworks
  • Some users report issues with integrating GoogleTest into certain build systems

Code Examples

  1. Basic test example:
#include <gtest/gtest.h>

TEST(MyTest, Addition) {
  EXPECT_EQ(2 + 2, 4);
}

This example demonstrates a simple test case using GoogleTest's TEST macro and EXPECT_EQ assertion.

  1. Test fixture example:
class MyFixture : public ::testing::Test {
protected:
  void SetUp() override {
    // Set up test environment
  }
  void TearDown() override {
    // Clean up after test
  }
};

TEST_F(MyFixture, TestUsingFixture) {
  // Test code using the fixture
}

This example shows how to create a test fixture for setting up and tearing down test environments.

  1. Parameterized test example:
class ParamTest : public ::testing::TestWithParam<int> {};

TEST_P(ParamTest, IsEven) {
  EXPECT_EQ(GetParam() % 2, 0);
}

INSTANTIATE_TEST_SUITE_P(EvenNumbers, ParamTest, ::testing::Values(2, 4, 6, 8));

This example demonstrates how to create parameterized tests using GoogleTest.

Getting Started

To get started with GoogleTest:

  1. Clone the repository:

    git clone https://github.com/google/googletest.git
    
  2. Add GoogleTest to your project's CMakeLists.txt:

    add_subdirectory(googletest)
    target_link_libraries(your_project gtest gtest_main)
    
  3. Include GoogleTest in your test file:

    #include <gtest/gtest.h>
    
  4. Write your tests and run them using the GoogleTest test runner.

Competitor Comparisons

18,461

A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)

Pros of Catch2

  • Header-only library, easier to integrate into projects
  • More expressive and readable test syntax
  • Supports a wider range of C++ versions (C++11 and later)

Cons of Catch2

  • Slower compile times due to header-only nature
  • Less extensive documentation compared to GoogleTest
  • Fewer advanced features for complex testing scenarios

Code Comparison

Catch2:

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Addition works", "[math]") {
    REQUIRE(2 + 2 == 4);
    CHECK(1 + 1 == 2);
}

GoogleTest:

#include <gtest/gtest.h>

TEST(MathTest, AdditionWorks) {
    ASSERT_EQ(2 + 2, 4);
    EXPECT_EQ(1 + 1, 2);
}

Both Catch2 and GoogleTest are popular C++ testing frameworks. Catch2 offers a more modern and user-friendly syntax, while GoogleTest provides a more comprehensive set of features and better performance for large-scale projects. The choice between the two often depends on project requirements, team preferences, and the specific testing needs of the application.

5,809

The fastest feature-rich C++11/14/17/20/23 single-header testing framework

Pros of doctest

  • Faster compilation times due to header-only implementation
  • Simpler setup and integration into projects
  • Smaller codebase, making it easier to understand and maintain

Cons of doctest

  • Less extensive feature set compared to GoogleTest
  • Smaller community and ecosystem
  • Fewer advanced testing capabilities (e.g., mock objects)

Code Comparison

GoogleTest:

#include <gtest/gtest.h>

TEST(MyTest, Addition) {
  EXPECT_EQ(2 + 2, 4);
}

doctest:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

TEST_CASE("Addition") {
  CHECK(2 + 2 == 4);
}

Both frameworks offer similar syntax for basic test cases, but GoogleTest requires separate compilation and linking, while doctest is header-only. doctest's implementation is more compact, potentially leading to faster compilation times. However, GoogleTest provides a wider range of assertion macros and testing utilities, making it more suitable for complex testing scenarios.

1,246

C++20 μ(micro)/Unit Testing Framework

Pros of ut

  • Lightweight and header-only, resulting in faster compilation times
  • Modern C++ design with extensive use of C++20 features
  • Simpler syntax and less boilerplate code for writing tests

Cons of ut

  • Less mature and less widely adopted compared to googletest
  • Fewer features and extensions available
  • Limited documentation and community support

Code Comparison

googletest:

TEST(MyTest, Addition) {
  EXPECT_EQ(2 + 2, 4);
  ASSERT_TRUE(true);
}

ut:

"Addition"_test = [] {
  expect(2 + 2_i == 4_i);
  expect(true);
};

Summary

googletest is a well-established, feature-rich testing framework with extensive documentation and community support. It offers a wide range of assertions, fixtures, and extensions, making it suitable for large-scale projects.

ut is a modern, lightweight alternative that leverages C++20 features to provide a simpler syntax and faster compilation times. It's ideal for smaller projects or those prioritizing simplicity and performance.

The choice between the two depends on project requirements, team familiarity, and the desired balance between features and simplicity. googletest offers more robustness and ecosystem support, while ut provides a more streamlined and modern approach to testing in C++.

20,408

A modern formatting library

Pros of fmt

  • Focused on string formatting and output, providing a more modern and efficient alternative to printf and iostreams
  • Smaller scope and codebase, making it easier to integrate and maintain in projects
  • Supports compile-time format string checks for improved safety

Cons of fmt

  • Limited to formatting and output functionality, unlike googletest's comprehensive testing framework
  • May require additional libraries for more complex string manipulation tasks
  • Less widespread adoption compared to googletest in the C++ ecosystem

Code Comparison

fmt:

#include <fmt/core.h>
std::string s = fmt::format("The answer is {}.", 42);
fmt::print("Hello, {}!", "world");

googletest:

#include <gtest/gtest.h>
TEST(MyTest, AssertionExample) {
  EXPECT_EQ(42, SomeFunction());
  ASSERT_TRUE(SomeCondition());
}

Summary

fmt is a specialized library for string formatting and output, offering modern alternatives to traditional C++ methods. googletest, on the other hand, is a comprehensive testing framework for C++ projects. While fmt excels in its specific domain, googletest provides a broader set of tools for unit testing and test-driven development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

GoogleTest

Announcements

Live at Head

GoogleTest now follows the Abseil Live at Head philosophy. We recommend updating to the latest commit in the main branch as often as possible. We do publish occasional semantic versions, tagged with v${major}.${minor}.${patch} (e.g. v1.15.2).

Documentation Updates

Our documentation is now live on GitHub Pages at https://google.github.io/googletest/. We recommend browsing the documentation on GitHub Pages rather than directly in the repository.

Release 1.15.2

Release 1.15.2 is now available.

The 1.15.x branch requires at least C++14.

Continuous Integration

We use Google's internal systems for continuous integration.

Coming Soon

  • We are planning to take a dependency on Abseil.

Welcome to GoogleTest, Google's C++ test framework!

This repository is a merger of the formerly separate GoogleTest and GoogleMock projects. These were so closely related that it makes sense to maintain and release them together.

Getting Started

See the GoogleTest User's Guide for documentation. We recommend starting with the GoogleTest Primer.

More information about building GoogleTest can be found at googletest/README.md.

Features

  • xUnit test framework:
    Googletest is based on the xUnit testing framework, a popular architecture for unit testing
  • Test discovery:
    Googletest automatically discovers and runs your tests, eliminating the need to manually register your tests
  • Rich set of assertions:
    Googletest provides a variety of assertions, such as equality, inequality, exceptions, and more, making it easy to test your code
  • User-defined assertions:
    You can define your own assertions with Googletest, making it simple to write tests that are specific to your code
  • Death tests:
    Googletest supports death tests, which verify that your code exits in a certain way, making it useful for testing error-handling code
  • Fatal and non-fatal failures:
    You can specify whether a test failure should be treated as fatal or non-fatal with Googletest, allowing tests to continue running even if a failure occurs
  • Value-parameterized tests:
    Googletest supports value-parameterized tests, which run multiple times with different input values, making it useful for testing functions that take different inputs
  • Type-parameterized tests:
    Googletest also supports type-parameterized tests, which run with different data types, making it useful for testing functions that work with different data types
  • Various options for running tests:
    Googletest provides many options for running tests including running individual tests, running tests in a specific order and running tests in parallel

Supported Platforms

GoogleTest follows Google's Foundational C++ Support Policy. See this table for a list of currently supported versions of compilers, platforms, and build tools.

Who Is Using GoogleTest?

In addition to many internal projects at Google, GoogleTest is also used by the following notable projects:

Related Open Source Projects

GTest Runner is a Qt5 based automated test-runner and Graphical User Interface with powerful features for Windows and Linux platforms.

GoogleTest UI is a test runner that runs your test binary, allows you to track its progress via a progress bar, and displays a list of test failures. Clicking on one shows failure text. GoogleTest UI is written in C#.

GTest TAP Listener is an event listener for GoogleTest that implements the TAP protocol for test result output. If your test runner understands TAP, you may find it useful.

gtest-parallel is a test runner that runs tests from your binary in parallel to provide significant speed-up.

GoogleTest Adapter is a VS Code extension allowing to view GoogleTest in a tree view and run/debug your tests.

C++ TestMate is a VS Code extension allowing to view GoogleTest in a tree view and run/debug your tests.

Cornichon is a small Gherkin DSL parser that generates stub code for GoogleTest.

Contributing Changes

Please read CONTRIBUTING.md for details on how to contribute to this project.

Happy testing!