Dela via


Unit Tests and C++

Unit tests can give you a quick way to check for logic errors in the methods of classes in your managed C++ code. A unit test can be created one time and run every time that source code is changed to make sure that no bugs are introduced. You can use unit tests with managed Visual C++ according to the specifics described in this topic. You can also use unit tests in Visual C# and Visual Basic. For more information, see How to: Create and Run a Unit Test.

Determining How to Test Your C++ Code

C++ project type

Testing method

Unit test support

Unmanaged DLL

EXE

  • Create C++ test project, change to /clr and call Code Under Test in regular C++ way.

  • Create Visual Basic, C#, or C++ Test Project and use Interop [DllImport].

No

Com Object

  • Use COM reference and call it in COM way.

No

Managed

/clr or /clr:pure

No

Managed

/clr:safe

  • Unit tests

Yes

Additional notes

  • You cannot have test projects with unit tests that use unmanaged C++.

    Here are some additional notes about the /clr: option.

    • 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 can do so at any time by using Visual C++ compiler options. For more information, see /clr (Common Language Runtime Compilation).

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

    • /clr:safe (the default) and /clr:pure does not permit calls to unmanaged code.

    • If you have to call unmanaged code, use /clr.

See Also

Reference

Compiler Options

Concepts

Creating and Running Unit Tests for Existing Code

Unit Tests For Generic Methods