我希望对我的代码进行单元测试。这是我所拥有的任务中的专有步骤,我已经编写了代码。
我正在使用 VS Community 2017 v.15.9.7。我已按照本网站的说明逐行详细说明: https ://blogs.msdn.microsoft.com/vcblog/2017/04/19/cpp-testing-in-visual-studio/#Setup
但毕竟包含我得到两个错误:
1) 错误 LNK1120 1 未解决的外部 UnitTest1 \source\repos\Primes\Debug\UnitTest1.dll 1
2) 函数“public: void __thiscall UnitTest1::TestClass::IsOdd(void)”中引用的错误 LNK2019 未解析外部符号“public: bool __thiscall SearchPrimes::IsPrime(int)” (?IsPrime@SearchPrimes@@QAE_NH@Z) (?IsOdd@TestClass@UnitTest1@@QAEXXZ) UnitTest1 C:\Users\Velzevoul\source\repos\Primes\UnitTest1\unittest1.obj
我尝试过移动文件,但我认为随意移动它们弊大于利。我读到了将“stdafx.h”包含到我的“源”中,但这使事情变得更糟,因为越来越多的错误不断出现。
以下是我编写的代码的头文件:
#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>
};
这是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));
}
};
}
为了解决外部依赖关系,我必须做什么?文章说,一旦我按照步骤操作,我就可以开始了。正如文章所暗示的,该测试在一个单独的项目中。如果您认为问题可能与我的 main() 函数有关,请告诉我包含它。我现在不知道,因为它很长。
我提前感谢您的时间!