-
Notifications
You must be signed in to change notification settings - Fork 1
/
RetryAttribute.cs
50 lines (42 loc) · 1.43 KB
/
RetryAttribute.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
using System;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
namespace SkbKontur.NUnit.Retries
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class RetryAttribute : Attribute, IRepeatTest, IApplyToContext
{
protected RetryAttribute(IRetryStrategy strategy)
{
this.strategy = strategy;
}
public TestCommand Wrap(TestCommand command)
{
return new RetryCommand(command, strategy);
}
public void ApplyToContext(TestExecutionContext context)
{
ApplyToTestRecursively(context.CurrentTest, overwrite : true);
}
private void ApplyToTestRecursively(Test test, bool overwrite)
{
if (test.GetCustomAttributes<NoRetryAttribute>(true).Any()
|| (!overwrite && test.GetCustomAttributes<RetryAttribute>(true).Any()))
{
return;
}
if (test is TestMethod method)
{
method.Method = new CustomAttributeMethodWrapper(method.Method, this);
return;
}
foreach (var child in test.Tests.OfType<Test>())
{
ApplyToTestRecursively(child, overwrite : false);
}
}
private readonly IRetryStrategy strategy;
}
}