microsoft / codecoverage

MIT License
84 stars 11 forks source link

Warning "No code coverage data available. Profiler was not initialized." #129

Closed StefH closed 4 months ago

StefH commented 4 months ago

When running this command in a powershell script:

dotnet-coverage collect -s "codecoverage.xml" -f cobertura -o coverageFile.xml "dotnet test ./MyTest.csproj --no-build --configuration Release --results-directory TestResults --logger trx --collect `"XPlat Code Coverage;Format=cobertura`""

I get a warning: No code coverage data available. Profiler was not initialized. and indeed the coverageFile.xml is empty:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<coverage line-rate="1" branch-rate="1" complexity="1" version="1.9" timestamp="1721989043">
  <packages />
</coverage>

Note that both the project and the testproject are a net6.0 project with RuntimeIdentifier win-x86.

And using a net48 project + test project works fine.

fhnaseer commented 4 months ago

dotnet test ./MyTest.csproj --no-build --configuration Release --results-directory TestResults --logger trx --collect "XPlat Code Coverage;Format=cobertura"

You are running tests with coverlet coverage collector ("XPlat Code Coverage") and not Microsoft Code Coverage collector. Try running without specifying --collect to dotnet test.

StefH commented 4 months ago

This yields the same warning and the coverageFile.xml is empty.

fhnaseer commented 4 months ago

Can you run with logs and share these? dotnet-coverage collect "dotnet test test.dll" -l log.txt -ll Verbose

StefH commented 4 months ago

See attached log file. log.txt

fhnaseer commented 4 months ago

From the logs, your dll is being skipped. Do you have some additional include/exclude in the runsettings file? Is it also possible that your dll is skipped because it contains reserved names which are skipped automatically.

2024-07-26 15:25:32.104 +02:00 [INF] LoggerBase.LoadModuleData[0]: Loading module id:C:...\bin\Release\net6.0\win-x86....dll_0_0, path:C:*\bin\Release\net6.0\win-x86*.dll 2024-07-26 15:25:32.104 +02:00 [INF] ReportGenerator.LoadModule: name:C:....\bin\Release\net6.0\win-x86....dll, skipReason:path_is_excluded, detailed reason: 2024-07-26 15:25:32.104 +02:00 [INF] StaticStreamCreator.CreateStream: Creating SharedMemoryStream. streamId:84, identifier:28a32265-7278-49cb-93ab-ea2470161bd9, bufferName:CodeCoverage.16cd45d7-cdc5-4ae7-a576-da8a0189acc0.d28a32265-7278-49cb-93ab-ea2470161bd9, bufferSize:6 2024-07-26 15:25:32.104 +02:00 [VRB] LoggerBase.OnDynamicInstrumentationSkipped: path:C:....\bin\Release\net6.0\win-x86....dll, reason:path_is_excluded, isManaged:True

StefH commented 4 months ago

@fhnaseer Good catch ! Thank you.

The other .NET48 projects where an exe, so I used these settings in the config file:

<ModulePaths>
    <Include>
        <ModulePath>.*MyCompanyName.*\.exe$</ModulePath>
       </Include>
</ModulePaths>

But apparently, although an exe should be generated, for this .NET 6 project I need to include the dll.

After changing the settings file, it works correct.

fhnaseer commented 3 months ago

Include/Exclude are regular expressions. You can also use <ModulePath>.*MyCompany\.[^/\\]+\.(dll|exe)$</ModulePath> or only company name and ignore extension.