我可以使用IntelliTest生成合理的单元测试吗?

时间:2017-11-27 23:39:56

标签: c# visual-studio unit-testing visual-studio-2017 intellitest

IntelliTest的Microsoft documentation说:

  

IntelliTest探索您的.NET代码以生成测试数据和一套单元测试。对于代码中的每个语句,都会生成一个将执行该语句的测试输入。对代码中的每个条件分支执行案例分析。例如,分析if语句,断言和可以抛出异常的所有操作。此分析用于为每个方法生成参数化单元测试的测试数据,从而创建具有高代码覆盖率的单元测试。

我正在使用Visual Studio 2017.我在我的方法中右键单击,选择“IntelliTest”,然后选择“Create IntelliTest”。我接受了弹出对话框中的所有默认值,然后单击“确定”。

    public static int ScanInternalInbox()
    {
        if (<code removed>)
        {
            <code removed>;
        }

        <code removed>;

        try
        {
            <code removed>;
        }
        catch (Exception e)
        {
            <code removed>;
            return 0;
        }

        <code removed>;

        try
        {
            <code removed>;
        }
        catch (Exception e)
        {
            <code removed>;
        }

        <code removed>;
        foreach (<code removed>)
        {
            <code removed>;

            if (<code removed>)
            {
                if (<code removed>)
                {
                    <code removed>;

                    if (<code removed>)
                    {
                        foreach (<code removed>)
                        {
                            <code removed>;
                            if (<code removed>)
                            {
                                if (<code removed>)
                                {
                                    <code removed>;
                                }
                            }
                        }

                        if (<code removed>)
                        {
                            if (<code removed>)
                            {
                                <code removed>;
                            }
                            else
                            {
                                <code removed>;
                            }
                        }
                    }
                    else
                    {
                        <code removed>;
                    }
                }
                else
                {
                    <code removed>;
                }                    
            }
            else
            {
                <code removed>;
            }
        }

        <code removed>;
    }

为什么然后,我的方法与许多(太多)if生成此单元测试代码?

public partial class HMR_AgentTest {

  /// <summary>Test stub for ScanInternalInbox()</summary>
  [PexMethod]
  public int ScanInternalInboxTest() {
    int result = global::HMR_Agent.HMR_Agent.ScanInternalInbox();
    return result;
    // TODO: add assertions to method HMR_AgentTest.ScanInternalInboxTest()
  }
}

我希望每个if至少进行一次测试,以完全覆盖代码(几乎)。我做错什么了吗?有没有办法像微软声称的那样生成默认测试?

修改

我现在也运行IntelliTest,它生成了以下代码:

  public partial class HMR_AgentTest {

[TestMethod]
[PexGeneratedBy(typeof(HMR_AgentTest))]
[PexRaisedException(typeof(TypeInitializationException))]
public void ScanInternalInboxTestThrowsTypeInitializationException908()
{
    int i;
    i = this.ScanInternalInboxTest();
    Assert.AreEqual<int>(-1, i);
}
  }

测试失败,因为抛出异常,即使测试期望抛出异常。

1 个答案:

答案 0 :(得分:0)

[PexMethod]有效地识别您要测试的功能。您需要右键单击并且&#34; IntelliTest - &gt;运行IntelliTest&#34;在PexMethod上第一次生成包含所有执行路径测试的子.g.cs文件。

你&#34;运行IntelliTest&#34;仅当您更改执行路径或更改行为的代码时,才会再次执行此操作;如果您只是重构代码,则不需要生成新的测试代码,只需正常运行[TestMethod]文件中生成的g.cs测试。