Visual Studio 2010 Unit Test

853 views Asked by At

I want to test a couple of functions in my source code with unit tests. Now If I run my test I don't get any test results.

Here is a simple Code Snippet what I try to do:

#include <iostream>

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

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

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

Does anyone know why this is not working?

1

There are 1 answers

3
Blachshma On

The problem is that you're not performing the unit tests correctly. You should rely on not getting Asserts, not on printing to the console.

The concept is to check methods and make sure they return the correct value.

See the following links for more details:

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

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

Specifically using your code, an example of a correct unit test would be:

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

and creating a TestMethod which will Assert if the value is incorrect. For instance:

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