question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

0% coverage of library when using source generator

See original GitHub issue

Hello,

I have a library targeted .Net Standard 2.0 and a test project using NUnit and targeted .Net 5.0. I used coverlet to calculate code-coverage as described in https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows.

Recently I started to use ISourceGenerator to generate some files during build. After that I noticed that code coverage emits coverage.cobertura.xml file with the following content :

<?xml version="1.0" encoding="utf-8"?>
<coverage line-rate="0" branch-rate="0" version="1.9" timestamp="1638209313" lines-covered="0" lines-valid="0" branches-covered="0" branches-valid="0">
  <sources>
    <source></source>
  </sources>
  <packages />

I played much with coverlet.collector v3.1.0 and coverlet.msbuild v3.1.0 but always got an empty report (0 lines being covered). Here is an output of command :

# dotnet test --configuration Debug /p:CollectCoverage=true --diag:log.txt --settings runsettings.xml
  Determining projects to restore...
  All projects are up-to-date for restore.
  Assure.EnumsGenerator -> ../Assure.EnumsGenerator/bin/Debug/netstandard2.0/Assure.EnumsGenerator.dll
  Assure -> ../dotnet/Assure/bin/Debug/netstandard2.0/Assure.dll
  Assure.Tests -> ../Assure.Tests/bin/Debug/net5.0/Assure.Tests.dll
Test run for ../Assure.Tests/bin/Debug/net5.0/Assure.Tests.dll (.NETCoreApp,Version=v5.0)
Microsoft (R) Test Execution Command Line Tool Version 16.11.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Logging Vstest Diagnostics in file: ../log.txt
A total of 1 test files matched the specified pattern.

Attachments:
  ../Assure.Tests/TestResults/777aab11-c4d4-45b7-9acf-639bc68fa0bd/coverage.info
  ../Assure.Tests/TestResults/777aab11-c4d4-45b7-9acf-639bc68fa0bd/coverage.cobertura.xml
Passed!  - Failed:     0, Passed:  2029, Skipped:     0, Total:  2029, Duration: 764 ms - ../Assure.Tests/bin/Debug/net5.0/Assure.Tests.dll (net5.0)

Calculating coverage result...
  Generating report ../Assure.Tests/coverage.json'

+--------+------+--------+--------+
| Module | Line | Branch | Method |
+--------+------+--------+--------+

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

#

File log.datacollector.21-11-29_18-08-30_12299_4.txt keeps a message :

TpTrace Verbose: 0 : 21376, 1, 2021/11/29, 18:08:30.641, 90805477623404, datacollector.dll, [coverlet]Unable to instrument module: ../Assure.Tests/bin/Debug/net5.0/Assure.dll, pdb without local source files, [.../Assure/Assure.EnumsGenerator/Assure.EnumsGenerator.EnumsGenerator/AccessRight.cs]
TpTrace Verbose: 0 : 21376, 1, 2021/11/29, 18:08:30.695, 90805531432922, datacollector.dll, [coverlet]Unable to instrument module: .../Assure.Tests/bin/Debug/net5.0/Moq.dll, embedded pdb without local source files, [C:\projects\moq4\src\Moq\obj\TypeNameFormatter.cs]
TpTrace Information: 0 : 21376, 1, 2021/11/29, 18:08:30.734, 90805570278990, datacollector.dll, DataCollectionRequestHandler.ProcessRequests : DataCollection started.
TpTrace Information: 0 : 21376, 1, 2021/11/29, 18:08:30.765, 90805601531262, datacollector.dll, DataCollectionRequestHandler.ProcessRequests : Datacollector received message: (DataCollection.TestHostLaunched) -> {
  "ProcessId": 21394
}
TpTrace Information: 0 : 21376, 1, 2021/11/29, 18:08:33.808, 90808644847167, datacollector.dll, DataCollectionRequestHandler.ProcessRequests : Datacollector received message: (DataCollection.AfterTestRunEnd) -> false
TpTrace Verbose: 0 : 21376, 1, 2021/11/29, 18:08:33.809, 90808645919498, datacollector.dll, [coverlet]CoverletCoverageDataCollector: SessionEnd received

In log.host.21-11-29_18-08-30_74105_4.txt I noticed a bunch of messages like :

TpTrace Warning: 0 : 21394, 13, 2021/11/29, 18:08:31.922, 90806758548843, testhost.dll, TestRunCache: No test found corresponding to testResult 'CreateSubclasses(Assure.Common.ServerAlreadyActivatedError: [AlreadyExists] : ,AlreadyExists)' in inProgress list.

Could somebody please assist to understand what is wrong ? Also, coverlet works fine when I remove compile-time code-generation.

PS : runsettings.xml :

<?xml version="1.0" encoding="utf-8"?>

<RunSettings>
	<DataCollectionRunSettings>
		<DataCollectors>
			<DataCollector friendlyName="XPlat code coverage">
				<Configuration>
					<Format>lcov,cobertura</Format>
					<UseSourceLink>true</UseSourceLink>
					<Include>Assure.Client*</Include>
				</Configuration>
			</DataCollector>
		</DataCollectors>
	</DataCollectionRunSettings>
</RunSettings> 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
dimarusyycommented, Nov 30, 2021

Thanks for help! Changing filename to .g.cs fixed the issue for me too.

2reactions
Andreas-Hubercommented, Nov 30, 2021

Ah sorry, I was already ahead and pushed a new commit after Peters comment with the .g.cs suffix - it was not there before. So that seems to fix it in the playground project. I was just about to test that in the production project because I thought adding the .g.cs suffix was one of the things I tried already.

So in this case adding .g.cs definitely fixed it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source Generators
A Source Generator is a .NET Standard 2.0 assembly that is loaded by the compiler along with any analyzers. It's usable in environments...
Read more >
Use code coverage for unit testing - .NET
This article discusses the usage of code coverage for unit testing with Coverlet and report generation using ReportGenerator. While this article ...
Read more >
Building a Source Generator for C# - InfoQ
For this walk-through, we'll be looking at the source code for Tortuga Test Monkey. It is available on Github under under the MIT...
Read more >
cesil
My own benchmarking suggests about 10%, in libraries like Cesil. ... As of 0.8.0, Cesil now has accompanying Source Generators (available as Cesil....
Read more >
Webinar: Source Generators with Stefan Pölz
Stefan Pölz explains source generators: how they are currently used in .NET, how you can build your own, test them, and make them...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found