BUGFIX: when deleting directories, delete any files within them
[tfs.git] / nunit20 / core / TestFixture.cs
blob7b1f94488718f66cbda215d1e7fb035f811ee134
1 using System;
2 using System.Collections;
3 using System.Reflection;
5 namespace NUnit.Core
7 /// <summary>
8 /// A TestSuite that wraps a class marked with TestFixtureAttribute
9 /// </summary>
10 public class TestFixture : TestSuite
12 private const string FIXTURE_SETUP_FAILED = "Fixture setup failed";
14 #region Constructors
16 public TestFixture( object fixture ) : base( fixture, 0 )
18 Initialize();
21 public TestFixture( object fixture, int assemblyKey ) : base( fixture, assemblyKey )
23 Initialize();
26 public TestFixture( Type fixtureType ) : base( fixtureType, 0 )
28 Initialize();
31 public TestFixture( Type fixtureType, int assemblyKey ) : base( fixtureType, assemblyKey )
33 Initialize();
36 private void Initialize()
38 try
40 Reflect.CheckFixtureType( fixtureType );
42 IList categories = Reflect.GetCategories( fixtureType );
43 CategoryManager.Add( categories );
44 this.Categories = categories;
46 this.fixtureSetUp = Reflect.GetFixtureSetUpMethod( fixtureType );
47 this.fixtureTearDown = Reflect.GetFixtureTearDownMethod( fixtureType );
49 this.IsExplicit = Reflect.HasExplicitAttribute( fixtureType );
51 if ( Reflect.HasIgnoreAttribute( fixtureType ) )
53 this.ShouldRun = false;
54 this.IgnoreReason = Reflect.GetIgnoreReason( fixtureType );
57 this.Description = Reflect.GetDescription( fixtureType );
59 MethodInfo [] methods = fixtureType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static|BindingFlags.NonPublic);
60 foreach(MethodInfo method in methods)
62 TestCase testCase = TestCaseBuilder.Make( fixtureType, method );
63 if(testCase != null)
65 testCase.AssemblyKey = this.AssemblyKey;
66 this.Add( testCase );
70 if( this.CountTestCases() == 0 )
72 this.ShouldRun = false;
73 this.IgnoreReason = this.Name + " does not have any tests";
76 catch( InvalidTestFixtureException exception )
78 this.ShouldRun = false;
79 this.IgnoreReason = exception.Message;
83 #endregion
85 #region Static Methods
87 public static bool IsValidType( Type type )
89 return !type.IsAbstract && Reflect.HasTestFixtureAttribute( type );
92 #endregion
94 public override void DoSetUp( TestResult suiteResult )
96 try
98 if ( Fixture == null )
99 Fixture = Reflect.Construct( fixtureType );
101 if (this.fixtureSetUp != null)
102 Reflect.InvokeMethod(fixtureSetUp, Fixture);
103 IsSetUp = true;
105 catch (Exception ex)
107 // Error in TestFixtureSetUp causes the suite and
108 // all contained suites to be ignored.
109 // TODO: Change this to be a failure?
110 NunitException nex = ex as NunitException;
111 if (nex != null)
112 ex = nex.InnerException;
114 if ( ex is NUnit.Framework.IgnoreException )
116 this.ShouldRun = false;
117 suiteResult.NotRun(ex.Message);
118 suiteResult.StackTrace = ex.StackTrace;
119 this.IgnoreReason = ex.Message;
121 else
123 suiteResult.Failure( ex.Message, ex.StackTrace, true );
126 finally
128 suiteResult.AssertCount = NUnit.Framework.Assert.Counter;
132 public override void DoTearDown( TestResult suiteResult )
134 if (this.ShouldRun)
136 try
138 IsSetUp = false;
139 if (this.fixtureTearDown != null)
140 Reflect.InvokeMethod(fixtureTearDown, Fixture);
142 catch (Exception ex)
144 // Error in TestFixtureTearDown causes the
145 // suite to be marked as a failure, even if
146 // all the contained tests passed.
147 NunitException nex = ex as NunitException;
148 if (nex != null)
149 ex = nex.InnerException;
151 suiteResult.Failure( ex.Message, ex.StackTrace);
153 finally
155 suiteResult.AssertCount += NUnit.Framework.Assert.Counter;
159 if (this.IgnoreReason == FIXTURE_SETUP_FAILED)
161 this.ShouldRun = true;
162 this.IgnoreReason = null;