Click here to Skip to main content
14,972,912 members
Articles / DevOps / Testing
Posted 23 Oct 2013


24 bookmarked

Native Unit Tests using Visual Studio 2012

Rate me:
Please Sign up or sign in to vote.
4.45/5 (8 votes)
23 Oct 2013CPOL2 min read
Using the native unit test project in Visual Studio 2012.


Native C++ test unit frameworks are a lot, but In visual studio 2012 there is a fantastic project template called Native Unit Test and it's fully supported in the visual studio to run the tests in the test explorer.


I searched for testing frameworks for native C++, as many experienced programmers said Google Test Framework or known as GTest is the best, I used it is more advanced but it shows the result in a dos window coloring the alerts.., but in this visual studio test project it is different we will use the visual studio to write and run our tests

Using the code

We will create a solution that contains three projects.

  1. The first is a Win32 library called DemoBusiness that will contain our business classes. In this library I'll use the class that visual studio generated for me CDemoBusiness.
  2. C++
    // DemoBusiness.h 
    #define DEMOBUSINESS_API __declspec(dllexport)
    #define DEMOBUSINESS_API __declspec(dllimport)
    class CDemoBusinessTest;
    // This class is exported from the DemoBusiness.dll
    class DEMOBUSINESS_API CDemoBusiness
    private://private members
        int data;
        // TODO: add your methods here.
        template<class T> T Max(T item1, T item2) const
            if (item2 > item1)
                return item2;
            return item1;
        void SetData(int data);
        int GetData() const;
        int BadAdd(int num1, int num2) const;
        int ThrowingException() const;
        const int* ReturnNullData() const;
        const int* ReturnData() const;
        friend class CDemoBusinessTest;
    // DemoBusiness.cpp
    #include "stdafx.h"
    #include "DemoBusiness.h"
    CDemoBusiness::CDemoBusiness() : data(0)
    void CDemoBusiness::SetData( int data )
        this->data = data;
    int CDemoBusiness::GetData() const
        return this->data;
    int CDemoBusiness::BadAdd( int num1, int num2 ) const
        return num1 + num2 + 10;
    int CDemoBusiness::ThrowingException() const
        throw exception("Problem");
        return 0;
    const int* CDemoBusiness::ReturnNullData() const
        return nullptr;
    const int* CDemoBusiness::ReturnData() const
        return &data;
  3. The second project is the native unit test project to test the previous business project. Create it from File -> New -> Project -> c++ -> Test -> Native Unit Test.
  4. New native test project

    Add the dependency to the DemoBusiness project. add the output DemoBusiness.lib to the linker input and add its output directory to the libraries directory in C++ directories, make sure they have the same output directory if it is shared linking not static linking, it is recommended to use static linking.

    Delete the default unittest1.cpp file and add a new cpp file for the test, I have created the following test for our class:

    #include "stdafx.h"
    #include "../DemoBusiness/DemoBusiness.h"//this is the file containing the code to test
    #include <CppUnitTest.h>
    #include <exception>
    using namespace std;
    using namespace Microsoft::VisualStudio::CppUnitTestFramework;
    // the following attributes are optional just attributes for more details about the test module
    // mention this attributes once per any test module(test project)
        TEST_MODULE_ATTRIBUTE(L"Project", L"DemoBusiness")
        TEST_MODULE_ATTRIBUTE(L"Owner", L"Ahmed")
        TEST_MODULE_ATTRIBUTE(L"Date", L"22/10/2013")
    // the following method is optional also just in case of need
        Logger::WriteMessage(L"The test is starting....");
        //        Initialize some important data        //
    // the following method is optional also just in case of need
        Logger::WriteMessage(L"Finalizing the test.");
        //        Finalize any previously initialized data        //
            Logger::WriteMessage(L"Inside the CDemoBusinessTest");
        //optional info about the test class
            TEST_CLASS_ATTRIBUTE(L"Owner", L"Ahmed")
            TEST_CLASS_ATTRIBUTE(L"Descrioption", L"CDemoBusiness")
            TEST_CLASS_ATTRIBUTE(L"Priority", L"Critical")
        // optional initialization method if needed
            Logger::WriteMessage(L"Initializing the class");
        //optional finalization method if needed
            Logger::WriteMessage(L"Finalizing the class");
            TEST_DESCRIPTION(L"Make sure that data is initialized to zero, 
                  as data is private this testclass must be friend to our class")
            CDemoBusiness demo;
            Assert::AreEqual(, 1);
            //make sure that they are equal if not the test fails
            CDemoBusiness demo;
            double d1 = 192.123;
            double d2 = 192.122;
            Assert::AreEqual(demo.Max(d1, d2), d1);
            CDemoBusiness demo;
            int data = 100;
            Assert::AreEqual(demo.GetData(), data);
            CDemoBusiness demo;
            int num1 = 10;
            int num2 = 20;
            int addition = num1 + num2;
            Assert::AreNotEqual(demo.BadAdd(num1, num2), addition);
            CDemoBusiness demo;
            CDemoBusiness demo;
            CDemoBusiness demo;
            int x = 100;
            int& y = x;
            Assert::AreSame(x, y);
  5. The optional main application that will uses the business classes// in our case it is optional as we are discovering the native testing feature in VS2012.
    • *After adding any new logic to the DemoBusiness project go to the test and add your test method
    • *After any changes in the DemoBusiness build it then build the test module and run all the tests from the test explorer
    • *All the Logger::<span style="color: black; font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; white-space: pre; background-color: rgb(251, 237, 187);">WriteMessage(L"")</span> the notes will appear in the output window but select Tests in show output from.

Points of Interest

For more information about native unit testing:

  1. Using Microsoft.VisualStudio.TestTools.CppUnitTestFramework:
  2. Using Assert::ExpectException with Native Unit Testing in VS11:
  3. Running Unit Tests with Test Explorer:

This is the output of the testing:

Image 2


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ahmed Safan
Software Developer SyrianSoft
Egypt Egypt
Skills Vb6, VB.NET, C#, C++[OWL, MFC], C++.NET, C for microcontroller, PHP+MySQL+jQuery, ASP.NET MVC, Ms Sql server, Microsoft Reporting.
Software Developer @ SyrianSoft

Comments and Discussions

QuestionWriting unit tests for global, static & overloaded class member functions ? Pin
parikshitnangre29-Dec-15 21:43
Memberparikshitnangre29-Dec-15 21:43 
QuestionNot able to view the unit tests Pin
suma_ms3-Jun-14 11:30
Membersuma_ms3-Jun-14 11:30 
AnswerRe: Not able to view the unit tests Pin
Member 460889817-Jun-14 20:07
MemberMember 460889817-Jun-14 20:07 
AnswerRe: Not able to view the unit tests Pin
Ahmed Safan23-Jun-14 0:41
professionalAhmed Safan23-Jun-14 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.