【发布时间】:2019-08-18 22:28:27
【问题描述】:
我正在尝试对 C++ 中另一个单元测试项目(我们称之为 UnitTest1)的解决方案(我们称之为 Project1)的一个项目中的一个类执行一些基本的单元测试。我正在使用最新版本的 Visual Studio 2019。
我在 Visual Studio 2019 中为 c++ 创建了一个全新的解决方案,并在另一个文件中添加了一个带有类 HelloWorld 的控制台应用程序,该文件只有一个返回 std::string "Hello World" 的方法。
然后我在解决方案中添加了一个新的“本机单元测试项目”,在引用下添加了 Project1 控制台应用程序,并键入如下所示的代码:
project1 文件:
#include <iostream>
#include "HelloWorld.h"
int main() {
HelloWorld* hello = new HelloWorld();
std::cout << hello->sayHello();
}
HelloWorld.h:
#pragma once
#include <string>
class HelloWorld {
public: HelloWorld();
public: std::string sayHello();
};
HelloWorld.cpp:
#include "HelloWorld.h"
#include <string>
HelloWorld::HelloWorld() {
}
std::string HelloWorld::sayHello() {
return std::string("Hello World");
}
UnitTest1.cpp:
#include "pch.h"
#include "CppUnitTest.h"
#include "..//ConsoleApplication1/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1 {
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
HelloWorld* hello = new HelloWorld();
Assert::AreEqual(hello->sayHello(), std::string("Hello World"));
}
};
}
当我尝试通过测试资源管理器运行测试时,我得到:
1>------ Build started: Project: UnitTest1, Configuration: Debug Win32 ------
1>pch.cpp
1>UnitTest1.cpp
1> Creating library C:\Users\Iblob\source\repos\ConsoleApplication1\Debug\UnitTest1.lib and object C:\Users\Iblob\source\repos\ConsoleApplication1\Debug\UnitTest1.exp
1>UnitTest1.obj : error LNK2019: unresolved external symbol "public: __thiscall HelloWorld::HelloWorld(void)" (??0HelloWorld@@QAE@XZ) referenced in function "public: void __thiscall UnitTest1::UnitTest1::TestMethod1(void)" (?TestMethod1@UnitTest1@1@QAEXXZ)
1>UnitTest1.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall HelloWorld::sayHello(void)" (?sayHello@HelloWorld@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function "public: void __thiscall UnitTest1::UnitTest1::TestMethod1(void)" (?TestMethod1@UnitTest1@1@QAEXXZ)
1>C:\Users\Iblob\source\repos\ConsoleApplication1\Debug\UnitTest1.dll : fatal error LNK1120: 2 unresolved externals
1>Done building project "UnitTest1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
我希望链接器能够找到根据微软自己的教程引用的文件:https://docs.microsoft.com/en-us/visualstudio/test/writing-unit-tests-for-c-cpp?view=vs-2019
我尝试将类标头和 cpp 作为现有项目添加到我的单元测试项目中,但是当我尝试运行测试时,它只是尝试在 HelloWorld 类中查找#include "pch.h"。
我在这里缺少什么来告诉链接器在哪里可以找到类符号?
【问题讨论】:
-
您应该将
HelloWorld项目添加到单元测试项目依赖项中。 -
这已经完成了,我现在也通过右键单击 UnitTest1 > 构建依赖项 > 项目依赖项 > 包含 hello world 的项目的框进行了检查。
-
离题了,但是你创建了内存泄漏,你从来没有
delete创建的对象。也许并不重要,无论如何你都会退出,但你应该习惯于从一开始就预见到每个new的delete。更好:改用智能指针。更好的是:在本地范围内创建对象 (HelloWorld helloWorld;)。 C++ 不是 Java,您不需要为每个函数指定public。 -
题外话,使用 Assert.IsTrue(string1.Equals(string2)) 在单元测试中比较“Hello World”,因为字符串是引用类型。因此,当 Assert 尝试比较字符串时,它会比较引用而不是实际字符串。
-
@KethiriSundar 这是c++,直接比较就好了。
标签: c++ unit-testing visual-studio-2019