1 // ***********************************************************************
2 // Copyright (c) 2007-2015 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 // RepeatAttribute should either
26 // 1) Apply at load time to create the exact number of tests, or
27 // 2) Apply at run time, generating tests or results dynamically
29 // #1 is feasible but doesn't provide much benefit
30 // #2 requires infrastructure for dynamic test cases first
33 #define NUNIT_FRAMEWORK
38 using NUnit.Framework.Interfaces;
39 using NUnit.Framework.Internal;
40 using NUnit.Framework.Internal.Commands;
42 namespace NUnit.Framework
45 /// RepeatAttribute may be applied to test case in order
46 /// to run it multiple times.
48 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
49 public class RepeatAttribute : PropertyAttribute, IWrapSetUpTearDown
54 /// Construct a RepeatAttribute
56 /// <param name="count">The number of times to run the test</param>
57 public RepeatAttribute(int count) : base(count)
62 #region IWrapSetUpTearDown Members
65 /// Wrap a command and return the result.
67 /// <param name="command">The command to be wrapped</param>
68 /// <returns>The wrapped command</returns>
69 public TestCommand Wrap(TestCommand command)
71 return new RepeatedTestCommand(command, _count);
76 #region Nested RepeatedTestCommand Class
79 /// The test command for the RepeatAttribute
81 public class RepeatedTestCommand : DelegatingTestCommand
83 private int repeatCount;
86 /// Initializes a new instance of the <see cref="RepeatedTestCommand"/> class.
88 /// <param name="innerCommand">The inner command.</param>
89 /// <param name="repeatCount">The number of repetitions</param>
90 public RepeatedTestCommand(TestCommand innerCommand, int repeatCount)
93 this.repeatCount = repeatCount;
97 /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
99 /// <param name="context">The context in which the test should run.</param>
100 /// <returns>A TestResult</returns>
101 public override TestResult Execute(TestExecutionContext context)
103 int count = repeatCount;
107 context.CurrentResult = innerCommand.Execute(context);
109 // TODO: We may want to change this so that all iterations are run
110 if (context.CurrentResult.ResultState != ResultState.Success)
114 return context.CurrentResult;