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
37 using NUnit.Framework.TUnit;
40 /// SetUpTearDownCommand runs any SetUp methods for a suite,
41 /// runs the test and then runs any TearDown methods.
43 public class SetUpTearDownCommand : DelegatingTestCommand
45 private IList<SetUpTearDownItem> _setUpTearDownItems;
48 /// Initializes a new instance of the <see cref="SetUpTearDownCommand"/> class.
50 /// <param name="innerCommand">The inner command.</param>
51 public SetUpTearDownCommand(TestCommand innerCommand)
54 Guard.ArgumentValid(innerCommand.Test is TestMethod, "SetUpTearDownCommand may only apply to a TestMethod", "innerCommand");
55 Guard.OperationValid(Test.TypeInfo != null, "TestMethod must have a non-null TypeInfo");
57 _setUpTearDownItems = CommandBuilder.BuildSetUpTearDownList(Test.TypeInfo.Type, typeof(SetUpAttribute), typeof(TearDownAttribute));
61 /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
63 /// <param name="context">The context in which the test should run.</param>
64 /// <returns>A TestResult</returns>
65 public override TestResult Execute(TestExecutionContext context)
69 TAsyncThreadMgr.GetInstance().ClearSetUpTearDown();
70 for (int i = _setUpTearDownItems.Count; i > 0;)
71 _setUpTearDownItems[--i].RunSetupForTC(context);
73 for (int i = 0; i < _setUpTearDownItems.Count; i++)
74 _setUpTearDownItems[i].RunTearDownForTC(context);
76 context.CurrentResult = innerCommand.Execute(context);
80 #if !NETCF && !SILVERLIGHT && !PORTABLE
81 if (ex is ThreadAbortException)
84 context.CurrentResult.RecordException(ex);
87 return context.CurrentResult;