Skip to content

Commit

Permalink
Allure NUnit - failing async handler
Browse files Browse the repository at this point in the history
  • Loading branch information
neparij committed Dec 30, 2022
1 parent 2cb811b commit 4342a78
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions Allure.NUnit/Core/Steps/AllureStepAspect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Allure.Net.Commons;
using AspectInjector.Broker;
using NUnit.Allure.Attributes;
using NUnit.Framework;
using NUnit.Framework.Internal;

namespace NUnit.Allure.Core.Steps
Expand Down Expand Up @@ -62,16 +63,35 @@ public class AllureStepAspect
})
.Where(x => x != null)
.ToList();

try
{
StartFixture(metadata, stepName);
StartStep(metadata, stepName, stepParameters);

executionResult = GetStepExecutionResult(returnType, target, args);

PassStep(metadata);
PassFixture(metadata);
if (typeof(Task).IsAssignableFrom(executionResult.GetType()))
{
((Task)executionResult).ContinueWith((task) =>
{
if (task.IsFaulted)
{
var e = task.Exception;
ThrowStep(metadata, e?.InnerException);
ThrowFixture(metadata, e?.InnerException);
}
else
{
PassStep(metadata);
PassFixture(metadata);
}
});
}
else
{
PassStep(metadata);
PassFixture(metadata);
}
}
catch (Exception e)
{
Expand All @@ -81,29 +101,6 @@ public class AllureStepAspect
}

return executionResult;

// object result;
// try
// {
// AllureLifecycle.Instance.StartStep(Guid.NewGuid().ToString(), stepResult);
// result = target(args);
// AllureLifecycle.Instance.StopStep(step => stepResult.status = Status.passed);
// }
// catch (Exception e)
// {
// AllureLifecycle.Instance.StopStep(step =>
// {
// step.statusDetails = new StatusDetails
// {
// message = e.Message,
// trace = e.StackTrace
// };
// step.status = Status.failed;
// });
// throw;
// }
//
// return result;
}

private static void StartStep(MethodBase metadata, string stepName, List<Parameter> stepParameters)
Expand Down Expand Up @@ -132,7 +129,7 @@ private static void ThrowStep(MethodBase metadata, Exception e)
trace = e.StackTrace
};

if (e is NUnitException)
if (e is NUnitException || e is AssertionException)
{
StepsHelper.FailStep(result => result.statusDetails = exceptionStatusDetails);
}
Expand Down Expand Up @@ -217,26 +214,12 @@ private object GetStepExecutionResult(Type returnType, Func<object[], object> ta

private static T WrapSync<T>(Func<object[], object> target, object[] args)
{
try
{
return (T)target(args);
}
catch (Exception e)
{
return default(T);
}
return (T)target(args);
}

private static async Task<T> WrapAsync<T>(Func<object[], object> target, object[] args)
{
try
{
return await (Task<T>)target(args);
}
catch (Exception e)
{
return default(T);
}
return await (Task<T>)target(args);
}
}
}

0 comments on commit 4342a78

Please sign in to comment.