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

« back to all changes in this revision

Viewing changes to src/framework/NUnitLite/TestSuite.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:
1
 
// ***********************************************************************
2
 
// Copyright (c) 2007 Charlie Poole
3
 
//
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:
11
 
// 
12
 
// The above copyright notice and this permission notice shall be
13
 
// included in all copies or substantial portions of the Software.
14
 
// 
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
 
// ***********************************************************************
23
 
 
24
 
using System;
25
 
using System.Collections;
26
 
using System.Reflection;
27
 
using NUnit.Framework;
28
 
 
29
 
namespace NUnitLite
30
 
{
31
 
    /// <summary>
32
 
    /// TestSuite represents a collection of tests
33
 
    /// </summary>
34
 
    public class TestSuite : ITest
35
 
    {
36
 
        #region Instance Variables
37
 
        private string name;
38
 
        private string fullName;
39
 
 
40
 
        private RunState runState = RunState.Runnable;
41
 
        private string ignoreReason;
42
 
 
43
 
        private IDictionary properties = new Hashtable();
44
 
 
45
 
        private NUnit.ObjectList tests = new NUnit.ObjectList();
46
 
        #endregion
47
 
 
48
 
        #region Constructors
49
 
        /// <summary>
50
 
        /// Initializes a new instance of the <see cref="TestSuite"/> class.
51
 
        /// </summary>
52
 
        /// <param name="name">The name of the suite.</param>
53
 
        public TestSuite(string name)
54
 
        {
55
 
            this.name = name;
56
 
        }
57
 
 
58
 
        /// <summary>
59
 
        /// Initializes a new instance of the <see cref="TestSuite"/> class.
60
 
        /// </summary>
61
 
        /// <param name="type">The type used to create the suite.</param>
62
 
        public TestSuite(Type type)
63
 
        {
64
 
            this.name = type.Name;
65
 
            this.fullName = type.FullName;
66
 
        }
67
 
        #endregion
68
 
 
69
 
        #region Properties
70
 
        /// <summary>
71
 
        /// Gets the name of the suite.
72
 
        /// </summary>
73
 
        /// <value>The name.</value>
74
 
        public string Name
75
 
        {
76
 
            get { return name; }
77
 
        }
78
 
 
79
 
        /// <summary>
80
 
        /// Gets the full name of the suite.
81
 
        /// </summary>
82
 
        /// <value>The full name.</value>
83
 
        public string FullName
84
 
        {
85
 
            get { return fullName; }
86
 
        }
87
 
 
88
 
        /// <summary>
89
 
        /// Gets or sets the run state of the suite.
90
 
        /// </summary>
91
 
        /// <value>The run state.</value>
92
 
        public RunState RunState
93
 
        {
94
 
            get { return runState; }
95
 
            set { runState = value; }
96
 
        }
97
 
 
98
 
        /// <summary>
99
 
        /// Gets or sets the ignore reason.
100
 
        /// </summary>
101
 
        /// <value>The ignore reason.</value>
102
 
        public string IgnoreReason
103
 
        {
104
 
            get { return ignoreReason; }
105
 
            set { ignoreReason = value; }
106
 
        }
107
 
 
108
 
        /// <summary>
109
 
        /// Gets the properties collection for this suite.
110
 
        /// </summary>
111
 
        /// <value>The properties.</value>
112
 
        public IDictionary Properties
113
 
        {
114
 
            get { return properties; }
115
 
        }
116
 
 
117
 
        /// <summary>
118
 
        /// Gets the test case count.
119
 
        /// </summary>
120
 
        /// <value>The test case count.</value>
121
 
        public int TestCaseCount
122
 
        {
123
 
            get
124
 
            {
125
 
                int count = 0;
126
 
                foreach (ITest test in this.tests)
127
 
                    count += test.TestCaseCount;
128
 
                return count;
129
 
            }
130
 
        }
131
 
 
132
 
        /// <summary>
133
 
        /// Gets the tests.
134
 
        /// </summary>
135
 
        /// <value>The tests.</value>
136
 
        public IList Tests
137
 
        {
138
 
            get { return tests; }
139
 
        }
140
 
        #endregion
141
 
 
142
 
        #region Public Methods
143
 
        /// <summary>
144
 
        /// Runs the suite.
145
 
        /// </summary>
146
 
        /// <returns>A TestResult</returns>
147
 
        public TestResult Run()
148
 
        {
149
 
            return Run(new NullListener());
150
 
        }
151
 
 
152
 
        /// <summary>
153
 
        /// Runs this test
154
 
        /// </summary>
155
 
        /// <param name="listener">A TestListener to handle test events</param>
156
 
        /// <returns>A TestResult</returns>
157
 
        public TestResult Run(TestListener listener)
158
 
        {
159
 
            int count = 0, failures = 0, errors = 0;
160
 
            listener.TestStarted(this);
161
 
            TestResult result = new TestResult(this);
162
 
 
163
 
            switch (this.RunState)
164
 
            {
165
 
                case RunState.NotRunnable:
166
 
                    result.Error(this.IgnoreReason);
167
 
                    break;
168
 
 
169
 
                case RunState.Ignored:
170
 
                    result.NotRun(this.IgnoreReason);
171
 
                    break;
172
 
 
173
 
                case RunState.Runnable:
174
 
                    foreach (ITest test in tests)
175
 
                    {
176
 
                        ++count;
177
 
                        TestResult r = test.Run(listener);
178
 
                        result.AddResult(r);
179
 
                        switch (r.ResultState)
180
 
                        {
181
 
                            case ResultState.Error:
182
 
                                ++errors;
183
 
                                break;
184
 
                            case ResultState.Failure:
185
 
                                ++failures;
186
 
                                break;
187
 
                            default:
188
 
                                break;
189
 
                        }
190
 
                    }
191
 
 
192
 
                    if (count == 0)
193
 
                        result.NotRun("Class has no tests");
194
 
                    else if (errors > 0 || failures > 0)
195
 
                        result.Failure("One or more component tests failed");
196
 
                    else
197
 
                        result.Success();
198
 
                    break;
199
 
            }
200
 
 
201
 
            listener.TestFinished(result);
202
 
            return result;
203
 
        }
204
 
 
205
 
        /// <summary>
206
 
        /// Adds the test.
207
 
        /// </summary>
208
 
        /// <param name="test">The test.</param>
209
 
        public void AddTest(ITest test)
210
 
        {
211
 
            tests.Add(test);
212
 
        }
213
 
        #endregion
214
 
    }
215
 
}