none
Weird IndexOutOfRangeException with TestMethod on Release configuration RRS feed

  • Question

  • Hi, all.

    Given these bits:

    (1) Microsoft Visual Studio Enterprise 2017
    Version 15.9.6
    VisualStudio.15.Release/15.9.6+28307.344

    (2) Microsoft .NET Framework
    Version 4.7.03056

    (3) <PackageReference Include="MSTest.TestAdapter">
          <Version>1.4.0</Version>
        </PackageReference>

    (4) <PackageReference Include="MSTest.TestFramework">
          <Version>1.4.0</Version>
        </PackageReference>

    (A) Why, on Release configuration, both following tests do not pass?

    (B) Which index the exception refers to?

    Thank you very much in advance for any help or clarification.

    namespace UnitTestProject1
    {
        class Message
        {
            public ulong Id;
            public DateTime Timestamp;
        }

        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void why_fail()
            {
                //Arrange
                var input = new[] { 0, 1, 2 };

                //Act
                var messages = new Message[input.Length];
                for (var k = 0; k < input.Length; ++k)
                {
                    messages[k] = new Message { Id = (ulong)input[k], Timestamp = DateTime.Now };
                }

                //Assert
                Assert.AreEqual(3, messages.Length);
                for (int k = 1; k < messages.Length; ++k)
                {
                    Assert.AreEqual(1UL, messages[k].Id - messages[k - 1].Id);
                    Assert.IsTrue(messages[k].Timestamp.Subtract(messages[k - 1].Timestamp).TotalMilliseconds >= 0D);
                }
            }

            [TestMethod]
            public void why_ok()
            {
                //Arrange
                var input = new[] { 1, 2, 3 };

                //Act
                var messages = new Message[input.Length];
                for (var k = 0; k < input.Length; ++k)
                {
                    messages[k] = new Message { Id = (ulong)input[k], Timestamp = DateTime.Now };
                }

                //Assert
                Assert.AreEqual(3, messages.Length);
                for (int k = 1; k < messages.Length; ++k)
                {
                    Assert.AreEqual(1UL, messages[k].Id - messages[k - 1].Id);
                    Assert.IsTrue(messages[k].Timestamp.Subtract(messages[k - 1].Timestamp).TotalMilliseconds >= 0D);
                }
            }
        }
    }

    Sunday, January 27, 2019 9:54 PM

All replies

  • Hi friend,

    Thanks for your posting here.

    I reproduce this issue in my machine(vs2017). As you said, it’s quite weird, I have used debug mode, try-catch to collect information, but it’s still hard to figure out how does it happen.

    As a result, I have post this issue to this forum so that community members who are interested in this will help you vote it. Also you can vote for it and add comments with more info there.

    After that, please be patient waiting for the response from the VS Product Team Engineers. If it gets more votes, the product team experts would watch it.

    And here is the link: https://developercommunity.visualstudio.com/content/problem/438072/test-unexpected-fail-with-indexoutofrange-in-unit.html.

    If there have any updates from there, I will also update the information  here. Thank you for your understanding and sorry for this inconveniences.

    Best regards

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 28, 2019 1:02 PM
  • Hi MarcoDorantes

    I tied to repro your issue, so do I, when I create a test project and run it with debug mode, everything is fine, but when I choose “release” mode. An error occurs and show me that

    A project with an Output type of Class Library cannot be started directly In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.”  So I want to know if you encountered same error as me?

    In general, a project you have downloaded compiles into a dll assembly and provide a set of classes with implemented functionality. Here is my resolved method:

    Add to your solution a new project with Output Type of either Console Application or Windows Application (VS Add Project wizard will offer you different templates of Projects) – then right click it, select “Start Up Project”- right click your test project –click “properties” – application tab – output type - to choose an appropriate type that matches your project - save and build your project. Run the project to see the output.

    Besides, I find some members solved it by other workarounds, I am willing to post them if it is possibly helpful for you:

    @ AllenG said, “You'll need some kind of app (Console Apps are my favorite for debugging, but a WinForm will do) which uses your Class Library. Just add a new project (in the same solution) of a Console Application or Windows Forms Application, and add a reference to your current project. Once you've done that, make any calls you need, set your break points, and go to town.

    Reference: https://stackoverflow.com/questions/3363106/a-project-with-an-output-type-of-class-library-cannot-be-started-directly .

    Looking forwards your reply.

    Br

    May


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, January 28, 2019 2:17 PM
  • Thank you very much, Lance Li.
    Monday, January 28, 2019 4:24 PM
  • Thank you, May Luo, for your reply.

    Although, I don’t Start (F5) the unit test project, I just execute its unit tests within Test Explorer.

    Best regards.

    Monday, January 28, 2019 4:28 PM
  • Hi MarcoDorantes,

    You could add your latest information or comment here:

    https://developercommunity.visualstudio.com/content/problem/437431/weird-indexoutofrangeexception-with-testmethod-on.html

    The product team member provided some suggestions there.

    Best Regards,

    JK


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 11, 2019 2:56 AM
    Moderator