1 // ***********************************************************************
2 // Copyright (c) 2009-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 #define NUNIT_FRAMEWORK
30 using System.Collections.Generic;
31 using NUnit.Framework.Interfaces;
32 using NUnit.Framework.Internal;
33 using NUnit.Framework.Internal.Builders;
35 namespace NUnit.Framework
38 /// TestFixtureAttribute is used to mark a class that represents a TestFixture.
40 [AttributeUsage(AttributeTargets.Class, AllowMultiple=true, Inherited=true)]
41 public class TestFixtureAttribute : NUnitAttribute, IFixtureBuilder, ITestFixtureData
43 private readonly NUnitTestFixtureBuilder _builder = new NUnitTestFixtureBuilder();
48 /// Default constructor
50 public TestFixtureAttribute() : this( new object[0] ) { }
53 /// Construct with a object[] representing a set of arguments.
54 /// In .NET 2.0, the arguments may later be separated into
55 /// type arguments and constructor arguments.
57 /// <param name="arguments"></param>
58 public TestFixtureAttribute(params object[] arguments)
60 RunState = RunState.Runnable;
61 Arguments = arguments;
62 TypeArgs = new Type[0];
63 Properties = new PropertyBag();
68 #region ITestData Members
71 /// Gets or sets the name of the test.
73 /// <value>The name of the test.</value>
74 public string TestName { get; set; }
77 /// Gets or sets the RunState of this test fixture.
79 public RunState RunState { get; private set; }
82 /// The arguments originally provided to the attribute
84 public object[] Arguments { get; private set; }
87 /// Properties pertaining to this fixture
89 public IPropertyBag Properties { get; private set; }
93 #region ITestFixtureData Members
96 /// Get or set the type arguments. If not set
97 /// explicitly, any leading arguments that are
98 /// Types are taken as type arguments.
100 public Type[] TypeArgs { get; set; }
104 #region Other Properties
107 /// Descriptive text for this fixture
109 public string Description
111 get { return Properties.Get(PropertyNames.Description) as string; }
112 set { Properties.Set(PropertyNames.Description, value); }
116 /// The author of this fixture
120 get { return Properties.Get(PropertyNames.Author) as string; }
121 set { Properties.Set(PropertyNames.Author, value); }
125 /// The type that this fixture is testing
129 get { return _testOf; }
133 Properties.Set(PropertyNames.TestOf, value.FullName);
136 private Type _testOf;
139 /// Gets or sets the ignore reason. May set RunState as a side effect.
141 /// <value>The ignore reason.</value>
144 get { return IgnoreReason; }
145 set { IgnoreReason = value; }
149 /// Gets or sets the reason for not running the fixture.
151 /// <value>The reason.</value>
154 get { return this.Properties.Get(PropertyNames.SkipReason) as string; }
155 set { this.Properties.Set(PropertyNames.SkipReason, value); }
159 /// Gets or sets the ignore reason. When set to a non-null
160 /// non-empty value, the test is marked as ignored.
162 /// <value>The ignore reason.</value>
163 public string IgnoreReason
165 get { return Reason; }
168 RunState = RunState.Ignored;
174 /// Gets or sets a value indicating whether this <see cref="NUnit.Framework.TestFixtureAttribute"/> is explicit.
177 /// <c>true</c> if explicit; otherwise, <c>false</c>.
181 get { return RunState == RunState.Explicit; }
182 set { RunState = value ? RunState.Explicit : RunState.Runnable; }
186 /// Gets and sets the category for this fixture.
187 /// May be a comma-separated list of categories.
189 public string Category
193 //return Properties.Get(PropertyNames.Category) as string;
194 var catList = Properties[PropertyNames.Category];
198 switch (catList.Count)
203 return catList[0] as string;
205 var cats = new string[catList.Count];
207 foreach (string cat in catList)
210 return string.Join(",", cats);
215 foreach (string cat in value.Split(new char[] { ',' }))
216 Properties.Add(PropertyNames.Category, cat);
222 #region IFixtureBuilder Members
225 /// Build a fixture from type provided. Normally called for a Type
226 /// on which the attribute has been placed.
228 /// <param name="typeInfo">The type info of the fixture to be used.</param>
229 /// <returns>A an IEnumerable holding one TestFixture object.</returns>
230 public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo)
232 yield return _builder.BuildFrom(typeInfo, this);