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: run tests with sets of data - TestWithParam
  • 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 3 replies
  • Subscribers 459 subscribers
  • Views 613 views
  • Users 0 members are here
  • gcc
  • unit test
  • vscode
  • googletest
Related

Try out GoogleTest: run tests with sets of data - TestWithParam

Jan Cumps
Jan Cumps 6 months ago

This week I'm going to learn GoogleTest for c++. Goals for day 4: test code with several data sets. In this case, I'm asking a GPS parser object (object under test) to parse sets of valid and invalid GPS payloads. The object should be able to report if the payload is valid or not (test condition).

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

The GTest WithParamInterface test fixture class allows that. I can write a single test case, and run that multiple times.

// generic template for NMEA parser unit tests
// base class for all test fixtures, parameterized or not.
template <class T>
class nmeaTest : public testing::Test {
protected:
    nmeaTest() {}
    T o;
};

// ==================================================================================
// template for parameterised parser tests
// generic fixture that allows to test any nmea parser class' parser function
// with multiple data inputs
template <class T>
class nmeaParsersTest : public nmeaTest<T>, public testing::WithParamInterface<std::tuple<std::string, bool>> {
protected:
    nmeaParsersTest() {}
};

The class accepts two parameters each time it's executed:

  • a string. In my tests, that will be GPS payload data. Both valid and invalid data.
  • a bool: what is the expected result of this test. I'm expecting a true result only for valid payloads. My class under test should report false if the payload is not compliant

note: this test fixture also uses a template. You can ignore that for now. I'll review that in a next post.

Next, we declare the tests that we will do on the object under test:

TEST_P(gllParserTest, gllparsetest) {
    bool expected = std::get<1>(GetParam());
    std::string s = std::get<0>(GetParam());
    ASSERT_EQ(expected, o.from_data(s, o));
}

We retrieve the expected outcome and the test payload from the parameters. Then we ask the object under test to execute a parse test with that string, and compare its return value with the expected outcome.

Here is how to call this test with multiple data sets and expectations:

INSTANTIATE_TEST_SUITE_P(
    parsetest,
    gllParserTest,
    ::testing::Values(
        std::make_tuple("$XXXXXXXX", false), // invalid payload
        std::make_tuple("", false), // empty payload
        std::make_tuple("$GPGLL,5051.83778,N,00422.55809,S,185427.150,A,N*4F", true),
        std::make_tuple("$GPGLL,5051.83778,N,00422.55809,S,185427.150,V,N*4F", true))
);

Test 1 and 2 should check for a false return value, because the payload isn't compliant with the GPS payload definition. Test 3 and 4 have valid payload, and the object under test should return true.

Outcome:

image

link to all posts.

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

    When you use the TestMate extension in VSCode, there is an option "run continuous tests"

    When you enable that, the plug-in will execute the unit tests, each time a "binary covered by tests" is modified. Usually when you build.

    Below, you see that the callbackmanager_test has run. This happened after I built the project. The blue "eye" icon visible in the left area, switches this auto-execute on or off.

    image

    If a test fails, you 'd get a badge with the count of failed tests in the left toolbar. So you don't have to keep this view active. It 'll draw attention when something failed:

    image

    Also note the handy pointer that highlights the line(s) of code where validation(s) failed.

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

    When you use the TestMate extension in VSCode, there is an option "run continuous tests"

    When you enable that, the plug-in will execute the unit tests, each time a "binary covered by tests" is modified. Usually when you build.

    Below, you see that the callbackmanager_test has run. This happened after I built the project. The blue "eye" icon visible in the left area, switches this auto-execute on or off.

    image

    If a test fails, you 'd get a badge with the count of failed tests in the left toolbar. So you don't have to keep this view active. It 'll draw attention when something failed:

    image

    Also note the handy pointer that highlights the line(s) of code where validation(s) failed.

    • 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