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;
32 namespace NUnit.Framework
38 /// SetUpFixtureAttribute is used to identify a SetUpFixture
40 [AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
41 public class SetUpFixtureAttribute : NUnitAttribute, IFixtureBuilder
43 #region ISuiteBuilder Members
46 /// Build a SetUpFixture from type provided. Normally called for a Type
47 /// on which the attribute has been placed.
49 /// <param name="typeInfo">The type info of the fixture to be used.</param>
50 /// <returns>A SetUpFixture object as a TestSuite.</returns>
51 public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo)
53 SetUpFixture fixture = new SetUpFixture(typeInfo);
55 if (fixture.RunState != RunState.NotRunnable)
58 if (!IsValidFixtureType(typeInfo, ref reason))
60 fixture.RunState = RunState.NotRunnable;
61 fixture.Properties.Set(PropertyNames.SkipReason, reason);
65 return new TestSuite[] { fixture };
70 #region Helper Methods
72 private bool IsValidFixtureType(ITypeInfo typeInfo, ref string reason)
74 if (typeInfo.IsAbstract)
76 reason = string.Format("{0} is an abstract class", typeInfo.FullName);
80 if (!typeInfo.HasConstructor(new Type[0]))
82 reason = string.Format("{0} does not have a default constructor", typeInfo.FullName);
86 var invalidAttributes = new Type[] {
87 typeof(SetUpAttribute),
88 typeof(TearDownAttribute),
89 #pragma warning disable 618 // Obsolete Attributes
90 typeof(TestFixtureSetUpAttribute),
91 typeof(TestFixtureTearDownAttribute) };
92 #pragma warning restore
94 foreach (Type invalidType in invalidAttributes)
95 if (typeInfo.HasMethodWithAttribute(invalidType))
97 reason = invalidType.Name + " attribute not allowed in a SetUpFixture";