Unresolved externals when compiling unit tests for Visual C++ 2012

后端 未结 1 1804
情歌与酒
情歌与酒 2020-12-16 13:15

I want to create unit tests for a Visual C++ project. I tried following these MSDN instructions. I\'ve found pages where they differentiate between unmanaged/mixed/pure code

相关标签:
1条回答
  • 2020-12-16 13:39

    Here is a step-by-step description on how to add an EXE as an unit-test target.

    The key point is to "export" the functions/classes you want to test... You can download the complete sample here: http://blog.kalmbachnet.de/files/CPP_UnitTestApp.zip (I did not change any project settings, so all changes you can see in the source-code; of course, some parts can be made in the project settings).

    1. Create a Win32 Application (Console or MFC or Windows, does not matter); I created a console project called CPP_UnitTestApp:

    2. Add a function you want to test (you can also add classes). For example:

      int Plus1(int i)
      {
        return i+1;
      }
      
    3. Add a header file for the functions you want to test: CPP_UnitTestApp.h

    4. Put the declaration of the methods into the header file, and also export these functions!

      #pragma once
      
      #ifdef EXPORT_TEST_FUNCTIONS
      
      #define MY_CPP_UNITTESTAPP_EXPORT __declspec(dllexport)
      #else
      #define MY_CPP_UNITTESTAPP_EXPORT
      #endif
      
      MY_CPP_UNITTESTAPP_EXPORT int Plus1(int i);
      
    5. Include this header file in the main-cpp (here CPP_UnitTestApp.cpp) and define the EXPORT_TEST_FUNCTIONS before including the header:

      #define EXPORT_TEST_FUNCTIONS
      #include "CPP_UnitTestApp.h"
      
    6. Now add a new project (Native unit test project: UnitTest1)

    7. Include the header and the lib to the "unittest1.cpp" file (adopt the paths as you want):

      #include "..\CPP_UnitTestApp.h"
      #pragma comment(lib, "../Debug/CPP_UnitTestApp.lib")
      
    8. Go to the project settings of the test project add add a reference to the "UnitTest1" project (Project|Properties|Common Properties|Add New Reference...: Select under "Projects" the "CPP_UnitTestApp"-Project)

    9. Create the unit test function:

      TEST_METHOD(TestMethod1)
      {
        int res = Plus1(12);
        Assert::AreEqual(13, res);
      }
      
    10. Run your unit test ;)

    As you can see, the main point was to export the function declaration! This is done via __declspec(dllexport) even if it is an EXE.

    As I said, the demo project can be downloaded here: http://blog.kalmbachnet.de/files/CPP_UnitTestApp.zip

    0 讨论(0)
提交回复
热议问题