vendredi 6 novembre 2020

How to link a file properly in Visual Studio to run native tests?

I'm trying to write unit tests for my classes but I'm getting "unresolved external symbol." There are no syntax errors and I have tried looking up the cause on the official Microsoft website but unfortunately it can be caused for a million different reasons. Any suggestions on what I can do or what might be causing it? This is my code:

// Example.h file
#pragma once
#include <iostream>

using namespace std;

class Example {
public:
  int addTwoNumbers(int first, int second);
};
// Example.cpp file
#include "Example.h"
#include <iostream>

using namespace std;

int Example::addTwoNumbers(int first, int second) {
  return first + second;
}
// Test.cpp file
#include "pch.h"
#include "../Project/Example.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace Tests
{
  TEST_CLASS(Tests)
  {
  public:
    TEST_METHOD(UnitTest1)
    {
      Example e;
      Assert::AreEqual(4, e.addTwoNumbers(2, 2)); // this line is causing issues
    }
  };
}

Aucun commentaire:

Enregistrer un commentaire