简体   繁体   English

Visual Studio 2015 C ++单元测试

[英]Visual Studio 2015 C++ Unit Test

I'm trying to learn how to use unit tests in Visual Studio with C++. 我正在尝试学习如何在Visual Studio中使用C ++进行单元测试。

I made an uber simple class called Adder that, as you might guess, adds 2 numbers. 我创建了一个名为Adder的超级简单类,正如您可能猜到的那样,它增加了2个数字。

Here is the .h file for it: 这是.h文件:

#ifndef ADDER
#define ADDER

class Adder {

private:
    int num1, num2;

public:
    Adder(int, int);
    ~Adder();

    int add();

};

#endif

So I made a new project with Native Unit Test and made a reference in it to my Adder project. 所以我用Native Unit Test创建了一个新项目,并在其中引用了我的Adder项目。 I couldn't include the .h file with just #include "adder.h" so I had to use a relative path. 我不能只包含#include "adder.h"的.h文件,所以我不得不使用相对路径。 I read through Microsoft's Tutorial and it looks like you have to use this kind of path. 我阅读了微软的教程 ,看起来你必须使用这种路径。

My problem is that I can't get the unit test to recognize the methods of my Adder class. 我的问题是我无法通过单元测试来识别Adder类的方法。 Here's my unit test: 这是我的单元测试:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "../addClass/adder.h" // Added a reference but still can't find without relative path

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{       
    TEST_CLASS(Adder)
    {
    public:

        TEST_METHOD(add) {

            Adder adder1(1, 1); // Won't recognize constructor

            int num = adder1.add(); // Won't recognize function

            Assert::AreEqual(2, num); 

        }

    };
}

I tried getting rid of the namespace UnitTest1 since it looked like Microsoft didn't have that in their tutorial. 我试图摆脱namespace UnitTest1因为它看起来像微软在他们的教程中没有。 This didn't work though. 但这并没有奏效。 I'm not sure how to get the Unit Test to recognize my method definitions. 我不知道如何让单元测试识别我的方法定义。

Just in case I messed something up in the implementation file for the Adder class i'll put it here but I don't think that's the problem. 万一我在Adder类的实现文件中搞砸了一些东西我会把它放在这里,但我不认为这是问题所在。

#include "adder.h"

Adder::Adder(int a=0, int b=1):num1(a), num2(b){}
Adder::~Adder(){}

int Adder::add() {

    return num1 + num2;

}

EDIT: Here are the errors i'm getting: 编辑:以下是我得到的错误:

line 15 in unittest1.cpp: no instance of constructor "UnitTest1::Adder::Adder" matches the argument list unittest1.cpp中的第15行:没有构造函数“UnitTest1 :: Adder :: Adder”的实例与参数列表匹配

line 17 in unittest1.cpp: a value of type "void" cannot be used to initalize an entity of type "int" unittest1.cpp中的第17行:类型为“void”的值不能用于初始化“int”类型的实体

line 15 in unittest1.cpp: 'UnitTest::Adder:: UnitTest1 Adder': no overloaded function takes 2 arguments unittest1.cpp中的第15行:'UnitTest :: Adder :: UnitTest1 Adder':没有重载函数需要2个参数

line 17 in unittest1.cpp: 'initializing': cannot convert from 'void' to 'int' unittest1.cpp中的第17行:'initializing':无法从'void'转换为'int'

EDIT 2: 编辑2:

Okay so I changed the unit test file to this: 好的,所以我将单元测试文件更改为:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "../addClass/adder.h" // Added a reference but still can't find without relative path

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{       
    TEST_CLASS(test)
    {
    public:

        TEST_METHOD(add) {

            ::Adder adder1(1,1); // recognizes constructor

            int num = adder1.add(); // recognizes function

            Assert::AreEqual(1, num); 

        }

    };
}

However now I get Linker errors: 但是现在我收到链接器错误:

LNK2019 unresolved external symbol "public: __thiscall Adder::Adder(int,int)" (??0Adder@@QAE@HH@Z) referenced in function "public: void __thiscall UnitTest1::test::add(void)" (?add@test@UnitTest1@@QAEXXZ) UnitTest1 C:\\Users\\Matt\\documents\\visual studio 2015\\Projects\\addClass\\UnitTest1\\unittest1.obj LNK2019未解析的外部符号“public:__thiscall Adder :: Adder(int,int)”(?? 0Adder @@ QAE @ HH @ Z)在函数“public:void __thiscall UnitTest1 :: test :: add(void)”中引用( ?add @ test @ UnitTest1 @@ QAEXXZ)UnitTest1 C:\\ Users \\ Matt \\ documents \\ visual studio 2015 \\ Projects \\ addClass \\ UnitTest1 \\ unittest1.obj

LNK2019 unresolved external symbol "public: __thiscall Adder::~Adder(void)" (??1Adder@@QAE@XZ) referenced in function "public: void __thiscall UnitTest1::test::add(void)" (?add@test@UnitTest1@@QAEXXZ) UnitTest1 C:\\Users\\Matt\\documents\\visual studio 2015\\Projects\\addClass\\UnitTest1\\unittest1.obj 1 LNK2019未解析的外部符号“public:__thiscall Adder :: ~Adder(void)”(?? 1Adder @@ QAE @ XZ)在函数“public:void __thiscall UnitTest1 :: test :: add(void)”中引用(?add @ test @ UnitTest1 @@ QAEXXZ)UnitTest1 C:\\ Users \\ Matt \\ documents \\ visual studio 2015 \\ Projects \\ addClass \\ UnitTest1 \\ unittest1.obj 1

LNK2019 unresolved external symbol "public: int __thiscall Adder::add(void)" (?add@Adder@@QAEHXZ) referenced in function "public: void __thiscall UnitTest1::test::add(void)" (?add@test@UnitTest1@@QAEXXZ) UnitTest1 C:\\Users\\Matt\\documents\\visual studio 2015\\Projects\\addClass\\UnitTest1\\unittest1.obj 1 LNK2019未解析的外部符号“public:int __thiscall Adder :: add(void)”(?add @ Adder @@ QAEHXZ)在函数“public:void __thiscall UnitTest1 :: test :: add(void)”中引用(?add @ test @ UnitTest1 @@ QAEXXZ)UnitTest1 C:\\ Users \\ Matt \\ documents \\ visual studio 2015 \\ Projects \\ addClass \\ UnitTest1 \\ unittest1.obj 1

I had a similar issue, but with an Application type project (as opposed to a DLL project). 我有一个类似的问题,但有一个应用程序类型项目(而不是DLL项目)。

For classes defined in the project under test which aren't exported, you have to tell the Linker of the tester-project where they are implemented. 对于未导出的测试项目中定义的类,您必须告知链接器测试器项目的实现位置。

Even after adding the project-under-test as a reference to the tester-project, I also had to modify the Linker options in the tester-project to include the object files which implement the classes under test. 即使在将测试项目添加为测试人员项目的参考之后,我还必须修改测试人员项目中的链接器选项,以包括实现被测试类的目标文件。

See Writing Unit tests for C/C++ with the Microsoft Unit Testing Framework for C++ for details. 有关详细信息,请参阅使用Microsoft Unit Testing Framework for C ++编写C / C ++单元测试 Specifically the section titled " To link the tests to the object or library files ". 特别是标题为“ 将测试链接到对象或库文件 ”的部分。

I went from having the same Linker errors you did to having a working test environment. 我从使用相同的链接器错误到拥有一个有效的测试环境。 It'd be nice if by "Referencing" the project-under-test from the tester-project, it'd handle this sort of stuff for you. 如果通过“引用”测试项目的测试项目,它会很好,它会为你处理这类东西。

I ran into this problem as well. 我也遇到了这个问题。 I found a couple things that worked, though I'm not sure either is the "correct" solution. 我找到了一些有用的东西,虽然我不确定是否是“正确的”解决方案。 One option is to include the .cpp file as well as the .h file in your unit test class. 一种选择是在单元测试类中包含.cpp文件和.h文件。 This is pretty ugly. 这非常难看。

The second option that worked was right-clicking on the unit test project, selecting Add, and selecting Existing Item. 第二个选项是右键单击单元测试项目,选择Add,然后选择Existing Item。 Navigate to the .cpp file in the window explorer that appears and select Add. 导航到出现的窗口浏览器中的.cpp文件,然后选择添加。 You can additionally create a new filter for storing these source files if you do not want them mixed in with your unit testing source files. 如果您不希望它们与您的单元测试源文件混合,您还可以创建一个用于存储这些源文件的新过滤器。

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

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