2012-10-28 251 views
2

我想用单元测试测试源代码中的几个函数。现在如果我运行我的测试,我没有得到任何测试结果。Visual Studio 2010单元测试

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

#include <iostream> 

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

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

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

有谁知道这是为什么不工作?

+0

没有人知道吗?!?!? – Kipcak08

回答

2

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

你可以修改我的HelloWorld Snippet到rigtht的方式。所以我可以看到上下文中的功能。 – Kipcak08

+0

用一个例子更新了答案,但我的答案中的第一个链接真的总结了它。 – Blachshma

+0

现在非常感谢我会尝试一下。如果它不起作用,我会再次上传我的代码。 – Kipcak08

相关问题