title: GoogleTest Test Fixture Clarification
body: <p>When I write Test fixture to test some c code I use the same set up from:
  <a href="https://github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests" rel="nofollow noreferrer">https://github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests</a>. The c code to be tested is as such:</p>
  
  <pre><code>static struct {
      uint32_t success;
      uint32_t errors;
  }stats;
  
  uint32_t get_errors(void)
  {
      return stats.errors;
  }
  uint32_t get_success(void)
  {
      return stats.success;
  }
  
  void increment_errors(void)
  {
      stats.errors++;
  }
  void increment_success(void)
  {
      stats.success++;
  }
  
  void main_function(int arg)
  {
      if (arg &gt;=0)
          increment_success();
      else
          increment_errors();
  }
  
  </code></pre>
  
  <p>Now when I write unit tests for this as such:</p>
  
  <pre><code>class MyTest : public ::testing::Test
  {
  protected:
  
      void SetUp(void)
      {
      }
  
      void TearDown(void)
      {
      }
  };
  
  TEST_F(MyTest, Test1)
  {
          main_function(1);
      EXPECT_EQ(1, decoder_get_success());
      EXPECT_EQ(0, decoder_get_errors());
  }
  
  TEST_F(MyTest, Test2)
  {
          main_function(40);
      EXPECT_EQ(1, decoder_get_success()); //This is a fail as number ends up being 2 not 1 which includes prev. test results
      EXPECT_EQ(0, decoder_get_errors());
  }
  </code></pre>
  
  <p>Now I have noticed that when I write different test fixtures for this code the values of the stats struct variables do not reset i.e. if first test is supposed to increment success number then when we start the second test the number of success = 1 not 0 and so on and so forth. I find this behavior odd as I assumed it is supposed to reset everything with each test. </p>
  
  <p>To solve this issue I ended up adding the following function to c code:</p>
  
  <pre><code>void stats_init(void)
  {
      decoder_stats.errors = 0;
      decoder_stats.success = 0;
  }
  </code></pre>
  
  <p>and add this to the TearDown():</p>
  
  <pre><code>void TearDown(void)
  {
      stats_init();
  }
  </code></pre>
  
  <p>This makes sure that all gets reset. The question here is this the correct behavior for gtests when using test fixtures? Am I wrong to assume that it should not require m to define the init() function and add it to TearDown()?</p>