title: CppUnitTestFramework: Test Method Fails, Stack Trace Lists Line Number at the End of Method, Debug Test Passes
body: <p>I know, I know - that question title is very much all over the place. However, I am not sure what could be an issue here that is causing what I am witnessing.</p>
  <p>I have the following method in class <code>Project</code> that is being unit tested:</p>
  <pre><code>bool Project::DetermineID(std::string configFile, std::string&amp; ID)
  {
      std::ifstream config;
      config.open(configFile);
  
      if (!config.is_open()) {
          WARNING &lt;&lt; &quot;Failed to open the configuration file for processing ID at: &quot; &lt;&lt; configFile;
          return false;
      }
  
      std::string line = &quot;&quot;;
      ID = &quot;&quot;;
  
      bool isConfigurationSection = false;
      bool isConfiguration = false;
  
      std::string tempID = &quot;&quot;;
  
      while (std::getline(config, line))
      {
          std::transform(line.begin(), line.end(), line.begin(), ::toupper); // transform the line to all capital letters
          boost::trim(line);
  
          if ((line.find(&quot;IDENTIFICATIONS&quot;) != std::string::npos) &amp;&amp; (!isConfigurationSection)) {
  
              // remove the &quot;IDENTIFICATIONS&quot; part from the current line we're working with
              std::size_t idStartPos = line.find(&quot;IDENTIFICATIONS&quot;);
              line = line.substr(idStartPos + strlen(&quot;IDENTIFICATIONS&quot;), line.length() - idStartPos - strlen(&quot;IDENTIFICATIONS&quot;));
              boost::trim(line);
  
              isConfigurationSection = true;
          }
  
          if ((line.find('{') != std::string::npos) &amp;&amp; isConfigurationSection) {
  
              std::size_t bracketPos = line.find('{');
  
              // we are working within the ids configuration section
              // determine if this is the first character of the line, or if there is an ID that precedes the { 
  
              if (bracketPos == 0) {
                  // is the first char
                  // remove the bracket and keep processing
  
                  line = line.substr(1, line.length() - 1);
                  boost::trim(line);
              }
              else {
                  // the text before { is a temp ID
  
                  tempID = line.substr(0, bracketPos - 1);
                  isConfiguration = true;
  
                  line = line.substr(bracketPos, line.length() - bracketPos);
                  boost::trim(line);
              }
          }
  
          if ((line.find(&quot;PORT&quot;) != std::string::npos) &amp;&amp; isConfiguration) {
  
              std::size_t indexOfEqualSign = line.find('=');
  
              if (indexOfEqualSign == std::string::npos) {
                  WARNING &lt;&lt; &quot;Unable to determine the port # assigned to &quot; &lt;&lt; tempID;
              }
              else {
                  std::string portString = &quot;&quot;;
                  portString = line.substr(indexOfEqualSign + 1, line.length() - indexOfEqualSign - 1);
                  boost::trim(portString);
  
                  // confirm that the obtained port string is not an empty value
  
                  if (portString.empty()) {
                      WARNING &lt;&lt; &quot;Failed to obtain the \&quot;Port\&quot; value that is set to &quot; &lt;&lt; tempID;
                  }
                  else {
  
                      // attempt to convert the string to int
  
                      int workingPortNum = 0;
  
                      try {
                          workingPortNum = std::stoi(portString);
                      }
                      catch (...) {
                          WARNING &lt;&lt; &quot;Failed to convert the obtained \&quot;Port\&quot; value that is set to &quot; &lt;&lt; tempID;
                      }
  
                      if (workingPortNum != 0) {
                          // check if this port # is the same port # we are publishing data on
  
                          if (workingPortNum == this-&gt;port) {
                              ID = tempID;
                              break;
                          }
                      }
                  }
              }
          }
      }
  
      config.close();
  
      if (ID.empty())
          return false;
      else
          return true;
  }
  </code></pre>
  <p>The goal of this method is to parse any text file for the ID portion, based on matching the port # that the application is publishing data to.</p>
  <p>Format of the file is like this:</p>
  <pre><code>Idenntifications {
    ID {
      port = 1001
    }
  }
  </code></pre>
  <p>In a separate Visual Studio project that unit tests various methods, including this <code>Project::DetermineID</code> method.</p>
  <pre><code>#define STRINGIFY(x) #x
  #define EXPAND(x) STRINGIFY(x)
  
  TEST_CLASS(ProjectUnitTests) {
      Project* parser;
      std::string projectDirectory;
  
      TEST_METHOD_INITIALIZE(ProjectUnitTestInitialization) {
          projectDirectory = EXPAND(UNITTESTPRJ);
          projectDirectory.erase(0, 1);
          projectDirectory.erase(projectDirectory.size() - 2);
  
          parser = Project::getClass(); // singleton method getter/initializer
      }
  
      // Other test methods are present and pass/fail accordingly
  
      TEST_METHOD(DetermineID) {
          std::string ID = &quot;&quot;;
  
          bool x = parser -&gt;DetermineAdapterID(projectDirectory + &quot;normal.cfg&quot;, ID);
          Assert::IsTrue(x);
      }
  };
  </code></pre>
  <p>Now, when I run the tests, <code>DetermineID</code> fails and the stack trace states:</p>
  <pre><code>   DetermineID
     Source: Project Tests.cpp line 86
     Duration: 2 sec
  
    Message: 
      Assert failed
    Stack Trace: 
      ProjectUnitTests::DetermineID() line 91
  </code></pre>
  <p>Now, in my test .cpp file, <code>TEST_METHOD(DetermineID) {</code> is present on line 86. But that method's <code>}</code> is located on line 91, as the stack trace indicates.</p>
  <p>And, when debugging, the unit test passes, because the return of <code>x</code> in the <code>TEST_METHOD</code> is <code>true</code>.
  Only when running the test individually or running all tests does that test method fail.</p>
  <p>Some notes that may be relevant:</p>
  <ul>
  <li>This is a single-threaded application with no tasks scheduled (no race condition to worry about supposedly)</li>
  <li>There is another method in the <code>Project</code> class that also processes a file with an <code>std::ifstream</code> same as this method does
  <ul>
  <li>That method has its own test method that has been written and passes without any problems</li>
  <li>The test method also access the &quot;normal.cfg&quot; file</li>
  </ul>
  </li>
  <li>Yes, <code>this-&gt;port</code> has an assigned value</li>
  </ul>
  <p>Thus, my questions are:</p>
  <ol>
  <li>Why does the stack trace reference the closing bracket for the test method instead of the single <code>Assert</code> within the method that is supposedly failing?</li>
  <li>How to get the unit test to pass when it is ran? (Since it currently only plasses during debugging where I can confirm that <code>x</code> is <code>true</code>).</li>
  <li>If the issue is a race condition where perhaps the other test method is accessing the &quot;normal.cfg&quot; file, why does the test method fail even when the method is individually ran?</li>
  </ol>
  <p>Any support/assistance here is very much appreciated. Thank you!</p>