为什么我的代码覆盖率是零,即使单元测试都通过了?

Why my code coverage is zero percent even unit test all passed?

我只用一种方法创建了一个项目,并在上面编写了单元测试,单元测试在本地通过了。但不确定为什么在 运行 声纳云扫描仪之后,它显示覆盖率为零。

这是测试class

public class DataStructureTest
{
    private readonly DataStructure ds;

    public DataStructureTest()
    {
        ds = new DataStructure();
    }

    [Theory, MemberData(nameof(LongestString_Return_Longest_String_ShouldPass_Data))]
    public void LongestString_Return_Longest_String_ShouldPass(string input, string expect)
    {
        // Act
        var actual = ds.LongestString(input);

        // Assert
        Assert.Equal(expect, actual);
    }

    public static TheoryData<string, string> LongestString_Return_Longest_String_ShouldPass_Data()
    {
        return new TheoryData<string, string>
        {
            { "Hello John", "Hello" },
            { "Hi John and Mandy", "Mandy" }
        };
    }
}

您必须注意这些软件在使用某些术语时的含义。例如,SonarQube 有以下文章:https://community.sonarsource.com/t/sonarqube-and-code-coverage/4725

FAQ 第一个问题是:

Q: After migrating from 5.6 to 6.7 my coverage shows 0%, why is that ? R: Since SonarQube 6.2 and the implementation of the MMF-345 565, if no coverage information is found the coverage is then set to zero by default.

我想你的情况可能属于这种情况。