Catch2 SECTIONs equivalent in GoogleTest

414 views Asked by At

The Catch2 framework has the concept of SECTIONs, which seem to make unit test code more readable. To get a feeling for how much easier writing certain tests would be in Catch2 vs GoogleTest, I am trying to come up with the best way to translate the given Catch2 sample code

#include <catch2/catch_test_macros.hpp>

TEST_CASE( "vectors can be sized and resized", "[vector]" ) {

    std::vector<int> v{1, 2, 3, 4, 5};

    REQUIRE(v.size() == 5);
    REQUIRE(v.capacity() == 5);

    SECTION( "A) reserving bigger changes capacity but not size" )
    {
        v.reserve(10);
    
        REQUIRE(v.size() == 5);
        REQUIRE(v.capacity() >= 10);

        SECTION( "A.1) reserving down unused capacity does not change capacity" )
        {
            v.reserve(7);
            REQUIRE(v.size() == 5);
            REQUIRE(v.capacity() >= 10);
        }

        v[0] = 42;

        SECTION( "A.2) shrink_to_fit is a non binding request to reduce capacity() to size()")
        {
            v.shrink_to_fit();
            REQUIRE(v.capacity() >= v.size());
        }
    }
}

to GoogleTest. I currently have

#include <gtest/gtest.h>

#include <vector>

class VectorTest : public ::testing::Test
{
protected:
    void SetUp() override
    {
        EXPECT_EQ(v.size(), 5);
        EXPECT_EQ(v.capacity(), 5);
    }

    std::vector<int> v{1, 2, 3, 4, 5};
};

TEST_F(VectorTest, SectionA1)
{
    // reserving bigger changes capacity but not size
    v.reserve(10);
    EXPECT_EQ(v.size(), 5);
    EXPECT_GE(v.capacity(), 10);

    // reserving down unused capacity does not change capacity
    v.reserve(7);
    EXPECT_EQ(v.size(), 5);
    EXPECT_GE(v.capacity(), 10);

    v[0] = 42;
}

TEST_F(VectorTest, SectionA2)
{
    // reserving bigger changes capacity but not size
    v.reserve(10);
    EXPECT_EQ(v.size(), 5);
    EXPECT_GE(v.capacity(), 10);

    v[0] = 42;

    // shrink_to_fit is a non binding request to reduce capacity() to size()
    v.shrink_to_fit();
    EXPECT_GE(v.capacity(), v.size());
}

which seems to be a bit more verbose and less readable than the Catch2 code.

I'm wondering if there are other more elegant/readable/shorter ways to do it?

If 'no', then this would be an argument for using Catch2.

1

There are 1 answers

5
273K On

The same behavior can be achieved with test fixtures.

class VectorTest : public ::testing::Test {
 protected:
  void SetUp() override {
    EXPECT_EQ(v_.size(), 5);
    EXPECT_EQ(v_.capacity(), 5);

    SCOPED_TRACE("A) reserving bigger changes capacity but not size");
    v_.reserve(10);
    
    EXPECT_EQ(v_.size(), 5);
    EXPECT_GE(v_.capacity(), 10);
  }

  std::vector<int> v_{1, 2, 3, 4, 5};
}; 

TEST_F(VectorTest, ResizeDown) {
  SCOPED_TRACE("A.1) reserving down unused capacity does not change capacity");
  v_.reserve(7);
  EXPECT_EQ(v_.size(), 5);
  EXPECT_GE(v_.capacity(), 10);
  v_[0] = 42;
}

TEST_F(VectorTest, Shrink) {
  v_[0] = 42;
  SCOPED_TRACE("A.2) shrink_to_fit is a non binding request to reduce capacity() to size()");
  v_.shrink_to_fit();
  EXPECT_EQ(v_.capacity(), v_.size());
}