element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Embedded Forum Try out GoogleTest: set up objects before testing with a fixture
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 1 reply
  • Subscribers 460 subscribers
  • Views 634 views
  • Users 0 members are here
  • gcc
  • unit test
  • vscode
  • googletest
Related

Try out GoogleTest: set up objects before testing with a fixture

Jan Cumps
Jan Cumps 6 months ago

This week I'm going to learn GoogleTest for c++. Goals for day 2: Test Fixtures.

To run unit tests on an object oriented design, you often require that some objects are created and set up before testing. Multiple tests will very likely need the same setup. GTest provides the fixture mechanism for that. A fixture is [...] a class that sets up your objects, and gives access to those objects to the GTest framework. For each test case, the fixture (and your objects) will be freshly set up before the test, and destroyed when done.

Create objects before running tests

In this post I'll use the same class TestClass that I tested in the previous exercise.

namespace test_module {

class TestClass {
public:
  bool match(const std::string_view& sv);
};

}

Here is how my tests looked like before using a fixture:

TEST(string_view, match) {
    test_module::TestClass o;
    EXPECT_TRUE(o.match("match")); // should pass
}

TEST(string_view, missmatch) {
    test_module::TestClass o;
    EXPECT_FALSE(o.match("mismatch")); // should pass
}

I created the object o at the start of the test myself. And repeated that at the start of each test that tests the object.
With the GTest fixture approach, I learn a class how to create that setup for me, each time I want to run a test:

class TestClassTest : public testing::Test {
protected:
    TestClassTest() {}
   
    // ~TestClassTest() override = default;
    test_module::TestClass o;
};

The only thing that this class does, is create a member o of the class that I want to test. Then, instead of using the GTest macro TEST, I use the macro TEST_F. First parameter is the name of the fixture class.

TEST_F(TestClassTest, match) {
    EXPECT_TRUE(o.match("match")); // should pass
}

This will automatically set up the fixture before the test is executed. And give us access to the freshly created object o.

In my example, that I kept simple on purpose, there is only one object to set up. But say that you created a more complex class, like a specialised array object. You can use this mechanism to create your container (and add some data to it - a subject for the section below) before the tests execute. You can then run multiple test cases that automatically have this known setup.

Results of my test run:

image

Configure those objects before test execution

In the section above, my object under test didn't have a requirement to configure it. When it was created, it was ready to use. It was able to compare a string to the fixed string "match". In this second part of the post, I'm modifying it into a more flexible class. It can compare a string to a configurable string. This new class will allow us to test how the GTest fixture setup functionality works.

Rewritten class, now allows you to define a string to compare to:

// ...

class TestClass {
public:
  TestClass() : match_string() {}
  bool match(const std::string_view& sv);
  void set_match_string(const std::string& s) {
    match_string = s;
  }

private:
  std::string match_string;
};

// ...

The difference with the class in the first part of this post:

  • we have a class member match_string
  • there is a method set_match_string() to set it
  • at object creation, this defaults to an empty string ("").

If we want to use it in a test case without fixture, our tests will look like this:

TEST(string_view, match) {
    test_module::TestClass o;
    o.set_match_string("match");
    EXPECT_TRUE(o.match("match")); // should pass
}

TEST(string_view, missmatch) {
    test_module::TestClass o;
    o.set_match_string("match");
    EXPECT_FALSE(o.match("mismatch")); // should pass
}

If we use the same fixture as in the first part of this post, we 'll get the object created, but not the string set. For that, we'll adapt our fixture class, and give it a SetUp() method:

class TestClassTest : public testing::Test {
protected:
    TestClassTest() {}
    void SetUp() override {
        o.set_match_string("match");
    }
   
    // ~TestClassTest() override = default;
    test_module::TestClass o;
};

GTest will now do 2 things for us:

  • create the object, as before
  • call its set_match_string() and set the string to "match".

The fixture test case does not change:

TEST_F(TestClassTest, match) {
    EXPECT_TRUE(o.match("match")); // should pass
}

That's it. In part 1 of the post we learn how to automatically create (a set of) objects before a test gets executed.
In part 2, we configure those objects, after they are created.

This is the main story about test fixtures. They allow you to set up an object structure before a test case is run, and configure those objects. Then it executes your tests on them.

link to all posts.

  • Sign in to reply
  • Cancel
Parents
  • Jan Cumps
    Jan Cumps 6 months ago

    Finally, GoogleTest tests a real class - a GPS NMEA message parser:  Try out GoogleTest: test existing embedded OO library 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Jan Cumps
    Jan Cumps 6 months ago

    Finally, GoogleTest tests a real class - a GPS NMEA message parser:  Try out GoogleTest: test existing embedded OO library 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube