mardi 23 juillet 2019

Errors in Visual C++ testing in-out-of-the-box project

I'm trying to add Unit Testing to a native C++ MFC app and have had nothing but problems. I'd like to add my tests to the same project my main app is in (which is an EXE).

To try to figure out my problems, I tried to be as clean as possible:

My project, if you want to play along: https://drive.google.com/open?id=1ru6VU8_dBwiDHUdH_838QxHH4-oQyneG

1) Fresh install of Visual Studio 2019. I set things up for C++ desktop development w/ MFC support. I did not install Google Test or Boost Test adapters.

2) Create a new project, chose MFC App (which my real app is)

Everything is done in Debug / x86 (32 bit)

3) Following these instructions: https://docs.microsoft.com/en-us/visualstudio/test/how-to-use-microsoft-test-framework-for-cpp?view=vs-2019

4) Added include dir: $(VCInstallDir)Auxiliary\VS\UnitTest\include

5) Added lib dir: $(VCInstallDir)Auxiliary\VS\UnitTest\lib

6) Added new C++ file to project, used sample from: https://docs.microsoft.com/en-us/visualstudio/test/microsoft-visualstudio-testtools-cppunittestframework-api-reference?view=vs-2019#Initialize_and_cleanup

#include "pch.h"
#include <CppUnitTest.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

BEGIN_TEST_MODULE_ATTRIBUTE()
TEST_MODULE_ATTRIBUTE(L"Date", L"2010/6/12")
END_TEST_MODULE_ATTRIBUTE()

TEST_MODULE_INITIALIZE(ModuleInitialize)
{
    Logger::WriteMessage(L"In Module Initialize");
}

TEST_MODULE_CLEANUP(ModuleCleanup)
{
    Logger::WriteMessage(L"In Module Cleanup");
}

TEST_CLASS(Class1)
{

public:

    Class1()
    {
        Logger::WriteMessage(L"In Class1");
    }

    ~Class1()
    {
        Logger::WriteMessage(L"In ~Class1");
    }

    TEST_CLASS_INITIALIZE(ClassInitialize)
    {
        Logger::WriteMessage(L"In Class Initialize");
    }

    TEST_CLASS_CLEANUP(ClassCleanup)
    {
        Logger::WriteMessage(L"In Class Cleanup");
    }

    BEGIN_TEST_METHOD_ATTRIBUTE(Method1)
        TEST_OWNER(L"OwnerName")
        TEST_PRIORITY(1)
        END_TEST_METHOD_ATTRIBUTE()

    TEST_METHOD(Method1)
    {
        Logger::WriteMessage(L"In Method1");
        Assert::AreEqual(0, 0);
    }

    TEST_METHOD(Method2)
    {
        Assert::Fail(L"Fail");
    }
};

7) Solution builds succesfully.

8) Test explorer shows 2 tests. I Run All, both fail. Error seems to be in ModuleInitialize() in call to Logger::WriteMessage

[7/23/2019 1:27:39 PM Error] Exception Code: C0000005
    at ModuleCleanup() in C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\MyTests.cpp:line 17

Interestingly, that says it's in ModuleCleanup, but the error message clicking on the Red Circle / White X says ModuleInitialize. Further, if I Debug the test, the call stack is under ModuleInitialize.

9) If I go into Tools > Options > Test > General and change Logging Level to Diagnostic, I get this output:

[7/23/2019 1:28:37 PM Diagnostic] About to Enqueue operation 'RunAllOperation', hashcode:9936281 
[7/23/2019 1:28:37 PM Diagnostic] Enqueue operation 'RunAllOperation', hashcode:9936281 
[7/23/2019 1:28:37 PM Diagnostic] Operation left in the the queue: 1
[7/23/2019 1:28:37 PM Diagnostic]   'RunAllOperation', hashcode:9936281
[7/23/2019 1:28:37 PM Diagnostic] 

[7/23/2019 1:28:37 PM Diagnostic] Processing Queue .....
[7/23/2019 1:28:37 PM Diagnostic] Operation Dequeue : 'RunAllOperation'
[7/23/2019 1:28:37 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=OperationSetStarted, operationInProgress=False
[7/23/2019 1:28:37 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=OperationSetStarted, InProgress=False
[7/23/2019 1:28:37 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=ChangeDetectionStarting, operationInProgress=False
[7/23/2019 1:28:37 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=ChangeDetectionStarting, InProgress=False
[7/23/2019 1:28:37 PM Diagnostic] We programmatically start the build in TestWindowHost.UpdateContainer... 
[7/23/2019 1:28:38 PM Diagnostic] After we await the build that programmatically started in TestWindowHost.UpdateContainer... 
[7/23/2019 1:28:38 PM Diagnostic] TestContainer update (build) complete : 860 ms
[7/23/2019 1:28:38 PM Diagnostic] Adding the Project in Project Map for Guid 09306e37-d569-49e3-8c6f-55ba222f9bba
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://vsprojectoutputcontainerdiscoverer/v1, discovered 1 containers
[7/23/2019 1:28:38 PM Diagnostic] Containers from 'Microsoft.VisualStudio.TestWindow.VsAdapters.VsProjectOutputContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic]   C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\Debug\MSUnitTestTest.exe:executor://vsprojectoutputcontainerdiscoverer/v1
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://testexplorerservicecontainerdiscoverer/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.TestWindow.Extensibility.TestExplorerServiceContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://353e8aa3-86c4-43e8-a442-164c08626369/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.BoostTestContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://7573647c-28a2-4d17-9cc0-6eb757caba76/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.BoostVsTestContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://ebftest/1.0, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.ExternalBuildProjectContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://c2caea1d-e816-46db-9e64-94c922fcd024/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.GoogleVsContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://f225ba42-55fd-4056-b9fa-302522225610/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.GoogleVsInBoxContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://5fa831ee-f41c-4604-8b8d-bf32d5606ca1/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.GoogleGitHubContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://orderedtestadapter/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.MSTest.TestWindow.OrderedTestContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://generictestadapter/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.MSTest.TestWindow.GenericTestContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] test container discoverer executor://webtestadapter/v1, discovered 0 containers
[7/23/2019 1:28:38 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.MSTest.TestWindow.WebTestContainerDiscoverer' :
[7/23/2019 1:28:38 PM Diagnostic] DiscoveryOperation<RunAllOperation> FinishedChangedCotainers, changed container count is 0
[7/23/2019 1:28:38 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=ChangeDetectionFinished, operationInProgress=False
[7/23/2019 1:28:38 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=ChangeDetectionFinished, InProgress=False
[7/23/2019 1:28:38 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionStarting, operationInProgress=False
[7/23/2019 1:28:38 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionStarting, InProgress=False
[7/23/2019 1:28:38 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionStarted, operationInProgress=False
[7/23/2019 1:28:38 PM Informational] ------ Run test started ------
[7/23/2019 1:28:38 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionStarted, InProgress=False
[7/23/2019 1:28:38 PM Diagnostic] RunSettings Content:
<RunSettings>
  <RunConfiguration>
    <ResultsDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\</SolutionDirectory>
    <TargetPlatform>X86</TargetPlatform>
    <TargetFrameworkVersion>Framework45</TargetFrameworkVersion>
  </RunConfiguration>
</RunSettings>
[7/23/2019 1:28:38 PM Diagnostic] Value of UseSpecifiedAdapterLocations as specified in Tools -> Options: True
[7/23/2019 1:28:38 PM Diagnostic] Adapter location settings being used after evaluation: UseSpecifiedAdapterLocations = False, SkipDefaultAdapters = False, AreAllTestContainersCSharpOrVBProjectsOnly = False
[7/23/2019 1:28:38 PM Diagnostic] Final RunSettings for the current TestRunCriteria:
<RunSettings>
  <RunConfiguration>
    <ResultsDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\</SolutionDirectory>
    <TargetPlatform>X86</TargetPlatform>
    <TargetFrameworkVersion>Framework40</TargetFrameworkVersion>
    <CollectSourceInformation>True</CollectSourceInformation>
  </RunConfiguration>
</RunSettings>
[7/23/2019 1:28:40 PM Error] Exception Code: C0000005
    at ModuleCleanup() in C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\MyTests.cpp:line 17
[7/23/2019 1:28:40 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=OperationSetFinished, operationInProgress=False
[7/23/2019 1:28:40 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=OperationSetFinished, InProgress=False
[7/23/2019 1:28:40 PM Diagnostic] ***    Run finished using 'InMemoryUnitTestWriter' ***
[7/23/2019 1:28:40 PM Informational] ========== Run test finished: 2 run (0:00:01.9539482) ==========
[7/23/2019 1:28:40 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionFinished, operationInProgress=False
[7/23/2019 1:28:40 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionFinished, InProgress=False

10) Since the crash seemed to be in Logger::WriteMessage, I commented out all calls to Logger::WriteMessage and tried again. Still crashes, this time in TestClassImpl::CrtHandlersSetter in call to IsDebuggerAttached. Log contains:

[7/23/2019 2:16:28 PM Diagnostic] About to Enqueue operation 'RunAllOperation', hashcode:66314243 
[7/23/2019 2:16:28 PM Diagnostic] Enqueue operation 'RunAllOperation', hashcode:66314243 
[7/23/2019 2:16:28 PM Diagnostic] Operation left in the the queue: 1
[7/23/2019 2:16:28 PM Diagnostic]   'RunAllOperation', hashcode:66314243
[7/23/2019 2:16:28 PM Diagnostic] 

[7/23/2019 2:16:28 PM Diagnostic] Processing Queue .....
[7/23/2019 2:16:28 PM Diagnostic] Operation Dequeue : 'RunAllOperation'
[7/23/2019 2:16:28 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=OperationSetStarted, operationInProgress=False
[7/23/2019 2:16:28 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=OperationSetStarted, InProgress=False
[7/23/2019 2:16:28 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=ChangeDetectionStarting, operationInProgress=False
[7/23/2019 2:16:28 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=ChangeDetectionStarting, InProgress=False
[7/23/2019 2:16:28 PM Diagnostic] We programmatically start the build in TestWindowHost.UpdateContainer... 
[7/23/2019 2:16:28 PM Diagnostic] After we await the build that programmatically started in TestWindowHost.UpdateContainer... 
[7/23/2019 2:16:28 PM Diagnostic] TestContainer update (build) complete : 542 ms
[7/23/2019 2:16:28 PM Diagnostic] Adding the Project in Project Map for Guid 09306e37-d569-49e3-8c6f-55ba222f9bba
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://vsprojectoutputcontainerdiscoverer/v1, discovered 1 containers
[7/23/2019 2:16:28 PM Diagnostic] Containers from 'Microsoft.VisualStudio.TestWindow.VsAdapters.VsProjectOutputContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic]   C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\Debug\MSUnitTestTest.exe:executor://vsprojectoutputcontainerdiscoverer/v1
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://testexplorerservicecontainerdiscoverer/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.TestWindow.Extensibility.TestExplorerServiceContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://66783bf0-ccb1-4169-8acc-0ab56235af80/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.BoostTestContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://fdfb8d12-5f69-4cba-a2f3-e104d80b7f94/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.BoostVsTestContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://ebftest/1.0, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.ExternalBuildProjectContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://7ebc7e50-a310-497b-a268-0a992b204497/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.GoogleVsContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://9bb99e40-f237-4b21-b326-205ddf200b69/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.GoogleVsInBoxContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://0fbbb65f-8d69-4bdd-9617-417d847e3a88/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.Workspace.ExternalBuildFramework.Impl.TestExplorer.GoogleGitHubContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://orderedtestadapter/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.MSTest.TestWindow.OrderedTestContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://generictestadapter/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.MSTest.TestWindow.GenericTestContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] test container discoverer executor://webtestadapter/v1, discovered 0 containers
[7/23/2019 2:16:28 PM Diagnostic] No containers found from 'Microsoft.VisualStudio.MSTest.TestWindow.WebTestContainerDiscoverer' :
[7/23/2019 2:16:28 PM Diagnostic] DiscoveryOperation<RunAllOperation> FinishedChangedCotainers, changed container count is 0
[7/23/2019 2:16:28 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=ChangeDetectionFinished, operationInProgress=False
[7/23/2019 2:16:28 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=ChangeDetectionFinished, InProgress=False
[7/23/2019 2:16:28 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionStarting, operationInProgress=False
[7/23/2019 2:16:28 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionStarting, InProgress=False
[7/23/2019 2:16:28 PM Informational] ------ Run test started ------
[7/23/2019 2:16:28 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionStarted, operationInProgress=False
[7/23/2019 2:16:28 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionStarted, InProgress=False
[7/23/2019 2:16:28 PM Diagnostic] RunSettings Content:
<RunSettings>
  <RunConfiguration>
    <ResultsDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\</SolutionDirectory>
    <TargetPlatform>X86</TargetPlatform>
    <TargetFrameworkVersion>Framework45</TargetFrameworkVersion>
  </RunConfiguration>
</RunSettings>
[7/23/2019 2:16:28 PM Diagnostic] Value of UseSpecifiedAdapterLocations as specified in Tools -> Options: True
[7/23/2019 2:16:28 PM Diagnostic] Adapter location settings being used after evaluation: UseSpecifiedAdapterLocations = False, SkipDefaultAdapters = False, AreAllTestContainersCSharpOrVBProjectsOnly = False
[7/23/2019 2:16:29 PM Diagnostic] Final RunSettings for the current TestRunCriteria:
<RunSettings>
  <RunConfiguration>
    <ResultsDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\TestResults</ResultsDirectory>
    <SolutionDirectory>C:\Work\Testing\MSUnitTestTest\MSUnitTestTest\</SolutionDirectory>
    <TargetPlatform>X86</TargetPlatform>
    <TargetFrameworkVersion>Framework40</TargetFrameworkVersion>
    <CollectSourceInformation>True</CollectSourceInformation>
  </RunConfiguration>
</RunSettings>
[7/23/2019 2:16:34 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=OperationSetFinished, operationInProgress=False
[7/23/2019 2:16:34 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=OperationSetFinished, InProgress=False
[7/23/2019 2:16:34 PM Error] The active test run was aborted. Reason: Test host process crashed : Process is terminated due to StackOverflowException.

[7/23/2019 2:16:34 PM Diagnostic] Cancel requested, actions left in queue 2, exiting...
[7/23/2019 2:16:34 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionCanceling, operationInProgress=False
[7/23/2019 2:16:34 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionCanceling, InProgress=False
[7/23/2019 2:16:34 PM Diagnostic] ***    Run finished using 'InMemoryUnitTestWriter' ***
[7/23/2019 2:16:34 PM Informational] ========== Run test finished: 0 run (0:00:05.9716557) ==========
[7/23/2019 2:16:34 PM Diagnostic] VirtualReadOnlyTestDataStore.OperationStateChanged State=TestExecutionCancelAndFinished, operationInProgress=False
[7/23/2019 2:16:34 PM Diagnostic] TestDiscoveryStats.OperationStateChanged State=TestExecutionCancelAndFinished, InProgress=False

I've spent a lot of time on this in various permutations. I'd really prefer to keep the testing code in the main project as this is a legacy app and I was running into include / linker hell trying to do it in a separate test project. If I have to do that, I will, but Microsoft says they support keeping the tests in the exe's project.

I don't really care about using MSTest vs Google Test, I just want something that works so I can get to actual test writing.

Help!

Aucun commentaire:

Enregistrer un commentaire