我想通过单元测试来测试源代码中的几个函数。现在,如果我运行测试,我不会得到任何测试结果。
这是我尝试做的一个简单的代码片段:
#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/