c++ - Visual Studio 2010 单元测试

标签 c++ visual-studio-2010 unit-testing white-box-testing

我想通过单元测试来测试源代码中的几个函数。现在,如果我运行测试,我不会得到任何测试结果。

这是我尝试做的一个简单的代码片段:

#include <iostream>

using namespace std;
namespace UnitTest
{
    [TestClass]
    public ref class UnitTestBlueSmart

        int main(){
        public:
        [TestMethod()]
        hello();
        }
}

void hello(){
 cout<<"Hello World!";
}

有人知道为什么这不起作用吗?

最佳答案

问题是您没有正确执行单元测试。 你应该依靠没有得到 Asserts ,而不是打印到控制台。

这个概念是检查方法并确保它们返回正确的值。

请参阅以下链接了解更多详细信息:

http://whinery.wordpress.com/2012/07/21/native-c-unit-testing-with-ms-test/

http://msdn.microsoft.com/en-us/library/ms182532.aspx

具体使用您的代码,正确的单元测试示例如下:

string hello()
{
 return "Hello World!";
}

并创建一个 TestMethod,如果值不正确,它将断言。 例如:

[TestMethod]
void HelloTest()
{
    string expected = "Hello World";
    string result = hello();
    Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, result);
}

关于c++ - Visual Studio 2010 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13110049/

相关文章:

visual-studio-2010 - 使用带有 Xcode 和 Visual Studio 的 CMake 构建 x86 和 x64 平台

visual-studio-2010 - Visual Studio 2010年白痴 list list

c# - 我可以复制 Moq Mock 实现吗?

javascript - TouchEvent非法构造函数

c++ - 无法从主 C++ 获取要打印的链表

c++ - 如何正确使用 boost::timed_mutex 和 scoped_lock

c++ - 在 xcode 4 中提升测试输出

c++ - 抓取字符串的一部分,如 substr

c# - web.config 转换未应用于发布或构建安装包

unit-testing - SPRING Roo 项目 - 版本控制中应该保留什么