none
unit tests that test for exceptions RRS feed

  • Question

  • Hello,

    I'm trying to write unit tests to test throwing of parse exceptions. How can I do this with the unit test failing BECAUSE of the throwing of parse exceptions.

    First of all, here is the code that throws the parse exception:

            public async Task<int> ImportAsync(StreamReader reader, int projectId, IProgress<ParseProgress> progress)
            {
    ...

                var nodes = ParseData(progress);

                if (_errorMessages.Count > 0)
                {
                    string fullErrorMessage = _errorMessages.Aggregate((x, y) => x + "\n" + y);
                    _errorMessages.Clear();
                    throw new ParsingException(string.Empty, fullErrorMessage);
                }
    ...
    }

    This method parses data from a CSV file. The call to ParseData(...) is what actually does the parsing. Whenever it encounters a problem during the parsing, it logs a message to _errorMessages, a List<string> of error messages. When it returns from ParseData(...), it checks the list for Count > 0. If error messages exist, it throws a ParseException with the full list of error messages concatenated by '\n' as the exception message.

    My unit tests begin their setup with the following method:

                protected override void When()
                {
                   SUT.ImportAsync(TestData, 1, new Progress<ParseProgress>()).Wait();
                }

    It passes in TestData which contains a moc CSV file, one row of which contains the error I'd like to test for (i.e. it should throw a ParseException when it hits this row).

    But the fact that it throws a ParseException at this point results in any unit tests depending on this setup failing.

    How can I setup unit tests such that rather than failing when exceptions are thrown, it actually detects when exceptions are thrown and recognizes this as a pass.
    Tuesday, January 3, 2017 3:44 PM

All replies

  • How can I setup unit tests such that rather than failing when exceptions are thrown, it actually detects when exceptions are thrown and recognizes this as a pass.

    If the expected exception is not thrown, the test fails. If the expected exception is thrown, the test passes. 

    https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

    Tuesday, January 3, 2017 10:01 PM
  • Hi gib898,

    Thank you for posting here.

    According to your question is more related to unit test, I will move it to Visual Studio Unit Testing forum for suitable support.

    The Visual C# forum discuss and ask the C# language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    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.

    Wednesday, January 4, 2017 5:59 AM
  • Hi gib898,

    >> How can I setup unit tests such that rather than failing when exceptions are thrown, it actually detects when exceptions are thrown and recognizes this as a pass.

    You could try to use "try catch" block to make it.

    The code will look like below:

    [TestMethod]
    
            public void test1()
    
       try
    
                {
    
                    Assert.AreEqual(1, 2); // the execution
    
                }
    
                catch(Exception ex)
    
                {
    
                    Debug.WriteLine(ex.Message);  // VS will run this code no matter the exception is thrown or not
    
                    
    
                  }


    Though there is an exception in this code, the test will pass and output the error message anyway.

    I hope it's helpful to you. 

    Best regards,

    Fletcher



    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.

    Thursday, January 5, 2017 7:46 AM
  • The issue is the exception is not thrown in a test but in the test setup:

                protected override void When()
                {
                   SUT.ImportAsync(TestData, 1, new Progress<ParseProgress>()).Wait();
                }

    Any actual test that are supposed to run after this don't get run at all.

    I'm just wondering if there's a way to tell the setup to expect exceptions to get thrown when reading TestData.

    Thursday, January 5, 2017 11:49 PM
  • Hi gib898,

    If this exception happened in setup method, it seems there is no other way to make the following tests pass.

    You may refer to this article, at the bottom of the page, the author mentioned this. Hope it’s helpful.

    https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

    Best regards,

    Fletcher


    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.

    • Proposed as answer by Fletch Zhou Wednesday, January 11, 2017 9:41 AM
    Friday, January 6, 2017 10:17 AM