~a-schlapsi/nunit-3.0/linux-makefile

« back to all changes in this revision

Viewing changes to src/framework/Api/TestController.cs

  • Committer: Andreas Schlapsi
  • Date: 2010-01-23 23:14:05 UTC
  • mfrom: (18.1.137 work)
  • Revision ID: a.schlapsi@gmx.at-20100123231405-17deqoh18nfnbq1j
Merged with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
using System.Collections;
26
26
using System.IO;
27
27
using System.Reflection;
 
28
using NUnit.Framework.Internal;
28
29
 
29
30
namespace NUnit.Framework.Api
30
31
{
 
32
    /// <summary>
 
33
    /// TestController provides a facade for use in loading, browsing 
 
34
    /// and running tests without requiringa reference to the NUnit 
 
35
    /// framework. All calls are encapsulated in constructors for
 
36
    /// this class and its nested classes, which only require the
 
37
    /// types of the Common Type System as arguments.
 
38
    /// </summary>
31
39
    public class TestController : MarshalByRefObject
32
40
    {
33
41
        private ITestAssemblyBuilder builder;
36
44
        #region Constructors
37
45
 
38
46
        /// <summary>
39
 
        /// Construct a TestController using default runner and builder
 
47
        /// Construct a TestController using the default builder and runner.
40
48
        /// </summary>
41
49
        public TestController()
42
50
        {
43
 
            if (!NUnit.Core.CoreExtensions.Host.Initialized)
44
 
                NUnit.Core.CoreExtensions.Host.Initialize();
 
51
            if (!CoreExtensions.Host.Initialized)
 
52
                CoreExtensions.Host.Initialize();
 
53
 
 
54
            // TODO: This should be taken from constructor options
 
55
            InternalTrace.Level = InternalTrace.TraceLevel.Debug;
 
56
            InternalTrace.Open("InternalTrace.txt");
45
57
 
46
58
            this.builder = new DefaultTestAssemblyBuilder();
47
59
            this.runner = new DefaultTestAssemblyRunner(this.builder);
48
 
        } 
 
60
        }
49
61
 
50
62
        /// <summary>
51
63
        /// Construct a TestController, specifying the types to be used
55
67
        /// <param name="builderType">The Type of the test builder</param>
56
68
        public TestController(string runnerType, string builderType)
57
69
        {
58
 
            if (!NUnit.Core.CoreExtensions.Host.Initialized)
59
 
                NUnit.Core.CoreExtensions.Host.Initialize();
 
70
            if (!CoreExtensions.Host.Initialized)
 
71
                CoreExtensions.Host.Initialize();
60
72
 
61
73
            Assembly myAssembly = Assembly.GetExecutingAssembly();
62
74
            this.builder = (ITestAssemblyBuilder)myAssembly.CreateInstance(builderType);
68
80
 
69
81
        #region Properties
70
82
 
 
83
        /// <summary>
 
84
        /// Gets the ITestAssemblyBuilder used by this controller instance.
 
85
        /// </summary>
 
86
        /// <value>The builder.</value>
71
87
        public ITestAssemblyBuilder Builder
72
88
        {
73
89
            get { return builder; }
74
90
        }
75
91
 
 
92
        /// <summary>
 
93
        /// Gets the ITestAssemblyRunner used by this controller instance.
 
94
        /// </summary>
 
95
        /// <value>The runner.</value>
76
96
        public ITestAssemblyRunner Runner
77
97
        {
78
98
            get { return runner; }
103
123
        public abstract class TestControllerAction : MarshalByRefObject
104
124
        {
105
125
            private TestController controller;
106
 
            private ITestAssemblyRunner runner;
107
126
            private AsyncCallback callback;
108
127
 
109
128
            /// <summary>
114
133
            protected TestControllerAction(TestController controller, AsyncCallback callback)
115
134
            {
116
135
                this.controller = controller;
117
 
                this.runner = controller.Runner;
118
136
                this.callback = callback;
119
137
            }
120
138
 
121
139
            /// <summary>
122
 
            /// Gets the runner.
123
 
            /// </summary>
124
 
            /// <value>The runner.</value>
125
 
            protected ITestAssemblyRunner Runner
126
 
            {
127
 
                get { return this.runner; }
128
 
            }
129
 
 
130
 
            /// <summary>
131
 
            /// Reports the progress.
132
 
            /// </summary>
133
 
            /// <param name="progress">The progress.</param>
134
 
            public void ReportProgress(object progress)
135
 
            {
136
 
                callback(new ProgressReport(progress));
137
 
            }
138
 
 
139
 
            /// <summary>
140
140
            /// Reports the result.
141
141
            /// </summary>
142
142
            /// <param name="result">The result.</param>
143
143
            /// <param name="synchronous">if set to <c>true</c> [synchronous].</param>
144
 
            public void ReportResult(object result, bool synchronous)
 
144
            protected void ReportResult(object result, bool synchronous)
145
145
            {
146
146
                callback(new FinalResult(result, synchronous));
147
147
            }
169
169
            /// </summary>
170
170
            /// <param name="controller">The controller.</param>
171
171
            /// <param name="assemblyFilename">The assembly filename.</param>
 
172
            /// <param name="loadOptions">Options controlling how the tests are loaded</param>
172
173
            /// <param name="callback">The callback.</param>
173
 
            public LoadTestsAction(TestController controller, string assemblyFilename, AsyncCallback callback) 
 
174
            public LoadTestsAction(TestController controller, string assemblyFilename, IDictionary loadOptions, AsyncCallback callback) 
174
175
                : base(controller, callback)
175
176
            {
176
 
                ReportResult(Runner.Load(assemblyFilename), true);
 
177
                ReportResult(controller.Runner.Load(assemblyFilename, loadOptions), true);
177
178
            }
178
179
        }
179
180
 
185
186
        /// CountTestsAction counts the number of test cases in the loaded TestSuite
186
187
        /// held by the TestController.
187
188
        /// </summary>
188
 
        public class CountTestsAction : TestControllerAction
189
 
        {
190
 
            /// <summary>
191
 
            /// Construct a CountsTestAction and perform the count of test cases.
192
 
            /// </summary>
193
 
            /// <param name="controller">A TestController holding the TestSuite whose cases are to be counted</param>
194
 
            /// <param name="callback">An AsyncCallback for reporting the count</param>
195
 
            public CountTestsAction(TestController controller, AsyncCallback callback) 
196
 
                : base(controller, callback)
197
 
            {
198
 
                ReportResult(Runner.CountTestCases(NUnit.Core.TestFilter.Empty), true);
199
 
            }
200
 
        }
 
189
        //public class CountTestsAction : TestControllerAction
 
190
        //{
 
191
        //    /// <summary>
 
192
        //    /// Construct a CountsTestAction and perform the count of test cases.
 
193
        //    /// </summary>
 
194
        //    /// <param name="controller">A TestController holding the TestSuite whose cases are to be counted</param>
 
195
        //    /// <param name="callback">An AsyncCallback for reporting the count</param>
 
196
        //    public CountTestsAction(TestController controller, AsyncCallback callback) 
 
197
        //        : base(controller, callback)
 
198
        //    {
 
199
        //        ReportResult(Runner.CountTestCases(TestFilter.Empty), true);
 
200
        //    }
 
201
        //}
201
202
 
202
203
        #endregion
203
204
 
206
207
        /// <summary>
207
208
        /// RunTestsAction runs the loaded TestSuite held by the TestController.
208
209
        /// </summary>
209
 
        public class RunTestsAction : TestControllerAction, NUnit.Core.ITestListener
 
210
        public class RunTestsAction : TestControllerAction
210
211
        {
211
212
            /// <summary>
212
213
            /// Construct a RunTestsAction and run all tests in the loaded TestSuite.
216
217
            public RunTestsAction(TestController controller, AsyncCallback callback) 
217
218
                : base(controller, callback)
218
219
            {
219
 
                ReportResult(Runner.Run(this, NUnit.Core.TestFilter.Empty), true);
220
 
            }
221
 
 
222
 
            /// <summary>
223
 
            /// Construct a RunTestsAction and run tests in the loaded TestSuite that pass the supplied filter
224
 
            /// </summary>
225
 
            /// <param name="controller">A TestController holding the TestSuite to run</param>
226
 
            /// <param name="filter">A TestFilter used to determine which tests should be run</param>
227
 
            /// <param name="result">A callback used to report results</param>
228
 
            public RunTestsAction(TestController controller, NUnit.Core.TestFilter filter, AsyncCallback callback) 
229
 
                : base(controller, callback)
230
 
            {
231
 
                ReportResult(Runner.Run(this, filter), true);
232
 
            }
233
 
 
234
 
            #region ITestListener Members
235
 
 
236
 
            public void RunStarted(NUnit.Core.TestName testName, int testCount)
237
 
            {
238
 
            }
239
 
 
240
 
            public void RunFinished(NUnit.Core.TestResult result)
241
 
            {
242
 
            }
243
 
 
244
 
            public void RunFinished(Exception exception)
245
 
            {
246
 
            }
247
 
 
248
 
            public void TestStarted(NUnit.Core.TestName testName)
249
 
            {
250
 
            }
251
 
 
252
 
            public void TestFinished(NUnit.Core.TestResult result)
253
 
            {
254
 
            }
255
 
 
256
 
            public void SuiteStarted(NUnit.Core.TestName testName)
257
 
            {
258
 
            }
259
 
 
260
 
            public void SuiteFinished(NUnit.Core.TestResult result)
261
 
            {
262
 
            }
263
 
 
264
 
            public void UnhandledException(Exception exception)
265
 
            {
266
 
            }
267
 
 
268
 
            public void TestOutput(NUnit.Core.TestOutput testOutput)
269
 
            {
270
 
                ReportProgress(testOutput);
271
 
            }
272
 
 
273
 
            #endregion
274
 
        }
275
 
 
276
 
        #endregion
277
 
 
278
 
        #endregion
279
 
 
280
 
        #region Nested Classes for use with Callbacks
281
 
 
282
 
        /// <summary>
283
 
        /// AsyncResult is the abstract base for all callback classes
284
 
        /// used with the framework.
285
 
        /// </summary>
286
 
        [Serializable]
287
 
        public abstract class AsyncResult : IAsyncResult
288
 
        {
289
 
            private object state;
290
 
            private bool synchronous;
291
 
            private bool isCompleted;
292
 
 
293
 
            /// <summary>
294
 
            /// Initializes a new instance of the <see cref="AsyncResult"/> class.
295
 
            /// </summary>
296
 
            /// <param name="state">The state of the result.</param>
297
 
            /// <param name="isCompleted">if set to <c>true</c> [is completed].</param>
298
 
            /// <param name="synchronous">if set to <c>true</c> [synchronous].</param>
299
 
            public AsyncResult(object state, bool isCompleted, bool synchronous)
300
 
            {
301
 
                this.state = state;
302
 
                this.isCompleted = isCompleted;
303
 
                this.synchronous = synchronous;
304
 
            }
305
 
 
306
 
            #region IAsyncResult Members
307
 
 
308
 
            /// <summary>
309
 
            /// Gets the result of an operation.
310
 
            /// </summary>
311
 
            /// <value></value>
312
 
            /// <returns>The result state.</returns>
313
 
            public object AsyncState
314
 
            {
315
 
                get { return state; }
316
 
            }
317
 
 
318
 
            /// <summary>
319
 
            /// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
320
 
            /// </summary>
321
 
            /// <returns>A WaitHandle</returns>
322
 
            public System.Threading.WaitHandle AsyncWaitHandle
323
 
            {
324
 
                get { throw new NotImplementedException(); }
325
 
            }
326
 
 
327
 
            /// <summary>
328
 
            /// Gets a value that indicates whether the asynchronous operation completed synchronously.
329
 
            /// </summary>
330
 
            /// <returns>true if the operation completed synchronously; otherwise, false.
331
 
            /// </returns>
332
 
            public bool CompletedSynchronously
333
 
            {
334
 
                get { return synchronous; }
335
 
            }
336
 
 
337
 
            public bool IsCompleted
338
 
            {
339
 
                get { return isCompleted; }
340
 
            }
341
 
 
342
 
            #endregion
343
 
        }
344
 
 
345
 
        [Serializable]
346
 
        public class FinalResult : AsyncResult
347
 
        {
348
 
            public FinalResult(object state, bool synchronous) : base(state, true, synchronous) { }
349
 
        }
350
 
 
351
 
        [Serializable]
352
 
        public class ProgressReport : AsyncResult
353
 
        {
354
 
            public ProgressReport(object report) : base(report, false, false) { }
355
 
        }
 
220
                ITestResult result = controller.Runner.Run(new TestProgressReporter(callback));
 
221
                ReportResult(result.ToXml(true), true);
 
222
            }
 
223
 
 
224
            ///// <summary>
 
225
            ///// Construct a RunTestsAction and run tests in the loaded TestSuite that pass the supplied filter
 
226
            ///// </summary>
 
227
            ///// <param name="controller">A TestController holding the TestSuite to run</param>
 
228
            ///// <param name="filter">A TestFilter used to determine which tests should be run</param>
 
229
            ///// <param name="result">A callback used to report results</param>
 
230
            //public RunTestsAction(TestController controller, TestFilter filter, AsyncCallback callback) 
 
231
            //    : base(controller, callback)
 
232
            //{
 
233
            //    ReportResult(Runner.Run(this, filter), true);
 
234
            //}
 
235
        }
 
236
 
 
237
        #endregion
356
238
 
357
239
        #endregion
358
240
    }