简体   繁体   English

我可以使用IntelliTest生成明智的单元测试吗?

[英]Can I use IntelliTest to generate sensible unit-tests?

The Microsoft documentation for IntelliTest says: Microsoft IntelliTest的文档说:

IntelliTest explores your .NET code to generate test data and a suite of unit tests. IntelliTest探索您的.NET代码以生成测试数据和一组单元测试。 For every statement in the code, a test input is generated that will execute that statement. 对于代码中的每个语句,都会生成一个将执行该语句的测试输入。 A case analysis is performed for every conditional branch in the code. 对代码中的每个条件分支都进行案例分析。 For example, if statements, assertions, and all operations that can throw exceptions are analyzed. 例如,分析了if语句,断言以及所有可能引发异常的操作。 This analysis is used to generate test data for a parameterized unit test for each of your methods, creating unit tests with high code coverage. 该分析用于为每种方法为参数化的单元测试生成测试数据,从而创建具有较高代码覆盖率的单元测试。

I'm using Visual Studio 2017. I right-clicked inside my method, selected "IntelliTest" and then "Create IntelliTest". 我使用的是Visual Studio2017。我在方法内部单击鼠标右键,选择“ IntelliTest”,然后选择“创建IntelliTest”。 I accepted all the defaults on the pop-up dialogue box and clicked "OK". 我在弹出对话框中接受了所有默认设置,然后单击“确定”。

    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>;
    }

Why then, does my method with many, many (too many) if s just generate this unit-test code? 为什么然后, 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()
  }
}

I expected to get at least one test for each if , to cover the code (almost) completely. 我希望每个if至少获得一个测试,以(几乎)完全覆盖代码。 Did I do something wrong? 我做错什么了吗? Is there a way to generate default tests the way Microsoft claims? 有没有办法像Microsoft所说的那样生成默认测试?

EDIT 编辑

I've now Run IntelliTest too, and it has generated the following code: 我现在也运行了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);
}
  }

The test fails because an Exception is thrown, even though the test expects an Exception to be thrown. 测试失败是因为抛出了异常,即使测试期望抛出异常也是如此。

The [PexMethod] effectively just identifies the function you want to test. [PexMethod]有效地仅标识您要测试的功能。 You need to right click and "IntelliTest -> Run IntelliTest" on the PexMethod for the first time to generate a child .g.cs file that contains tests for all paths of execution. 您需要右键单击并在PexMethod上首次单击“ IntelliTest-> Run IntelliTest”,以生成包含所有执行路径测试的子.g.cs文件。

You "Run IntelliTest" again only when you make changes to the code that changes execution paths or alters the behavior; 仅当您更改更改执行路径或更改行为的代码时,才再次“运行IntelliTest”。 if you're just refactoring code, you don't need to generate new test code, you just run the generated [TestMethod] tests in the g.cs file as normal. 如果您只是重构代码,则无需生成新的测试代码,只需正常运行g.cs文件中生成的[TestMethod]测试g.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM