【发布时间】:2019-02-19 09:27:23
【问题描述】:
我希望对我的代码进行单元测试。这是我所拥有的任务中的专有步骤,我已经编写了代码。
我正在使用 VS Community 2017 v.15.9.7。我已按照本网站的说明逐行详细说明: https://blogs.msdn.microsoft.com/vcblog/2017/04/19/cpp-testing-in-visual-studio/#Setup
但毕竟包含了两个错误:
1) Error LNK1120 1 unresolved externals UnitTest1 \source\repos\Primes\Debug\UnitTest1.dll 1
2) 函数“public: void __thiscall UnitTest1::TestClass::IsOdd( void)" (?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() 函数有关,请告诉我包含它。我现在不知道,因为它很长。
提前感谢您的宝贵时间!
【问题讨论】:
标签: c++ visual-studio mstest microsoft-cpp-unit-test