2 using System
.Collections
;
3 using System
.Reflection
;
8 /// A TestSuite that wraps a class marked with TestFixtureAttribute
10 public class TestFixture
: TestSuite
12 private const string FIXTURE_SETUP_FAILED
= "Fixture setup failed";
16 public TestFixture( object fixture
) : base( fixture
, 0 )
21 public TestFixture( object fixture
, int assemblyKey
) : base( fixture
, assemblyKey
)
26 public TestFixture( Type fixtureType
) : base( fixtureType
, 0 )
31 public TestFixture( Type fixtureType
, int assemblyKey
) : base( fixtureType
, assemblyKey
)
36 private void Initialize()
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
);
65 testCase
.AssemblyKey
= this.AssemblyKey
;
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
;
85 #region Static Methods
87 public static bool IsValidType( Type type
)
89 return !type
.IsAbstract
&& Reflect
.HasTestFixtureAttribute( type
);
94 public override void DoSetUp( TestResult suiteResult
)
98 if ( Fixture
== null )
99 Fixture
= Reflect
.Construct( fixtureType
);
101 if (this.fixtureSetUp
!= null)
102 Reflect
.InvokeMethod(fixtureSetUp
, Fixture
);
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
;
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
;
123 suiteResult
.Failure( ex
.Message
, ex
.StackTrace
, true );
128 suiteResult
.AssertCount
= NUnit
.Framework
.Assert
.Counter
;
132 public override void DoTearDown( TestResult suiteResult
)
139 if (this.fixtureTearDown
!= null)
140 Reflect
.InvokeMethod(fixtureTearDown
, Fixture
);
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
;
149 ex
= nex
.InnerException
;
151 suiteResult
.Failure( ex
.Message
, ex
.StackTrace
);
155 suiteResult
.AssertCount
+= NUnit
.Framework
.Assert
.Counter
;
159 if (this.IgnoreReason
== FIXTURE_SETUP_FAILED
)
161 this.ShouldRun
= true;
162 this.IgnoreReason
= null;