Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vstest.console.exe generate cover for only Moq.dll

I am trying to generate code coverage report using vstest.console.exe. I am also using .runsettings file and passing it as a parameter.

Whatever I am trying to do, it generates a coverage report for only moq.dll.

I am sharing below the full text of command parameters I am running and also content of .runsettings file. Any idea, where am I doing something wrong?

Command:

vstest.console.exe "C:\Xyz.Tests\bin\Debug\netcoreapp2.0\Xyz.Tests.dll" /InIsolation /EnableCodeCoverage /settings:CodeCoverage.runsettings

CodeCoverage.runsettings file content:

<RunSettings>
<DataCollectionRunSettings>
  <DataCollectors>
    <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" enabled="false">
      <Configuration>
        <CodeCoverage>
        </CodeCoverage>
      </Configuration>
    </DataCollector>
  </DataCollectors>
</DataCollectionRunSettings>
</RunSettings>

Image of generated code coverage report: enter image description here

like image 778
Nazim Avatar asked Feb 06 '18 12:02

Nazim


People also ask

How do I download VSTest console exe?

Download the nuget package: Microsoft. TestPlatform, rename it a zip file. Open the zip file, you will find all you need from this folder: . \tools\net451\Common7\IDE\Extensions\TestPlatform , including vstest.

What is VSTest console exe?

VSTest. Console.exe is the command-line tool to run tests. You can specify several options in any order on the command line. These options are listed in General command-line options. The MSTest adapter in Visual Studio also works in legacy mode (equivalent to running tests with mstest.exe) for compatibility.

How do I generate code coverage in Runsettings?

Code coverage formats Different formats may be useful across different editors and pipelines. You can enable this in runsettings by adding <Format>Cobertura</Format> or <Format>Xml</Format> in the DataCollector configuration section in your runsettings file.

How do I enable analyze code coverage in Visual Studio 2022?

Starting in Visual Studio 2022 Update 2, you can enable faster code coverage test results by selecting Tools > Options > Environment > Preview Features, then selecting Code coverage experience improvements, and then restarting Visual Studio.


1 Answers

I faced the same behavior, but fortunately I found out a solution:

Open Visual Studio Test task and:

  • Uncheck Code coverage enabled flag
  • Put --collect:"Code Coverage" in Other console options
  • Edit .csproj file of project, containing the tested classes and:

    Add <DebugType>full</DebugType> in <PropertyGroup> section

    To avoid moq.dll in Code Coverage results:

    Add <ModulePath>.*moq.dll</ModulePath> in <ModulePaths> -> <Exclude> section of .runsettings file

    Here is my .runsettings

    <?xml version="1.0" encoding="utf-8"?>
    <RunSettings>
      <RunConfiguration>
        <MaxCpuCount>0</MaxCpuCount>
      </RunConfiguration>
      <DataCollectionRunSettings>
        <DataCollectors>
          <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
            <Configuration>
              <CodeCoverage>
                <!-- Match assembly file paths: -->
                <ModulePaths>
                  <Include>
                    <ModulePath>.*\.dll$</ModulePath>
                    <ModulePath>.*\.exe$</ModulePath>
                  </Include>
                  <Exclude>
                    <ModulePath>.*moq.dll</ModulePath>
                    <ModulePath>.*CPPUnitTestFramework.*</ModulePath>
                    <ModulePath>.*TestAdapter.*</ModulePath>
                  </Exclude>
                </ModulePaths>
              </CodeCoverage>
            </Configuration>
          </DataCollector>
        </DataCollectors>
      </DataCollectionRunSettings>
    </RunSettings>
    

    And please, check out https://developercommunity.visualstudio.com/content/problem/92905/net-core-unit-testing-code-coverage.html link

    like image 186
    Alexandr Avatar answered Oct 04 '22 05:10

    Alexandr