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)
// 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 insideTEST_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
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 detectcppunittest.h
by default, so must respectively appendUnitTest\include
andUnitTest\lib
toINCLUDE
andLIB
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
- Their pathes will be changed depending on environment and Visual Studio version.
You can confirm them at
/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
.
vstest.console /Platform:x64 app.dll
/Platform:x64
: Change tox86
orARM
depending on targeting platform.app.dll
: Specifycl
command output.
For more information, run vstest.console /?
(not /help
)