Unit Tests and C++
The document is archived and information here might be outdated

Unit Tests and C++

You can use unit tests in Visual C# and Visual Basic.


Some restrictions apply to the generation of unit tests for generic types and generic methods in Visual C# and Visual Basic. For more information, see Unit Tests and Generics.

You can also use unit tests in Visual C++ according to the specifics described in this topic.

The way you can use Visual C++ with unit tests depends on the role that Visual C++ plays in your solution. These are the possible cases:

Visual C++ Production Code

Please note the following about the code you want to test, which is referred to here as production code.

  • Unmanaged. If your production code is unmanaged, it needs to be callable from a static library, an .obj file, or a DLL entry point. Class methods that are embedded inside an executable file or a DLL will not work because unmanaged Visual C++ does not offer the runtime discovery mechanism, reflection. Therefore, only the traditional native binding mechanisms will work.

  • Mixed. A mixed project is a project that uses the /clr compiler option. If your production code is mixed, and not an executable file, any callable method in your code can be called and tested.

  • Safe or Pure. If your production code is safe or pure, any callable assembly method can be called and tested, in an executable file or in a DLL.

Test Projects and Visual C++

Your test project has different capabilities depending on the compiler options you have set for the project. For more information, see Compiler Options and /clr (Common Language Runtime Compilation). The following sections describe the capabilities available for various compiler option settings.


You cannot use unmanaged, or native, Visual C++ as the programming language for a test project.


A mixed project is a project that uses the /clr compiler option. This kind of a test project provides the ability to test the following production code:

  • Static native libraries

  • Native DLL entry points

  • Standalone .obj files

  • Mixed-mode DLL assemblies with methods that are callable. This does not include executable files because they contain unmanaged code, and unmanaged executable code is generally not re-base address enabled.

  • Any managed method that is callable. This is code that is compiled with the /clr:pure or /clr:safe compiler option.

Safe or Pure

If you are using either the /clr:pure or the /clr:safe compiler option with your test project, it provides the ability to test any managed method that is callable. This means production code that is compiled with the /clr, /clr:pure, or /clr:safe compiler option.

Code Generation and Visual C++

You can generate unit tests into a Visual C++ test project. You can generate these tests from a Visual C++ production code project. Please note the following:

  • Production code project. If your production code is written in Visual C++, you can generate unit tests only if your product uses the /clr:safe compiler option. 

  • Test project. Code generation can produce unit tests in any of the Visual C++ test project types: mixed, safe, and pure. The default project type produced is a /clr:safe project. If you want to change your project to /clr or /clr:pure, you do so at any time by using Visual C++ compiler options. For more information, see /clr (Common Language Runtime Compilation).

See Also

© 2016 Microsoft