title: C++ Unit Test in Visual Studio 2019
body: <p>I am having a problem with Visual Studio 2019 CPPUnitTestFramework. I follow the instructions, but every time I get an error. I have looked for tutorials, but anything I get is for Visual Studio 2017 and it does not solve my problem.</p>
  <p>I am writing a program that uses OOP in C++ and I want to make unit tests, since it is going to be a considerably long project. The problem that I am having is that the program is not compiling in the test module.</p>
  <p>Consider that I have the code such that I have the header file:</p>
  <pre><code>//A.h
  #pragma once
  class A
  {
  private:
     // One parameter.
     int a;
  
  public:
  
     // Add function.
     int add(int, int);
  
     // Subtract function.
     int subtract(int, int);
  
     A();
  };
  </code></pre>
  <p>with the proper source file:</p>
  <pre><code>// A.cpp
  #include &quot;A.h&quot;
  int a;
  
  int A::add(int alpha, int beta)
  {
      return alpha + beta;
  }
  
  int A::subtract(int alpha, int beta)
  {
      return alpha - beta;
  }
  
  A::A()
  {
      a = 4;
  }
  </code></pre>
  <p>The structure of the program looks something like this:</p>
  <p><a href="https://i.stack.imgur.com/wwhHG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wwhHG.png" alt="enter image description here" /></a></p>
  <p>To make my Unit Test, I right click on the &quot;Solution 'TestTestUnit'&quot; label and choose new project, look for the unit test, add the unit test and attach the reference, such that I get a file structure such as the one below:</p>
  <p><a href="https://i.stack.imgur.com/cUyQV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cUyQV.png" alt="enter image description here" /></a></p>
  <p>To perform the unit test I write the code:</p>
  <pre><code>// TestUnitForTestTestUnit.cpp
  #include &quot;pch.h&quot;
  #include &quot;CppUnitTest.h&quot;
  
  #include &quot;../TestTestUnit/A.h&quot;
  
  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  
  namespace TestUnitForTestTestUnit
  {
     TEST_CLASS(TestUnitForTestTestUnit)
     {
     public:
      
        TEST_METHOD(TestMethod1)
        {
           A first;
           Assert::AreEqual(first.add(3, 2), 5);
        }
     };
  }
  </code></pre>
  <p>When I try to run a test, the Test Explorer does nothing and throws the message: <strong>Aborting test run due to build failures. Please see the build for more details.</strong></p>
  <p><a href="https://i.stack.imgur.com/91335.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/91335.png" alt="enter image description here" /></a></p>
  <p>I cannot find the mistake here. The program runs perfect, but when instantiating a new &quot;A&quot; object the test fails. I am stuck here, are there any suggestions? What am I doing wrong (besides developing in Windows)?</p>
  <p><strong>UPDATE</strong>:</p>
  <p>I have followed the suggestion to remove the namespace, as suggested by @ChrisMM, so that the test file now reads:</p>
  <pre><code>// TestUnitForTestTestUnit.cpp
  #include &quot;pch.h&quot;
  #include &quot;CppUnitTest.h&quot;
  
  #include &quot;../TestTestUnit/A.h&quot;
  
  using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  
  TEST_CLASS(TestUnitForTestTestUnit)
  {
  public:
      
     TEST_METHOD(TestMethod1)
     {
        A first;
        Assert::AreEqual(first.add(3, 2), 5);
     }
  };
  </code></pre>
  <p>such that when I run the Test Explorer gives the same message:</p>
  <p><a href="https://i.stack.imgur.com/kx8c4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kx8c4.png" alt="enter image description here" /></a></p>
  <p>with error message:</p>
  <pre><code>1&gt;------ Build started: Project: TestUnitForTestTestUnit, Configuration: Debug Win32 ------
  1&gt;   Creating library C:\Users\&lt;user&gt;\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.lib and object C:\Users\&lt;user&gt;\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.exp
  1&gt;TestUnitForTestTestUnit.obj : error LNK2019: unresolved external symbol &quot;public: int __thiscall A::add(int,int)&quot; (?add@A@@QAEHHH@Z) referenced in function &quot;public: void __thiscall TestUnitForTestTestUnit::TestMethod1(void)&quot; (?TestMethod1@TestUnitForTestTestUnit@@QAEXXZ)
  1&gt;TestUnitForTestTestUnit.obj : error LNK2019: unresolved external symbol &quot;public: __thiscall A::A(void)&quot; (??0A@@QAE@XZ) referenced in function &quot;public: void __thiscall TestUnitForTestTestUnit::TestMethod1(void)&quot; (?TestMethod1@TestUnitForTestTestUnit@@QAEXXZ)
  1&gt;C:\Users\&lt;user&gt;\Desktop\CPlusPlus\TestTestUnit\Debug\TestUnitForTestTestUnit.dll : fatal error LNK1120: 2 unresolved externals
  1&gt;Done building project &quot;TestUnitForTestTestUnit.vcxproj&quot; -- FAILED.
  ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
  </code></pre>
  <p>Help would be appreciated.</p>