This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
AllureStepAspect.cs
55 lines (49 loc) · 1.87 KB
/
AllureStepAspect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Reflection;
using Allure.Commons;
using AspectInjector.Broker;
using NUnit.Allure.Attributes;
namespace NUnit.Allure.Core.Steps
{
[Aspect(Scope.Global)]
public class AllureStepAspect
{
[Advice(Kind.Around, Targets = Target.Method)]
public object WrapStep(
[Argument(Source.Name)] string name,
[Argument(Source.Metadata)] MethodBase methodBase,
[Argument(Source.Arguments)] object[] arguments,
[Argument(Source.Target)] Func<object[], object> method)
{
var stepName = methodBase.GetCustomAttribute<AllureStepAttribute>().StepName;
for (var i = 0; i < arguments.Length; i++)
{
stepName = stepName?.Replace("{" + i + "}", arguments[i]?.ToString() ?? "null");
}
var stepResult = string.IsNullOrEmpty(stepName)
? new StepResult {name = name, parameters = AllureStepParameterHelper.CreateParameters(arguments)}
: new StepResult {name = stepName, parameters = AllureStepParameterHelper.CreateParameters(arguments)};
object result;
try
{
AllureLifecycle.Instance.StartStep(Guid.NewGuid().ToString(), stepResult);
result = method(arguments);
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;
}
}
}