Skip to content

Instantly share code, notes, and snippets.

@retorillo
Last active July 7, 2018 18:05
Show Gist options
  • Save retorillo/cdad6e8884d4003b50f1b66dbb9352d1 to your computer and use it in GitHub Desktop.
Save retorillo/cdad6e8884d4003b50f1b66dbb9352d1 to your computer and use it in GitHub Desktop.

Writing C++ Unit Test From Scratch (without Visual Studio IDE) for VS2015

This post provides super-simplified examples that describes how to write C++ unit test without any Visual Studio IDE assistant and template. (Tested on Visual Studio 2015 Community edition)

In fact unit test can be accomplished by only using cl and vstest.console commands. To use these, start with VS Tools Command Prompt, or load vcvarsall on vanilla-cmd.exe to configure. (manual enviromental configuration is too complex and not recommended)

Write test

// test.cpp
#include <cppunittest.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

// Your modules (optional)
#include "../app.h"

TEST_CLASS(Test){
public:
  TEST_METHOD(Zero_Should_Be_Zero) {
    Assert::AreEqual(0, 0);
  }
};
  • On this post, file name is assumed as test.cpp, but, of course, can change it as you like.
  • TEST_METHOD must be defined inside TEST_CLASS. Note that class name and method name for them are not string but identifier, so never be surrounded by double-quotes.
  • About assersion and syntax are described at Using Microsoft.VisualStudio.TestTools.CppUnitTestFramework

Build DLL

set INCLUDE=%INCLUDE%;%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\UnitTest\include;
set LIB=%LIB%;%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\UnitTest\lib;
cl /EHsc /Zi /LDd test.cpp ../app.cpp /Fe:test.dll
  • Note that cl cannot detect cppunittest.h by default, so must respectively append UnitTest\include and UnitTest\lib to INCLUDE and LIB as the above.
    • Their pathes will be changed depending on environment and Visual Studio version. You can confirm them at <Visual Studio Installed Path>\VC\UnitTest
  • /EHsc: Enables C++ exception handing. Mandatory for modern C++ application.
  • /Zi: (Optional) Includes debug information. Omitting this allows boost your build speed.
  • /LD: Mandatory to build as DLL.
  • test.cpp: Your unit test code file path.
  • ../app.cpp: (Optional) Enumerate here code files on which your test depend.
  • /Fe:test.dll: (Optional) Use to change output DLL path.

For more information, run cl /help.

Run unit test

vstest.console /Platform:x64 app.dll
  • /Platform:x64: Change to x86 or ARM depending on targeting platform.
  • app.dll: Specify cl command output.

For more information, run vstest.console /? (not /help)

Writing C++ Unit Test From Scratch (without Visual Studio IDE) for VS2017

Tested on Visual Studio 2017 Community Edition.

Component Installation

The following individual component might be required:

[ Visual Studio Installer ]

Indivisual components
   `--- Debugging and testing
           `--- Testing tools core features

Include directory

include and lib directory is different to 2015.

VS Version Directory
2015 %ProgramFiles(x86)%\Microsoft Visual Studio 14.0\VC\UnitTest\
2017 %ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\VS\UnitTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment