【问题标题】:Visual Studio 2010 Unit TestVisual Studio 2010 单元测试
【发布时间】:2012-10-18 02:00:58
【问题描述】:

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

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

#include <iostream>

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    问题在于您没有正确执行单元测试。 你应该依靠没有得到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);
    }
    

    【讨论】:

    • 您能否将我的 HelloWorld 代码段修改为正确的方式。所以我可以在上下文中看到函数。
    • 用一个例子更新了答案,我的答案中的第一个链接确实总结了它。
    • 非常感谢,我会试试的。如果它不起作用,我将再次上传我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多