Search code examples
c++visual-studiomstestmicrosoft-cpp-unit-test

How to access my code using Visual Studio CppUnitTestFramework


I wish to unit test my code. It is a proprietary step in a task I have, the code of which I already wrote.

I am using VS Community 2017 v.15.9.7. I have followed the instructions of this site to the utmost detail, line by line: https://blogs.msdn.microsoft.com/vcblog/2017/04/19/cpp-testing-in-visual-studio/#Setup

But after all the includes I get two errors :

1) Error LNK1120 1 unresolved externals UnitTest1 \source\repos\Primes\Debug\UnitTest1.dll 1

2) Error LNK2019 unresolved external symbol "public: bool __thiscall SearchPrimes::IsPrime(int)" (?IsPrime@SearchPrimes@@QAE_NH@Z) referenced in function "public: void __thiscall UnitTest1::TestClass::IsOdd(void)" (?IsOdd@TestClass@UnitTest1@@QAEXXZ) UnitTest1 C:\Users\Velzevoul\source\repos\Primes\UnitTest1\unittest1.obj

I have tried moving files, but I thing randomly moving them around will do more harm than good. I read about including the "stdafx.h" to my "source", but that made thing worse, as more errors kept popping up.

Here are the header files of the code I wrote:

#pragma once

#include <vector>
#include "XMLParser.h"

class SearchPrimes 
{

public:
    std::vector<int> RangePrime(const std::pair<int, int>&);     
    //Setting the range to search for prime numbers, executing the algorithm

    bool IsPrime(int);  //The algorithm that checks if a number is prime
    bool IsOdd(int);    //Checking if a number if even or odd

};


#pragma once

#include <iostream>
#include <vector>


class XMLParser
{

public:

    void removeTags(std::string&);  //Removing the brackets of the tags of the .xml


    std::string openFile(std::string);  //Opening a file
    std::vector<std::string> readFile(const std::string&, std::string); 
   //Getting the text from the .xml file to a vector

    std::vector<std::pair<int, int> > stringsToInts();  
   //Finding the values of the tags that contain the ranges
   //and converting the string numbers to a vector<int>
};  

Here is the test.cpp

#include "stdafx.h"
#include "CppUnitTest.h"
#include "/Users/Velzevoul/source/repos/Primes/Primes/SearchPrimes.h"
#include "/Users/Velzevoul/source/repos/Primes/Primes/XMLParser.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{       
    TEST_CLASS(TestClass)
    {
    public:

        TEST_METHOD(IsOdd)
        {
            SearchPrimes prime;
            Assert::IsTrue(prime.IsPrime(4));
        }

    };
}

What do I have to do in order to solve the external dependencies? The article says, that once I follow the steps I can begin. The test is in a separate project, as the article suggests. If you think that the problem may be related to my main() function, please tell me to include it. I do not right now, because it's quite lengthy.

I thank you for your time in advance!


Solution

  • That article is suggesting that you can just link to a Windows executable the same way you would a DLL. I suppose this is theoretically be possible if your executable has been set up to export its functions, but it seems like an odd thing to do.

    There are two options for accessing the code under test in a C++ unit test project:

    1. Add the source modules (.cpp/.h) to your unit test project.
    2. Link with a library containing the code.

    If your project is relatively simple, with just a few .cpp modules, then option 1 is probably the way to go. Right-click your unit test project, select "Add -> Existing Item..." and add the .cpp modules you want to test.

    For a more complex project with many source modules, option 2 might be a better option. Create one or more library projects (static or dynamic) to contain your source modules, then link both the executable and unit test projects with the library.

    A good practice is to create one unit test project for each project to be tested. Give the unit test project a name that indicates what project it is testing, i.e. MyExecutable and MyExecutable.Test, MyLibrary and MyLibrary.Test, etc.