Skip to content

Commit

Permalink
Fix NUnit OneTimeTearDown reporting (#419)
Browse files Browse the repository at this point in the history
  - missing container if [AllureAfter] is applied to some, but not all
    OneTimeTearDown methods of a class
  - Fix IndexOutOfRangeException if [AllureAfter] is applied to a method
    with arguments
  • Loading branch information
delatrie committed Mar 27, 2024
1 parent b92c51b commit 935dc9c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions Allure.NUnit/Internals/StopContainerAspect.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Allure.Net.Commons;
using AspectInjector.Broker;
using NUnit.Allure.Attributes;
using NUnit.Framework;
using NUnit.Framework.Internal;

Expand All @@ -14,6 +16,7 @@ public class StopContainerAspect
[Advice(Kind.Around)]
public object StopTestContainerAfterTheLastOneTimeTearDown(
[Argument(Source.Target)] Func<object[], object> target,
[Argument(Source.Arguments)] object[] arguments,
[Argument(Source.Metadata)] MethodBase metadata
)
{
Expand All @@ -22,18 +25,21 @@ public object StopTestContainerAfterTheLastOneTimeTearDown(
CurrentTearDownCount++;
if (IsLastTearDown)
{
return CallAndStopContainer(target);
return CallAndStopContainer(target, arguments);
}
}
return target(Array.Empty<object>());
return target(arguments);
}

static object CallAndStopContainer(Func<object[], object> target)
static object CallAndStopContainer(
Func<object[], object> target,
object[] arguments
)
{
object returnValue = null;
try
{
returnValue = target(Array.Empty<object>());
returnValue = target(arguments);
}
finally
{
Expand All @@ -43,7 +49,7 @@ static object CallAndStopContainer(Func<object[], object> target)
}
else
{
// This branch is executed only in case of an async one time tear down
// This branch is executed only in case of an async OneTimeTearDown
returnValue = StopContainerAfterAsyncTearDown(returnValue);
}
}
Expand All @@ -52,6 +58,8 @@ static object CallAndStopContainer(Func<object[], object> target)

async static Task StopContainerAfterAsyncTearDown(object awaitable)
{
// Currently, we only support the Task return type for async tear
// downs. ValueTask support should be added to commons first.
await ((Task)awaitable).ConfigureAwait(false);
StopContainer();
}
Expand Down Expand Up @@ -84,12 +92,15 @@ static int CurrentTearDownCount
);
}

static int TotalTearDownCount
{
get => (
(TestSuite)TestExecutionContext.CurrentContext.CurrentTest
).OneTimeTearDownMethods.Length;
}
static int TotalTearDownCount =>
((TestSuite)TestExecutionContext.CurrentContext.CurrentTest)
.OneTimeTearDownMethods
.Count(
m => Attribute.IsDefined(
m.MethodInfo,
typeof(AllureAfterAttribute)
)
);

const string CurrentTearDownKey = "CurrentTearDownCount";
}
Expand Down

0 comments on commit 935dc9c

Please sign in to comment.