Visual Studio 2012中的C ++单元测试 [英] C++ Unit Test in Visual Studio 2012
问题描述
我正在使用Microsoft Visual Studio 2012 Ultimate编写C ++应用程序。我从我的MSDNAA访问的版本。我的问题是,我想为我编写的C ++类创建单元测试。
请注意:
它是标准的C ++,没有混合,没有C#,它只是C ++,也可以编译
$ b b
$ b
但是,当我创建这样的项目,我不能管理它添加引用eg到MyClass.h并编译。我不能找到一个简单的教程。
任何人都可以通过显示如何使用Visual Studio 2012设置一个简单的C ++单元测试来帮助我?
$您有两个选择C ++单元测试管理测试项目和本机单元测试项目。 。你应该选择原生的,然后只是添加你想要的包括并写测试。这里是一个虚拟示例,其中包含一个foo.h头,实例化一个 foo
#includestdafx.h
#include..\ foo.h//< - my header
#includeCppUnitTest.h
使用命名空间Microsoft :: VisualStudio :: CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
foo f;
Assert :: AreEqual(f.run(),true);
}
};
}
I am working with Microsoft Visual Studio 2012 Ultimate to write C++ applications. I got that version from my MSDNAA access. My Problem is that I want to create unit tests for the C++ classes I wrote.
Please Note: It's standard conform C++, nothing mixed, no C#, it's just C++ that can also be compiled with the g++.
Under file -> new -> project -> Visual C++ exists something like a "managed testproject":
However when I create such a project I cannot manage it to add references e.g. to "MyClass.h" and to compile. And I cannot find a simple tutorial for that.
Can anyone help me by showing how to set up a simple C++ Unit Test with Visual Studio 2012?
You have two choices for C++ unit tests Manage Test Project and Native Unit Test Project. You should select the native one, and then just add the includes you want and write the tests.
Here is a dummy example where I include a "foo.h" header, instantiate a foo
and call one of its methods.
#include "stdafx.h"
#include "..\foo.h" // <- my header
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
foo f;
Assert::AreEqual(f.run(), true);
}
};
}
See Unit testing existing C++ applications with Test Explorer for more.
这篇关于Visual Studio 2012中的C ++单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!