locked
Code Coverage in VS 2017 Using Runsettings RRS feed

  • Question

  • Hey all.

    Don't know if this is possible or not but I am unable to get a runsettings file to work with a .NET Core 2.0 unit test project when trying to collect code coverage. 

    I am calling vstest.console.exe (VS 2017 version) from the command line and passing it the path to a runsettings file.

    The runsettings file has a TargetFrameworkVersion element with suggested values of Framework35 | Framework40 | Framework45.  However, the test binary is netcoreapp2.0. 

    vstest.console.exe is throwing the following warning: Test run will use DLL(s) built for framework .NETFramework,Version=v4.5 and platform X86. Following DLL(s) do not match framework/platform settings.  Test.UnitTests.dll is built for Framework 2.0 and Platform X86.

    What is the correct value for the TargetFrameworkVersion element so that I can execute the test and gather code coverage?  Is it even possible with a runsettings file?



    -7

    Monday, February 12, 2018 7:21 PM

Answers

  • Hi smyrin,

    Welcome to the MSDN forum.

    I can repro your issue in my side.

    Please use <supportedRuntime> instead of <TargetFrameworkVersion> in your runsettings file.

    This is my runsettings file example:

    <?xml version="1.0" encoding="utf-8"?>  
    <RunSettings>  
      <!-- Configurations that affect the Test Framework -->  
      <RunConfiguration>  
        <MaxCpuCount>1</MaxCpuCount>  
        <!-- Path relative to solution directory -->  
        <ResultsDirectory>.\TestResults</ResultsDirectory>  
    
        <!-- [x86] | x64    
          - You can also change it from menu Test, Test Settings, Default Processor Architecture -->  
        <TargetPlatform>x86</TargetPlatform>  
    
        <!-- Framework35 | [Framework40] | Framework45 -->  
        <!--<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>--> 
        <startup>
          <supportedRuntime version="v2.0.50727"/>
        </startup>
    
        <!-- Path to Test Adapters -->  
        <TestAdaptersPaths>%SystemDrive%\Temp\foo;%SystemDrive%\Temp\bar</TestAdaptersPaths>  
      </RunConfiguration>  
    
      <!-- Configurations for data collectors -->  
      <DataCollectionRunSettings>  
        <DataCollectors>  
          <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">  
            <Configuration>  
              <CodeCoverage>  
                <ModulePaths>  
                  <Exclude>  
                    <ModulePath>.*CPPUnitTestFramework.*</ModulePath>  
                  </Exclude>  
                </ModulePaths>  
    
                <!-- We recommend you do not change the following values: -->  
                <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>  
                <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>  
                <CollectFromChildProcesses>True</CollectFromChildProcesses>  
                <CollectAspDotNet>False</CollectAspDotNet>  
    
              </CodeCoverage>  
            </Configuration>  
          </DataCollector>  
    
        </DataCollectors>  
      </DataCollectionRunSettings>  
    
      <!-- Parameters used by tests at runtime -->  
      <TestRunParameters>  
        <Parameter name="webAppUrl" value="http://localhost" />  
        <Parameter name="webAppUserName" value="Admin" />  
        <Parameter name="webAppPassword" value="Password" />  
      </TestRunParameters>  
    
      <!-- Adapter Specific sections -->  
    
      <!-- MSTest adapter -->  
      <MSTest>  
        <MapInconclusiveToFailed>True</MapInconclusiveToFailed>  
        <CaptureTraceOutput>false</CaptureTraceOutput>  
        <DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>  
        <DeploymentEnabled>False</DeploymentEnabled>  
        <AssemblyResolution>  
          <Directory Path="D:\myfolder\bin\" includeSubDirectories="false"/>  
        </AssemblyResolution>  
      </MSTest>  
    
    </RunSettings>

    After change the runsettings file, please use following command test again:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow>vstest.console.exe "C:\Users\Admin\source\repos\NETUnitTest\NETUnitTest\bin\Debug\netcoreapp2.0\NETUnitTest.dll" /Settings:"C:\Users\Admin\source\repos\NETUnitTest\test.runsettings" /Platform:x86

    This document describe “How to Configure an App to Support .NET Framework 4 or 4.5”, please have a look at it: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-configure-an-app-to-support-net-framework-4-or-4-5

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by smyrin Thursday, February 15, 2018 12:29 AM
    Tuesday, February 13, 2018 3:22 AM
  • Thanks.  I will have a look at that solution.

    However, I did discover another answer by some trial and error.  You can set the TargetFrameworkVersion element to FrameworkCore10 and it will work:

    <!-- Framework35 | [Framework40] | Framework45 -->
    <TargetFrameworkVersion>FrameworkCore10</TargetFrameworkVersion>

    This is the solution I am currently using.


    -7

    • Proposed as answer by Judy ZhuY Wednesday, February 14, 2018 1:49 AM
    • Marked as answer by smyrin Thursday, February 15, 2018 12:29 AM
    Tuesday, February 13, 2018 4:16 PM

All replies

  • Hi smyrin,

    Welcome to the MSDN forum.

    I can repro your issue in my side.

    Please use <supportedRuntime> instead of <TargetFrameworkVersion> in your runsettings file.

    This is my runsettings file example:

    <?xml version="1.0" encoding="utf-8"?>  
    <RunSettings>  
      <!-- Configurations that affect the Test Framework -->  
      <RunConfiguration>  
        <MaxCpuCount>1</MaxCpuCount>  
        <!-- Path relative to solution directory -->  
        <ResultsDirectory>.\TestResults</ResultsDirectory>  
    
        <!-- [x86] | x64    
          - You can also change it from menu Test, Test Settings, Default Processor Architecture -->  
        <TargetPlatform>x86</TargetPlatform>  
    
        <!-- Framework35 | [Framework40] | Framework45 -->  
        <!--<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>--> 
        <startup>
          <supportedRuntime version="v2.0.50727"/>
        </startup>
    
        <!-- Path to Test Adapters -->  
        <TestAdaptersPaths>%SystemDrive%\Temp\foo;%SystemDrive%\Temp\bar</TestAdaptersPaths>  
      </RunConfiguration>  
    
      <!-- Configurations for data collectors -->  
      <DataCollectionRunSettings>  
        <DataCollectors>  
          <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">  
            <Configuration>  
              <CodeCoverage>  
                <ModulePaths>  
                  <Exclude>  
                    <ModulePath>.*CPPUnitTestFramework.*</ModulePath>  
                  </Exclude>  
                </ModulePaths>  
    
                <!-- We recommend you do not change the following values: -->  
                <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>  
                <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>  
                <CollectFromChildProcesses>True</CollectFromChildProcesses>  
                <CollectAspDotNet>False</CollectAspDotNet>  
    
              </CodeCoverage>  
            </Configuration>  
          </DataCollector>  
    
        </DataCollectors>  
      </DataCollectionRunSettings>  
    
      <!-- Parameters used by tests at runtime -->  
      <TestRunParameters>  
        <Parameter name="webAppUrl" value="http://localhost" />  
        <Parameter name="webAppUserName" value="Admin" />  
        <Parameter name="webAppPassword" value="Password" />  
      </TestRunParameters>  
    
      <!-- Adapter Specific sections -->  
    
      <!-- MSTest adapter -->  
      <MSTest>  
        <MapInconclusiveToFailed>True</MapInconclusiveToFailed>  
        <CaptureTraceOutput>false</CaptureTraceOutput>  
        <DeleteDeploymentDirectoryAfterTestRunIsComplete>False</DeleteDeploymentDirectoryAfterTestRunIsComplete>  
        <DeploymentEnabled>False</DeploymentEnabled>  
        <AssemblyResolution>  
          <Directory Path="D:\myfolder\bin\" includeSubDirectories="false"/>  
        </AssemblyResolution>  
      </MSTest>  
    
    </RunSettings>

    After change the runsettings file, please use following command test again:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow>vstest.console.exe "C:\Users\Admin\source\repos\NETUnitTest\NETUnitTest\bin\Debug\netcoreapp2.0\NETUnitTest.dll" /Settings:"C:\Users\Admin\source\repos\NETUnitTest\test.runsettings" /Platform:x86

    This document describe “How to Configure an App to Support .NET Framework 4 or 4.5”, please have a look at it: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-configure-an-app-to-support-net-framework-4-or-4-5

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by smyrin Thursday, February 15, 2018 12:29 AM
    Tuesday, February 13, 2018 3:22 AM
  • Thanks.  I will have a look at that solution.

    However, I did discover another answer by some trial and error.  You can set the TargetFrameworkVersion element to FrameworkCore10 and it will work:

    <!-- Framework35 | [Framework40] | Framework45 -->
    <TargetFrameworkVersion>FrameworkCore10</TargetFrameworkVersion>

    This is the solution I am currently using.


    -7

    • Proposed as answer by Judy ZhuY Wednesday, February 14, 2018 1:49 AM
    • Marked as answer by smyrin Thursday, February 15, 2018 12:29 AM
    Tuesday, February 13, 2018 4:16 PM
  • Hi smyrin,

    Glad receive your reply.

    If your issue has been resolved, please mark your reply as an answer, it will help other community members who has the same issue find the solution faster.

    And, if my reply could helpful for you, please also make it as an answer.

    Thanks for your understanding.

    Regards,

    Judyzh


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 14, 2018 1:49 AM