Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Unit Test in Visual Studio 2012

I am working with Microsoft Visual Studio 2012 Ultimate to write C++ applications. I got that version from my MSDNAA access. My Problem is that I want to create unit tests for the C++ classes I wrote.

Please Note: It's standard conform C++, nothing mixed, no C#, it's just C++ that can also be compiled with the g++.

Under file -> new -> project -> Visual C++ exists something like a "managed testproject":

However when I create such a project I cannot manage it to add references e.g. to "MyClass.h" and to compile. And I cannot find a simple tutorial for that.

Can anyone help me by showing how to set up a simple C++ Unit Test with Visual Studio 2012?

like image 480
Anonymous Avatar asked Feb 01 '13 13:02

Anonymous


People also ask

How do I run unit test in Visual Studio?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

Can you unit test C?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.


1 Answers

You have two choices for C++ unit tests Manage Test Project and Native Unit Test Project. You should select the native one, and then just add the includes you want and write the tests.

Here is a dummy example where I include a "foo.h" header, instantiate a foo and call one of its methods.

#include "stdafx.h"

#include "..\foo.h" // <- my header

#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{       
    TEST_CLASS(UnitTest1)
    {
    public:

        TEST_METHOD(TestMethod1)
        {
            foo f;
            Assert::AreEqual(f.run(), true);
        }
    };
}

See Unit testing existing C++ applications with Test Explorer for more.

like image 110
Marius Bancila Avatar answered Oct 12 '22 09:10

Marius Bancila