简体   繁体   English

如何使用Visual Studio CppUnitTestFramework访问我的代码

[英]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. 我正在使用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 我已按照本网站的说明逐行进行了最详尽的说明: 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 1)错误LNK1120 1无法解析的外部单元Test1 \\ 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 2)错误LNK2019无法解析的外部符号“ public:bool __thiscall SearchPrimes :: IsPrime(int)”(?IsPrime @ SearchPrimes @@ QAE_NH @ Z)在函数“ 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. 我读到有关将“ stdafx.h”包含到“源”中的信息,但随着更多错误不断弹出,使情况变得更糟。

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 这是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. 如果您认为问题可能与我的main()函数有关,请告诉我包括该问题。 I do not right now, because it's quite lengthy. 我现在不在,因为它很长。

I thank you for your time in advance! 谢谢您的宝贵时间!

That article is suggesting that you can just link to a Windows executable the same way you would a DLL. 该文章建议您可以像DLL一样链接到Windows可执行文件。 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: 在C ++单元测试项目中,有两种方法可以访问被测代码:

  1. Add the source modules (.cpp/.h) to your unit test project. 将源模块(.cpp / .h)添加到您的单元测试项目中。
  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. 如果您的项目相对简单,只有几个.cpp模块,那么选择方法1可能是可行的方法。 Right-click your unit test project, select "Add -> Existing Item..." and add the .cpp modules you want to test. 右键单击您的单元测试项目,选择“添加->现有项...”,然后添加要测试的.cpp模块。

For a more complex project with many source modules, option 2 might be a better option. 对于具有许多源模块的更复杂的项目,选项2可能是更好的选择。 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, ie MyExecutable and MyExecutable.Test , MyLibrary and MyLibrary.Test , etc. 给单元测试项目一个名称,以指示它正在测试的项目,即MyExecutableMyExecutable.TestMyLibraryMyLibrary.Test等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM