1 // ***********************************************************************
2 // Copyright (c) 2012 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 NUnit.Framework.Interfaces;
33 namespace NUnit.Framework.Internal.Commands
36 /// OneTimeSetUpCommand runs any one-time setup methods for a suite,
37 /// constructing the user test object if necessary.
39 public class OneTimeSetUpCommand : TestCommand
41 private readonly TestSuite _suite;
42 private readonly ITypeInfo _typeInfo;
43 private readonly object[] _arguments;
44 private readonly List<SetUpTearDownItem> _setUpTearDown;
45 private readonly List<TestActionItem> _actions;
48 /// Constructs a OneTimeSetUpCommand for a suite
50 /// <param name="suite">The suite to which the command applies</param>
51 /// <param name="setUpTearDown">A SetUpTearDownList for use by the command</param>
52 /// <param name="actions">A List of TestActionItems to be run after Setup</param>
53 public OneTimeSetUpCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDown, List<TestActionItem> actions)
57 _typeInfo = suite.TypeInfo;
58 _arguments = suite.Arguments;
59 _setUpTearDown = setUpTearDown;
64 /// Overridden to run the one-time setup for a suite.
66 /// <param name="context">The TestExecutionContext to be used.</param>
67 /// <returns>A TestResult</returns>
68 public override TestResult Execute(TestExecutionContext context)
70 if (_typeInfo != null)
72 // Use pre-constructed fixture if available, otherwise construct it
73 if (!_typeInfo.IsStaticClass)
75 context.TestObject = _suite.Fixture ?? _typeInfo.Construct(_arguments);
76 if (_suite.Fixture == null)
78 _suite.Fixture = context.TestObject;
80 Test.Fixture = _suite.Fixture;
83 for (int i = _setUpTearDown.Count; i > 0; )
84 _setUpTearDown[--i].RunSetUp(context);
88 for (int i = 0; i < _actions.Count; i++)
89 _actions[i].BeforeTest(Test);
91 return context.CurrentResult;