0

次の2つの(無関係な)メソッドをテストし、OpenCover2.0.802.1を使用して完全なブランチとステートメントのカバレッジを実現したいと思います。

public class Methods
{
    public static void MethodWithDelegate(SynchronizationContext context)
    {
        context.Send(delegate { Console.Beep(); }, null);
    }

    public static string MethodWithSwitchStatement(Type value)
    {
        string output = string.Empty;

        if (value != null)
        {
            switch (value.ToString())
            {
                case "System.Int32":
                    output = "int";
                    break;
                default:
                    output = "other type";
                    break;
            }
        }

        return output;
    }
}

次の(NUnit)テストを作成しました。1つは「Moq」モックオブジェクトを使用しています。

[Test]
public void Test_ShouldCoverMethodWithDelegate()
{
    var context = new Mock<SynchronizationContext>();

    Methods.MethodWithDelegate(context.Object);

    context.Verify(x => x.Send(It.IsAny<SendOrPostCallback>(), It.IsAny<object>()));
}

[Test]
public void Test_ShouldCoverSwitchStatement()
{
    Assert.That(Methods.MethodWithSwitchStatement(null), Is.EqualTo(string.Empty));
    Assert.That(Methods.MethodWithSwitchStatement(typeof(int)), Is.EqualTo("int"));
    Assert.That(Methods.MethodWithSwitchStatement(typeof(float)), Is.EqualTo("other type"));
}

ただし、OpenCoverを介してテストを実行した後、coverage.xmlファイルには常に、両方のテストの訪問数がゼロの分岐点が含まれます。シーケンスカバレッジは100%を示しています。

ILの専門家ではないので、ブランチカバレッジを100%にするために、さらにテストを作成する方法がわかりません。

4

1 に答える 1

3

さて、最初にILの最初の方法を見てみましょう(私はIL SPYを使用しています)

.method public hidebysig static 
void MethodWithDelegate (
    class [mscorlib]System.Threading.SynchronizationContext context
) cil managed 
{
// Method begins at RVA 0x2059
// Code size 41 (0x29)
.maxstack 8

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldsfld class [mscorlib]System.Threading.SendOrPostCallback so8254847.Methods::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0007: brtrue.s IL_001c

IL_0009: ldnull
IL_000a: ldftn void so8254847.Methods::'<MethodWithDelegate>b__0'(object)
IL_0010: newobj instance void [mscorlib]System.Threading.SendOrPostCallback::.ctor(object, native int)
IL_0015: stsfld class [mscorlib]System.Threading.SendOrPostCallback so8254847.Methods::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_001a: br.s IL_001c

IL_001c: ldsfld class [mscorlib]System.Threading.SendOrPostCallback so8254847.Methods::'CS$<>9__CachedAnonymousMethodDelegate1'
IL_0021: ldnull
IL_0022: callvirt instance void [mscorlib]System.Threading.SynchronizationContext::Send(class [mscorlib]System.Threading.SendOrPostCallback, object)
IL_0027: nop
IL_0028: ret
} // end of method Methods::MethodWithDelegate

ご覧のとおり、IL_0007には条件付きブランチがあり、キャッシュされた匿名デリゲートが設定されている場合にのみ実行されます。それ以外の場合は、デリゲートを設定してから呼び出すメインコードを実行します。

解決策:テストを2回実行するか、.NETの最適化のビットであるため忘れてください

今回の2番目の問題では、実際にC#で生成されたものを確認することをお勧めします。switchステートメントを記述しましたが、コンパイラーは代わりにifsを使用しました。

public static string MethodWithSwitchStatement(Type value)
{
    string output = string.Empty;
    if (value != null)
    {
        string a;
        if ((a = value.ToString()) != null && a == "System.Int32")
        {
            output = "int";
        }
        else
        {
            output = "other type";
        }
    }
    return output;
}

ご覧のとおり、コンパイラはswitchステートメントを使用してif nullテストを導入していますが、すでにこれを持っているため、実行されることはありません。

解決策:nullテストの場合は最初のテストを削除します(不要なため)。

于 2011-11-25T04:22:13.130 に答える