allure-csharp icon indicating copy to clipboard operation
allure-csharp copied to clipboard

System.Reflection.TargetInvocationException is throwing instead of actual step exception

Open undron opened this issue 1 year ago • 4 comments

Consider this code:

        [AllureStep("This step should fail")]
        public static int FailedStep()
        {
            var x = 1;
            return 1 / (1 - x);
        }

        [Test]
        [AllureName("Should fail on step")]
        public void ReturnFail()
        {
            FailedStep();
        }

This test failed with exception:

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.DivideByZeroException : Attempted to divide by zero.

but should be failed with System.DivideByZeroException : Attempted to divide by zero.

  • Allure version: 2.9.5-preview.1

undron avatar May 19 '23 06:05 undron

@unickq Are there any plans to address this issue ? This is especially problematic when trying to handle exception thrown by other step methods: Ex: trying to catch a timeout exception thrown by a WebDriverWait.

vladdex avatar May 23 '24 10:05 vladdex

Hi, @vladdex What runtime do you use?

delatrie avatar May 23 '24 10:05 delatrie

@delatrie I'm reproducing this in dotnet 8. Accessing the InnerException property of the TargetInvocationException reveals the actual exception.

vladdex avatar May 23 '24 10:05 vladdex

Hello, I also faced this same issue, as a workaround, you can have a look at my solution in the discussion section of the Allure organization. It involves creating an aspect around the base aspect to handle this exception and unwrap it. https://github.com/orgs/allure-framework/discussions/2590#discussioncomment-9830924

driverog avatar Jun 20 '24 16:06 driverog