How to use code coverage with C# generics

Just came across the code coverage package. Seems like a very useful tool, so I’ve been prototyping with it a bit. Ran into a fairly common case that seems like it should work, so just verifying if I’m doing something wrong, or it’s a known issue I haven’t seen documented.

What I’m trying to do is check the code coverage on a generic C# class or method. But the generic method or class always has 0 coverage. Here’s some screenshots of an example I tried.

Test:

[TestFixture, SingleThreaded]
public class Tests
{
    [Test]
    public void Test()
    {
        Code code = new Code();
        code.GetInt(5);
    }
}

Coverage:
8445281--1119758--coverage.png

Any one has gotten it working with generics and has advice?

I have noticed the same issue and also wondered about abstract classes. It seems that only concrete classes and implementations can be detected by the coverage tool. At first glance this kind of makes sense because that is the code that actually runs, whereas the generic definition or abstract class is only a like template for the code that is produces from it. However, it would of course be a great feature to support this, because quite a lot of code is affected.

Hi. There appears to be a bug in code coverage API. I’ve opened a bug to track this. You can find it here Unity Issue Tracker - Coverage.GetStatsFor API shows 0 hits for generic methods

2 Likes

Someone said it worked in version 1.1.1 and I’ve confirmed it indeed works in that version. I guess it should be marked as a regression then?

There are two parts to this issue. First one is that code coverage API is not handling generics well (issue tracker link posted previously). Coverage package was doing some work on top to handle generics and as a result it was working properly when using the package.

However, there was indeed a regression at some point and Code Coverage package stopped handling generics properly, too. Issue for the package side can be tracked here: https://issuetracker.unity3d.com/issues/coverage-package-fails-to-gather-any-coverage-for-generic-methods

Earlier today we have released Code Coverage package 1.2.3 which fixes the generics issue. Coverage data for generic methods should be correct once again.

2 Likes