title: How can I use Google Test to call specific test functions at specific places in the main() function?
body: <p>I am using TDD and Google Test for a project that involves numerical simulations. The state of the data gets changed within a loop in the <code>main</code> function, but there are requirements that need to be fulfilled after each change. If all requirements are fulfilled for a test case, the acceptance test passed: </p>
  
  <pre><code>while(simulation)
       modify simulation data
       FIRST_TEST()
       some other simulation operations
       SECOND_TEST()
  </code></pre>
  
  <p>The <a href="http://code.google.com/p/googletest/wiki/Primer#Test_Fixtures:_Using_the_Same_Data_Configuration_for_Multiple_Te" rel="nofollow noreferrer">GTest primer</a> states that usually RUN_ALL_TESTS() is called. The <a href="http://code.google.com/p/googletest/wiki/AdvancedGuide#Running_Test_Programs:_Advanced_Options" rel="nofollow noreferrer">Advanced Guide</a> shows how to run sub-tests, by <em>filtering out tests</em> from RUN_ALL_TESTS. However, I would like to know how to invoke tests individually.</p>
  
  <p>Otherwise I would need to write a new acceptance testing application each time I need a new test like FIRST_ and SECOND_ in the pseudocode snippet above.</p>
  
  <p>Some more background: I’m using the OpenFOAM framework for Computational Fluid Dynamics, so creating global fixtures outside of <code>main</code> is not an option. The simulation application requires a directory and configuration files to run, and the global objects in main are correlated (require each other for initialization). One example of such an application is <a href="https://github.com/OpenFOAM/OpenFOAM-2.2.x/blob/master/applications/solvers/multiphase/interFoam/interFoam.C" rel="nofollow noreferrer"> OpenFOAM-2.2.x</a>.</p>
  
  <h3>Further Notes</h3>
  
  <p>I took the accepted answer as well as the answer on how to use argc and argv as global variables within the test found on <a href="https://stackoverflow.com/a/5262122/735756">another question on Stack Overflow</a> and I summarized this into this compile-able small model, maybe someone finds it useful:</p>
  
  <pre><code>#include &lt;gtest/gtest.h&gt;
  #include &lt;iostream&gt;
  
  class Type 
  {
      int value_ = 0;
      int times_ = 1; 
  
      public: 
  
          Type(int x) : value_(x) {}; 
  
          void operator()(){
              if (times_ &lt; 10) {
                  ++times_; 
                  value_ *= times_; 
              }
          }
  
          int value () const { return value_; } 
  }; 
  
  class mySeparatedTests : public ::testing::Test 
  {
      protected:
  
        template&lt;typename Type&gt;
        void TEST_ONE(Type const &amp;t)
        {
            ASSERT_TRUE((t.value() % 2) == 0); 
        }
  
        template&lt;typename Type&gt; 
        void TEST_TWO(Type const &amp; t)
        {
            ASSERT_TRUE((t.value() - 5) &gt; 0); 
        }
  };
  
  TEST_F(mySeparatedTests, testName)
  {
      extern char** globalArgv; 
      char** argv = globalArgv;
  
      // Simulation parameters and objects requiring argc and argv for initialization. 
      int simulationEndTime;  
  
      *argv[1] &gt;&gt; simulationEndTime;  
  
      Type typeObject(*argv[2]); 
  
      TEST_ONE(typeObject); 
  
      // Simulation loop. 
      for (int i = 0; i &lt; simulationEndTime; ++i)
      {
          typeObject(); 
  
          TEST_TWO(typeObject); 
      }
  }
  
  int globalArgc; 
  char** globalArgv; 
  
  int main(int argc, char **argv)
  {
      ::testing::InitGoogleTest(&amp;argc, argv);
  
      globalArgc = argc; 
      globalArgv = argv; 
  
      return RUN_ALL_TESTS(); 
  
      return 0; 
  }
  </code></pre>
  
  <p>This approach, as described in the accepted answer, ports the simulation code from <code>main</code> into TEST_F, and uses the <code>mySeparatedTests</code> class functions to define individual tests, that can then be called anywhere. This is compiled with:</p>
  
  <pre><code>g++ -std=c++11 -l gtest main.cpp  -o main
  </code></pre>
  
  <p>And tests fail/pass depending on parsed parameter pairs; </p>
  
  <p>this fails:</p>
  
  <pre><code>./main 1 1 
  </code></pre>
  
  <p>this succeeds:</p>
  
  <pre><code>./main 2 2 
  </code></pre>
  
  <p><strong>Note:</strong> I’m aware of char/int conversion happening; this is just to show how to pick up args. </p>