1 // ***********************************************************************
2 // Copyright (c) 2014 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 #define NUNIT_FRAMEWORK
30 using System.Collections.Generic;
31 using System.Threading;
33 namespace NUnit.Framework.Internal.Commands
38 /// TestActionCommand runs the BeforeTest actions for a test,
39 /// then runs the test and finally runs the AfterTestActions.
41 public class TestActionCommand : DelegatingTestCommand
43 private IList<TestActionItem> _actions = new List<TestActionItem>();
46 /// Initializes a new instance of the <see cref="TestActionCommand"/> class.
48 /// <param name="innerCommand">The inner command.</param>
49 public TestActionCommand(TestCommand innerCommand)
52 Guard.ArgumentValid(innerCommand.Test is TestMethod, "TestActionCommand may only apply to a TestMethod", "innerCommand");
56 /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
58 /// <param name="context">The context in which the test should run.</param>
59 /// <returns>A TestResult</returns>
60 public override TestResult Execute(TestExecutionContext context)
62 if (Test.Fixture == null)
63 Test.Fixture = context.TestObject;
65 // In the current implementation, upstream actions only apply to tests. If that should change in the future,
66 // then actions would have to be tested for here. For now we simply assert it in Debug. We allow
67 // ActionTargets.Default, because it is passed down by ParameterizedMethodSuite.
68 foreach (ITestAction action in context.UpstreamActions)
70 System.Diagnostics.Debug.Assert(
71 action.Targets == ActionTargets.Default || (action.Targets & ActionTargets.Test) == ActionTargets.Test,
72 "Invalid target on upstream action: " + action.Targets.ToString());
74 _actions.Add(new TestActionItem(action));
77 //foreach (ITestAction action in ActionsHelper.GetActionsFromAttributeProvider(((TestMethod)Test).Method.MethodInfo))
78 // if (action.Targets == ActionTargets.Default || (action.Targets & ActionTargets.Test) == ActionTargets.Test)
79 // _actions.Add(new TestActionItem(action));
83 for (int i = 0; i < _actions.Count; i++)
84 _actions[i].BeforeTest(Test);
86 context.CurrentResult = innerCommand.Execute(context);
90 #if !NETCF && !SILVERLIGHT && !PORTABLE
91 if (ex is ThreadAbortException)
94 context.CurrentResult.RecordException(ex);
98 if (context.ExecutionStatus != TestExecutionStatus.AbortRequested)
99 for (int i = _actions.Count - 1; i >= 0; i--)
100 _actions[i].AfterTest(Test);
103 return context.CurrentResult;