Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:21:56+00:00 2026-06-15T03:21:56+00:00

Why does a test fixture have a SetUp method in Google Test? Isn’t the

  • 0

Why does a test fixture have a SetUp method in Google Test? Isn’t the Constructor effectively the same thing? Likewise for the TearDown method. Calls to both SetUp and the Constructor, as well as TearDown and the Destructor, are consistent with the TestEventListeners: OnTestStart and OnTestEnd.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:21:57+00:00Added an answer on June 15, 2026 at 3:21 am

    There is an answer to that in the FAQ:

    Should I use the constructor/destructor of the test fixture or the set-up/tear-down function?

    The first thing to remember is that googletest does not reuse the same test
    fixture object across multiple tests. For each TEST_F, googletest will create
    a fresh test fixture object, immediately call SetUp(), run the test body,
    call TearDown(), and then delete the test fixture object.

    When you need to write per-test set-up and tear-down logic, you have the choice
    between using the test fixture constructor/destructor or SetUp()/TearDown().
    The former is usually preferred, as it has the following benefits:

    • By initializing a member variable in the constructor, we have the option to
      make it const, which helps prevent accidental changes to its value and
      makes the tests more obviously correct.
    • In case we need to subclass the test fixture class, the subclass’
      constructor is guaranteed to call the base class’ constructor first, and
      the subclass’ destructor is guaranteed to call the base class’ destructor
      afterward. With SetUp()/TearDown(), a subclass may make the mistake of
      forgetting to call the base class’ SetUp()/TearDown() or call them at the
      wrong time.

    You may still want to use SetUp()/TearDown() in the following rare cases:

    • In the body of a constructor (or destructor), it’s not possible to use the
      ASSERT_xx macros. Therefore, if the set-up operation could cause a fatal
      test failure that should prevent the test from running, it’s necessary to
      use a CHECK macro or to use SetUp() instead of a constructor.
    • If the tear-down operation could throw an exception, you must use
      TearDown() as opposed to the destructor, as throwing in a destructor leads
      to undefined behavior and usually will kill your program right away. Note
      that many standard libraries (like STL) may throw when exceptions are
      enabled in the compiler. Therefore you should prefer TearDown() if you
      want to write portable tests that work with or without exceptions.
    • The googletest team is considering making the assertion macros throw on
      platforms where exceptions are enabled (e.g. Windows, Mac OS, and Linux
      client-side), which will eliminate the need for the user to propagate
      failures from a subroutine to its caller. Therefore, you shouldn’t use
      googletest assertions in a destructor if your code could run on such a
      platform.
    • In a constructor or destructor, you cannot make a virtual function call on
      this object. (You can call a method declared as virtual, but it will be
      statically bound.) Therefore, if you need to call a method that will be
      overridden in a derived class, you have to use SetUp()/TearDown().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one test a method that does some interactions with the local D-Bus
I have got a standard NUnit unit test. [TestFixture] public class MyTest { [SetUp]
I've created some MbUnit Test Fixtures that have SetUp methods marked with the SetUp
So I have a test class which has a setup methiod to create a
I'm trying to understand how the Google Test Fixtures work. Say I have the
I'm trying to understand why the following test does not fail. In this simplified
Facebook does not allow callbacks on ip addresses. So how does one test their
This question is spawned from the partial answer to JSF2: why does empty test
use Service layer to persist data into database. But the Unit Test does not
Why does the expression: test = cast(strtrim('3'), 'uint8') produce 51? This is also true

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.