2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / nunit20 / core / TestFixture.cs
1 using System;
2 using System.Collections;
3 using System.Reflection;
4
5 namespace NUnit.Core
6 {
7         /// <summary>
8         /// A TestSuite that wraps a class marked with TestFixtureAttribute
9         /// </summary>
10         public class TestFixture : TestSuite
11         {
12                 private const string FIXTURE_SETUP_FAILED = "Fixture setup failed";
13
14                 #region Constructors
15
16                 public TestFixture( object fixture ) : base( fixture, 0 )
17                 {
18                         Initialize();
19                 }
20
21                 public TestFixture( object fixture, int assemblyKey ) : base( fixture, assemblyKey )
22                 {
23                         Initialize();
24                 }
25
26                 public TestFixture( Type fixtureType ) : base( fixtureType, 0 )
27                 {
28                         Initialize();
29                 }
30
31                 public TestFixture( Type fixtureType, int assemblyKey ) : base( fixtureType, assemblyKey )
32                 {
33                         Initialize();
34                 }
35
36                 private void Initialize()
37                 {
38                         try
39                         {
40                                 Reflect.CheckFixtureType( fixtureType );
41
42                                 IList categories = Reflect.GetCategories( fixtureType );
43                                 CategoryManager.Add( categories );
44                                 this.Categories = categories;
45
46                                 this.fixtureSetUp = Reflect.GetFixtureSetUpMethod( fixtureType );
47                                 this.fixtureTearDown = Reflect.GetFixtureTearDownMethod( fixtureType );
48
49                                 this.IsExplicit = Reflect.HasExplicitAttribute( fixtureType );
50
51                                 if ( Reflect.HasIgnoreAttribute( fixtureType ) )
52                                 {
53                                         this.ShouldRun = false;
54                                         this.IgnoreReason = Reflect.GetIgnoreReason( fixtureType );
55                                 }
56                 
57                                 this.Description = Reflect.GetDescription( fixtureType );
58
59                                 MethodInfo [] methods = fixtureType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static|BindingFlags.NonPublic);
60                                 foreach(MethodInfo method in methods)
61                                 {
62                                         TestCase testCase = TestCaseBuilder.Make( fixtureType, method );
63                                         if(testCase != null)
64                                         {
65                                                 testCase.AssemblyKey = this.AssemblyKey;
66                                                 this.Add( testCase );
67                                         }
68                                 }
69
70                                 if( this.CountTestCases() == 0 )
71                                 {
72                                         this.ShouldRun = false;
73                                         this.IgnoreReason = this.Name + " does not have any tests";
74                                 }
75                         }
76                         catch( InvalidTestFixtureException exception )
77                         {
78                                 this.ShouldRun = false;
79                                 this.IgnoreReason = exception.Message;
80                         }
81                 }
82
83                 #endregion
84
85                 #region Static Methods
86
87                 public static bool IsValidType( Type type )
88                 {
89                         return !type.IsAbstract && Reflect.HasTestFixtureAttribute( type );
90                 }
91
92                 #endregion
93                 
94                 public override void DoSetUp( TestResult suiteResult )
95                 {
96                         try 
97                         {
98                                 if ( Fixture == null )
99                                         Fixture = Reflect.Construct( fixtureType );
100
101                                 if (this.fixtureSetUp != null)
102                                         Reflect.InvokeMethod(fixtureSetUp, Fixture);
103                                 IsSetUp = true;
104                         } 
105                         catch (Exception ex) 
106                         {
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;
113
114                                 if ( ex is NUnit.Framework.IgnoreException )
115                                 {
116                                         this.ShouldRun = false;
117                                         suiteResult.NotRun(ex.Message);
118                                         suiteResult.StackTrace = ex.StackTrace;
119                                         this.IgnoreReason = ex.Message;
120                                 }
121                                 else
122                                 {
123                                         suiteResult.Failure( ex.Message, ex.StackTrace, true );
124                                 }
125                         }
126                         finally
127                         {
128                                 suiteResult.AssertCount = NUnit.Framework.Assert.Counter;
129                         }
130                 }
131
132                 public override void DoTearDown( TestResult suiteResult )
133                 {
134                         if (this.ShouldRun) 
135                         {
136                                 try 
137                                 {
138                                         IsSetUp = false;
139                                         if (this.fixtureTearDown != null)
140                                                 Reflect.InvokeMethod(fixtureTearDown, Fixture);
141                                 } 
142                                 catch (Exception ex) 
143                                 {
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;
150
151                                         suiteResult.Failure( ex.Message, ex.StackTrace);
152                                 }
153                                 finally
154                                 {
155                                         suiteResult.AssertCount += NUnit.Framework.Assert.Counter;
156                                 }
157                         }
158
159                         if (this.IgnoreReason == FIXTURE_SETUP_FAILED) 
160                         {
161                                 this.ShouldRun = true;
162                                 this.IgnoreReason = null;
163                         }
164                 }
165         }
166 }