From 2bb7806b19b7c3d74584bae34b7316fee63dc7f9 Mon Sep 17 00:00:00 2001 From: Andrey G Date: Fri, 19 May 2023 09:57:17 +0300 Subject: [PATCH] Unwrap TargetInvocationException when returning step result --- Allure.NUnit.Examples/AllureStepTest.cs | 14 ++++++++++++++ Allure.NUnit/Core/Steps/AllureStepAspect.cs | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Allure.NUnit.Examples/AllureStepTest.cs b/Allure.NUnit.Examples/AllureStepTest.cs index 2bd70fa4..e2710c65 100644 --- a/Allure.NUnit.Examples/AllureStepTest.cs +++ b/Allure.NUnit.Examples/AllureStepTest.cs @@ -27,6 +27,13 @@ public static void StepWithParams(object firstParam, object lastParam) Console.WriteLine(firstParam); Console.WriteLine(lastParam); } + + [AllureStep("This step should fail")] + public static int FailedStep() + { + var x = 1; + return 1 / (1 - x); + } } [AllureSuite("Tests - Steps")] @@ -70,5 +77,12 @@ public void SimpleStepTest2() StepsExamples.StepWithParams(1, 2); StepsExamples.StepWithParams(new[] { 1, 3, 5}, "array"); } + + [Test] + [AllureName("Should fail on step")] + public void TestWithFailedStep() + { + Assert.That(()=> StepsExamples.FailedStep(), Throws.TypeOf()); + } } } \ No newline at end of file diff --git a/Allure.NUnit/Core/Steps/AllureStepAspect.cs b/Allure.NUnit/Core/Steps/AllureStepAspect.cs index a4174960..c6bfbd30 100644 --- a/Allure.NUnit/Core/Steps/AllureStepAspect.cs +++ b/Allure.NUnit/Core/Steps/AllureStepAspect.cs @@ -63,8 +63,15 @@ public object Around( } else { - return SyncHandler.MakeGenericMethod(returnType) - .Invoke(this, new object[] { target, args, metadata, stepName, stepParameters }); + try + { + return SyncHandler.MakeGenericMethod(returnType) + .Invoke(this, new object[] { target, args, metadata, stepName, stepParameters }); + } + catch (TargetInvocationException e) + { + throw e.InnerException ?? e; + } } }