Search Unity

Resolved How to use code coverage with C# generics

Discussion in 'Testing & Automation' started by aleung_eidosmontreal, Sep 16, 2022.

  1. aleung_eidosmontreal

    aleung_eidosmontreal

    Joined:
    Jul 9, 2020
    Posts:
    1
    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:
    Code (CSharp):
    1. [TestFixture, SingleThreaded]
    2. public class Tests
    3. {
    4.     [Test]
    5.     public void Test()
    6.     {
    7.         Code code = new Code();
    8.         code.GetInt(5);
    9.     }
    10. }
    Coverage:
    coverage.png

    Any one has gotten it working with generics and has advice?
     
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    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.
     
  3. JuliusJ

    JuliusJ

    QA Minion of the month Unity Technologies

    Joined:
    May 20, 2015
    Posts:
    35
    noy-g and Xarbrough like this.
  4. joaoborks

    joaoborks

    Joined:
    Jul 7, 2015
    Posts:
    29
    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?
     
  5. JuliusJ

    JuliusJ

    QA Minion of the month Unity Technologies

    Joined:
    May 20, 2015
    Posts:
    35
    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/is...ls-to-gather-any-coverage-for-generic-methods
     
  6. JuliusJ

    JuliusJ

    QA Minion of the month Unity Technologies

    Joined:
    May 20, 2015
    Posts:
    35
    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.
     
    stephyswe and joaoborks like this.