Still using gtest? Introduction to a better testing framework

gtest needs to be installed and sometimes brings a lot of inconvenience. For network reasons, downloading and installing gtest or pulling gtest from git may fail due to network reasons. In addition to gtest, there are many lightweight and easy-to-use unit testing libraries, such as doctest.

Modern C++ unit-test library


In addition to gtest, there are many lightweight and easy-to-use unit testing libraries, such as doctest and catch. Compared with gtest, which needs to be compiled/installed, they are all header only, and they can be used for unit testing when they are directly included in the project. Portable does not have any dependencies, and the requirements for the compiler version are not high, only C++11 is required. Yes, there is only one word after using it: cool!

Doctest is recommended here because its performance is better than catch , and of course it is better than gtest. Let's see how doctest is used.

Introduction to doctest


The github address of doctest: doctest/doctest: The fastest feature-rich C++11/14/17/20 single-header testing framework

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

doctest is a new C++ testing framework. Compile-time (by orders of magnitude ) and runtime are the fastest compared to other feature-rich alternatives . It provides the ability to compile languages ​​(such as  D / Rust / Nim ) and write tests directly in production code by providing a fast, transparent and flexible test runner with a clean interface.

The main difference between doctest and other testing frameworks is that it is lightweight and non-intrusive.

A complete example with a self-registering test that compiles to an executable looks like this:

Related properties


  1. Everything related to testing can be removed from the binary executable by defining the DOCTEST_CONFIG_DISABLE flag.
  2. Very small and easy to integrate - just a header file.
  3. Very low compile time - about 25ms compile time overhead for expanding header files.
  4. Probably the fastest assertion macro - 50K assertions can be compiled in 30 seconds (or even 10 seconds).
  5. No header files are moved (except translation units implemented by the library).
  6. Everything is in the doctest namespace (implementation details are in a nested detail namespace).
  7. All macros are prefixed - some are unprefixed by default, but this is optional - see configuration.
  8. No warnings are produced, even with the strictest compilation options (on all compilers tested).
  9. Clang : -Weverything -pedantic
  10. GCC: -Wall -Wextra -pedantic and over 35 other warnings that these flags do not override GCC! ——Reference here.
  11. MSVC: /W4 (/Wall is too much - even Microsoft's own headers will generate thousands of warnings if this option is used)
  12. Options not recognized by the command line will not throw an error, and prefixes are supported for interoperability with client command line parsing.
  13. Options can be set in the program instead of being passed in from the command line via argc/argv.
  14. No warning is generated when self is disabled.

doctest basic usage

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
 
TEST_CASE("vectors can be sized and resized") {
    std::vector<int> v(5);
 
    REQUIRE(v.size() == 5);
    REQUIRE(v.capacity() >= 5);
 
    SUBCASE("adding to the vector increases it's size") {
        v.push_back(1);
 
        CHECK(v.size() == 6);
        CHECK(v.capacity() >= 6);
    }
    SUBCASE("reserving increases just the capacity") {
        v.reserve(6);
 
        CHECK(v.size() == 5);
        CHECK(v.capacity() >= 6);
    }
}

Using CHECK here to make assertions is similar to gtest’s EXPECT_xx. I think one of the cooler and more practical features of doctest is SUBCASE, which allows adding more sub-cases to test some special things in the current case. It is very practical, which is also A better place than gtest.

doctest provides a lot of rich macros, which can fully meet our testing needs.

Assertion macros for doctests


The CHECK macro only checks and will not terminate the test, and the REQUIRE macro will terminate the test. Similar to the ASSET_XX macro in gtest, these two macros are also the two most commonly used macros when doing single tests.

Modern C++ benchmark library


nanobench is also a header only library written in C++11, it is also very simple to use, just include the header file.

#define ANKERL_NANOBENCH_IMPLEMENT
#include <nanobench.h>
 
int main() {
    double d = 1.0;
    ankerl::nanobench::Bench().run("some double ops", [&] {
        d += 1.0 / d;
        if (d > 5.0) {
            d -= 5.0;
        }
        ankerl::nanobench::doNotOptimizeAway(d);
    });
}

Use the modern C++ test tool chain: doctest+FakeIt+nanobench, which can perfectly replace gtest/gmock and google bench, without any dependencies, without installation, and can be used directly including header files, very easy to integrate and use, it is time to abandon google test and google bench!

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

 

Guess you like

Origin blog.csdn.net/OKCRoss/article/details/131437225