coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.93k stars 385 forks source link

0% code coverage and error in collector #1661

Open tiwarishubham635 opened 1 month ago

tiwarishubham635 commented 1 month ago

We have a C# project - https://github.com/twilio/twilio-csharp. We were earlier calculating coverage using these steps:

dotnet sonarscanner begin /k:"$(PROJECT_NAME)" /o:"twilio" /d:sonar.host.url=https://sonarcloud.io /d:sonar.token="${SONAR_TOKEN}"  /d:sonar.language="cs" $(SONAR_SOURCES) /d:sonar.cs.opencover.reportsPaths="test/lcov.opencover.xml" /d:"sonar.verbose=true"
# Write to a log file since the logs for build with sonar analyzer are pretty beefy and travis has a limit on the number of log lines
dotnet build Twilio.sln > buildsonar.log
dotnet test test/Twilio.Test/Twilio.Test.csproj --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=../lcov
dotnet sonarscanner end /d:sonar.token="${SONAR_TOKEN}"

And were using msbuild for coverage in test.csproj like this

This was giving 0% code coverage like this:

Calculating coverage result...
   Generating report '/home/runner/work/twilio-csharp/twilio-csharp/test/lcov.opencover.xml'

+--------+------+--------+--------+
| Module | Line | Branch | Method |
+--------+------+--------+--------+
| Twilio | 0%   | 0%     | 0%     |
+--------+------+--------+--------+

+---------+------+--------+--------+
|         | Line | Branch | Method |
+---------+------+--------+--------+
| Total   | 0%   | 0%     | 0%     |
+---------+------+--------+--------+
| Average | 0%   | 0%     | 0%     |
+---------+------+--------+--------+

Then i came across this known issue and shifted to use coverlet.collectors. I am using it like this:

dotnet sonarscanner begin /k:"$(PROJECT_NAME)" /o:"twilio" /d:sonar.host.url=https://sonarcloud.io /d:sonar.login="${SONAR_TOKEN}"  /d:sonar.language="cs" $(SONAR_SOURCES) /d:sonar.cs.opencover.reportsPaths="test/lcov.opencover.xml"
# Write to a log file since the logs for build with sonar analyzer are pretty beefy and travis has a limit on the number of log lines
dotnet build Twilio.sln > buildsonar.log
dotnet test test/Twilio.Test/Twilio.Test.csproj --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput=../lcov --collect:"XPlat Code Coverage"
dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"

My Test.csproj file looks like this now.

Now I am stuck with this error:

Error: /home/runner/work/twilio-csharp/twilio-csharp/test/Twilio.Test/Program.cs(9,20): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. [/home/runner/work/twilio-csharp/twilio-csharp/test/Twilio.Test/Twilio.Test.csproj]

Here are the complete logs, if you can see.

I would like to know why is this failing and is there any fix available for msbuild as well? Thanks!