none
There is no clear way to catch an unhandled exception in a TestMethod and TestCleanup RRS feed

  • Question

  • I asked this question in GitHub

    There is no clear way to catch an unhandled exception in a TestMethod and TestCleanup

    Description

    There is no clear way to catch an unhandled exception in a TestMethod and TestCleanup.

    We are using Selenium with MSTest to run tests. I would want to take an screenshot if the TestMethod or TestCleanup fails. However, creating a new TestMethodAttribute only let's me change how the test is run and wrapping the method in a try-catch is no good as I need a EventFiringWebDriver instance to take the screenshot.

    TestCleanup cannot be changed as far as I am aware, so if TestCleanup throws exception I also cannot take a screenshot.

    What we are doing is setting a TestBase class which already has all the previous configuration set up and I was trying to avoid wrapping each action in TestMethod in a try-catch.

    I understand this is not an issue per se, but I don't know where else to turn to for information in MSTest. If you search for exceptions regarding MSTest absolutely everything talks about asserting exceptions but nothing about how to handle unexpected ones.

    So my question is: What is the proper way to handle an unhandled exception at the TestMethod level so I am able to take a screenshot before the TestMethod throws the exception? And, is this also possible for TestCleanup?

    Thank you

    Environment

    Visual Studio 2015 update 3
    Windows 10 x64 (17134.885). MSTest 1.4.0

    Friday, September 20, 2019 3:22 PM

All replies

  • Seriosly? No takers?
    Monday, September 23, 2019 7:42 AM
  • Hi DanielRMig,

    Sorry for delay in reply.

    As far as I know, the exception handling of test methods is based on the test itself, that is, based on whether the test has a result or successes or fails to judge and throw an exception (expected exception).

    And for the unexpected exceptions, more from the C# language framework, so we recommend you could refer this doc about other exception handling methods.

    Any feedback will be expected.

    Best Regards,

    Dylan


    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, September 23, 2019 10:20 AM
  • Hello Dylan, and thanks for replying.

    I've seen in microsoft docs that there are events (https://docs.microsoft .com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.testexecution?view=visualstudiosdk-2017) but it seems they are only available for Visual Studio 2017. Could you please confirm if this is true and if they could be used in this case?

    Thank you.

    Monday, September 23, 2019 11:16 AM
  • Hi DanielRMig,

    Sorry for delay in reply.

    I have tried these events which is from the doc, and it could catch a unexpected exception in visual studio 2015. Please refer the below screenshot:

    Hope it could help you.

    Cheers,

    Dylan


    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

    Friday, September 27, 2019 8:18 AM