0

I'm trying to reproduce an issue, so I have created an empty MFC C++ application using the VS2019 wizard and a separate native Unit Test project.

Before adding the Unit Test project, the MFC application compiled and launched successfully.

The MFC application still compiles successfully, but the Unit Test project will not compile. I'm getting two errors:

E0035 #error directive: "include 'pch.h' before including this file for PCH"
C1189 #error: "include 'pch.h' before including this file for PCH"

However, the only file in the Unit Test project (UnitTest1.cpp) already includes pch.h at the top of the file:

#include "pch.h"
#include "CppUnitTest.h"
#include "../MFCApplication1/MFCApplication1.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest1
{
    TEST_CLASS(UnitTest1)
    {
    public:
        
        TEST_METHOD(TestMethod1)
        {
            CMFCApplication1App app;
            bool result = app.InitInstance();
            Assert::IsTrue(result);
        }
    };
}

It seems to be telling me to do something that is already done.

What's going on here?

afarley
  • 781
  • 6
  • 26
  • Where you ever able to get this working? I have the exact same situation, the only proposed and not accepted answer to the post at this time is not working. – Ty Deuty Sep 07 '21 at 19:45

2 Answers2

0

Try to include stdafx.h and remove pch.h, it resolves the issue

  • These should be equivalent or interchangeable, right? Can you explain why this would fix the issue? – afarley Sep 15 '21 at 19:42
0

I had the same issue. I found that I could not include the MFC DLL project's default header file directly. In your example this would be #include "../MFCApplication1/MFCApplication1.h"

I eventually found this example MFC DLL project code: https://github.com/Microsoft/VCSamples/tree/master/VC2010Samples/MFC/advanced from a list of examples here.

Notice that in the example that the default header/implementation files created by the MFC DLL project creation wizard (in this example, DLLScreenCap.h) don't export or provide any additional functionality. An existing MFC DLL project that I work with did the same.

So I added a class to the MFC DLL project to be tested and exported a simple function from it, and tested this exported function from my unit test project after linking to the project under test.

Exported class and function look like this:

#pragma once
class __declspec(dllexport) MFCLibraryExports
{
public:
    // tests returning 17 to test unit test library against an mfc project
    int SampleExport();
};

In your example I know that you are trying to test an instanced app and my answer doesn't help with that, but I was able to confirm that I can test at least a function exported from an MFC DLL project using the MS Unit Test framework. I'm not sure if you are expected to be able to get access to the application from a unit test as in your example; I am not able to include that header directly.

Ty Deuty
  • 137
  • 1
  • 2
  • 11
  • Hmm, it's possible that trying to test the application itself is not the right approach. I can extract the functionality to be tested into a library. Still, I'm confused by the error message about including PCH, because it seems like it's simply incorrect. – afarley Sep 15 '21 at 19:43