将待测 PEX 代码限制为单一方法?

发布于 2024-12-05 18:15:47 字数 1249 浏览 0 评论 0原文

PEX 中的待测代码能否限制为单一方法?我知道您可以将其限制为程序集和类,但是成员呢?

我本质上是试图实现以下方法的 100% 代码覆盖率:

public virtual bool GetLastSymbol(string symbolHint, out string symbol)
{
    if (symbolHint == null)
    {
        throw new ArgumentNullException("symbolHint");
    }

    IEnumerable<string> symbols;
    symbol = this.VariableHints.TryGetValue(symbolHint, out symbols) ? symbols.Last() : null;

    return symbol != null;
}

以下 PUT 实现 12/15 代码覆盖率,因为我只测试它可以返回的可能的 2 个值中的 1 个

found = symbolManager.GetLastSymbol(symbolHint, out symbol);
PexAssert.IsFalse(found);

:要实现此 PUT 的完全覆盖,我需要更改对象的状态,以便该方法命中两个分支。我可以通过使用工厂方法使用单独的 PUT 来设置不同的状态来满足此要求,但这会给我留下 2 个代码覆盖不完整的 PUT,而不是 1 个完全覆盖的 PUT。我意识到理论上 2 个 PUT 的综合覆盖率将达到 100%,但我在实践中需要 100% 的数字,以便我可以正确设置 CI。

因此,要到达同一个 PUT 中的另一个分支,我必须将以下代码附加到上述 2 行:

symbolManager.CreateSymbol(symbolHint); // Ensure next call returns true.
found = symbolManager.GetLastSymbol(symbolHint, out symbol);
PexAssert.IsTrue(found);

大概 GetLastSymbol 方法的代码覆盖率现在是 100%,但是因为我已经引入了对被测类型的另一个方法调用,代码覆盖率现在下降到20/29

如何限制 PUT 仅测量单个方法的代码覆盖率?我意识到我可能完全误解了这个指标,所以如果是这种情况请解释原因:)

Can the code-under-test in PEX be constrained to a single method? I am aware you can constrain it to assemblies and classes, but what about members?

I am essentially trying to achieve 100% code coverage for the following method:

public virtual bool GetLastSymbol(string symbolHint, out string symbol)
{
    if (symbolHint == null)
    {
        throw new ArgumentNullException("symbolHint");
    }

    IEnumerable<string> symbols;
    symbol = this.VariableHints.TryGetValue(symbolHint, out symbols) ? symbols.Last() : null;

    return symbol != null;
}

The following PUT achieves 12/15 code coverage, because I’m only testing for 1 of the possible 2 values it can return:

found = symbolManager.GetLastSymbol(symbolHint, out symbol);
PexAssert.IsFalse(found);

To achieve full coverage for this PUT, I need to change the object’s state so that the method hits both branches. I could satisfy this by using separate PUTs using a factory method to setup the different states, but this would leave me with 2 PUTs with incomplete code coverage, rather than 1 PUT with full coverage. I realise in theory the 2 PUTs would have a combined coverage of 100%, but I need that 100% figure in practice so I can setup CI properly.

So, to reach the other branch in the same PUT, I must append the following code to the above 2 lines:

symbolManager.CreateSymbol(symbolHint); // Ensure next call returns true.
found = symbolManager.GetLastSymbol(symbolHint, out symbol);
PexAssert.IsTrue(found);

Presumably the code coverage for the GetLastSymbol method is now 100%, but because I’ve introduced another method call to the type under test, the code coverage now drops to 20/29.

How can I constrain a PUT to only measure code coverage for a single method? I realise I may have misunderstood this metric entirely, so please explain why if this is the case :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-12-12 18:15:47

随 PEX 一起安装的“PEX API 参考”对于解决此问题非常有用:

Microsoft.Pex.Framework.Coverage 命名空间包含多个过滤器属性,这些属性可以排除影响代码覆盖率的各种方面。我想要的是:

PexCoverageFilterMethodAttribute

使用此方法,我能够从覆盖率报告中删除 CreateSymbol 方法:

[PexCoverageFilterMethod(PexCoverageDomain.UserOrTestCode, "CreateVariable")]

这现在将我的代码覆盖率增加到13/15;比以前高了,但仍然没有达到我预期的 100%。长话短说,我发现 PEX 也在覆盖率报告中包含了构造函数...

[PexCoverageFilterMethod(PexCoverageDomain.UserOrTestCode, "ctor")]

我的代码覆盖率现在收到 13/13 。我是一只快乐的小兔子:)

The 'PEX API Reference' which comes installed with PEX was useful for solving this:

The Microsoft.Pex.Framework.Coverage namespace includes several filter attributes which can exclude various facets from effecting code coverage. The one I wanted was:

PexCoverageFilterMethodAttribute

Using this method I was able to remove the CreateSymbol method from the coverage report:

[PexCoverageFilterMethod(PexCoverageDomain.UserOrTestCode, "CreateVariable")]

This now increased my code coverage to 13/15; higher than before, but still not the 100% I was expecting. Long story short, I discovered PEX was including the constructor in the coverage reports too...

[PexCoverageFilterMethod(PexCoverageDomain.UserOrTestCode, "ctor")]

I'm now receiving 13/13 for my code coverage. I'm a happy bunny :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文