-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatForConfigValueAttribute.cs
More file actions
101 lines (97 loc) · 3.93 KB
/
RepeatForConfigValueAttribute.cs
File metadata and controls
101 lines (97 loc) · 3.93 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using Gallio.Common.Reflection;
using Gallio.Framework;
using Gallio.Framework.Pattern;
using Gallio.Model;
using MbUnit.Framework;
namespace ProtoTest.Nightshade
{
/// <summary>
/// Runs a test the number of times specified by the key in the App.Config
/// </summary>
/// <remarks>
/// <para>
/// Each repetition of the test method will occur within its own individually labeled
/// test step so that it can be identified in the test report.
/// </para>
/// <para>
/// The initialize, setup, teardown and dispose methods will are invoked around each
/// repetition of the test.
/// </para>
/// </remarks>
/// <seealso cref="RepeatAttribute" />
[AttributeUsage(PatternAttributeTargets.Test, AllowMultiple = true, Inherited = true)]
public class RepeatForConfigValueAttribute : TestDecoratorPatternAttribute
{
private int _numberOfRepetitions;
private string _configKey;
private string _configValue;
/// <summary>
/// Will re-run the test method each time we get a failure for a limited number of attempts.
/// </summary>
/// <example>
/// <code><![CDATA[
/// [Test]
/// [RepeatForConfigValue("NameOfKey")]
/// public void Test()
/// {
/// }
/// ]]></code>
/// </example>
/// <param name="configKey">The key in the App.Config to look for. Assumes the value is an int <add key="NameOfKey" value="2"/> </param>
/// </exception>
public RepeatForConfigValueAttribute(string configKey)
{
try
{
this._configKey = configKey;
this._configValue = Config.GetConfigValue(configKey, "Default");
if (this._configValue == "Default")
{
_numberOfRepetitions = 1;
}
else
{
_numberOfRepetitions = int.Parse(_configValue);
}
}
catch (Exception e)
{
throw new ArgumentException("RepeatForConfigValueAttribue could not read the App.Config file for a value=" + configKey + " " + e.Message);
}
}
/// <inheritdoc />
protected override void DecorateTest(IPatternScope scope, ICodeElementInfo codeElement)
{
scope.TestBuilder.TestInstanceActions.RunTestInstanceBodyChain.Around(
delegate(PatternTestInstanceState state,
Gallio.Common.Func<PatternTestInstanceState, TestOutcome> inner)
{
TestOutcome outcome = TestOutcome.Skipped;
if (_configValue == "Default")
{
TestLog.Warnings.WriteLine("RepeatForConfigValueAttribue did not find a key matching '"+_configKey +"' in the App.Config, using a default value of 1");
_numberOfRepetitions = 1;
}
else
{
_numberOfRepetitions = int.Parse(_configValue);
}
for (int i = 0; i < _numberOfRepetitions; i++)
{
string name = String.Format("Repetition #{0}", i + 1);
TestContext context = TestStep.RunStep(name, delegate
{
TestOutcome innerOutcome = inner(state);
if (innerOutcome.Status != TestStatus.Passed)
{
throw new SilentTestException(innerOutcome);
}
}, null, false, codeElement);
outcome = context.Outcome;
}
return outcome;
});
}
}
}