1 // ***********************************************************************
2 // Copyright (c) 2007-2014 Charlie Poole
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 namespace NUnit.Framework.Interfaces
34 /// The ResultState class represents the outcome of running a test.
35 /// It contains two pieces of information. The Status of the test
36 /// is an enum indicating whether the test passed, failed, was
37 /// skipped or was inconclusive. The Label provides a more
38 /// detailed breakdown for use by client runners.
40 public class ResultState
45 /// Initializes a new instance of the <see cref="ResultState"/> class.
47 /// <param name="status">The TestStatus.</param>
48 public ResultState(TestStatus status) : this (status, string.Empty, FailureSite.Test)
53 /// Initializes a new instance of the <see cref="ResultState"/> class.
55 /// <param name="status">The TestStatus.</param>
56 /// <param name="label">The label.</param>
57 public ResultState(TestStatus status, string label) : this (status, label, FailureSite.Test)
62 /// Initializes a new instance of the <see cref="ResultState"/> class.
64 /// <param name="status">The TestStatus.</param>
65 /// <param name="site">The stage at which the result was produced</param>
66 public ResultState(TestStatus status, FailureSite site) : this(status, string.Empty, site)
71 /// Initializes a new instance of the <see cref="ResultState"/> class.
73 /// <param name="status">The TestStatus.</param>
74 /// <param name="label">The label.</param>
75 /// <param name="site">The stage at which the result was produced</param>
76 public ResultState(TestStatus status, string label, FailureSite site)
79 Label = label == null ? string.Empty : label;
85 #region Predefined ResultStates
88 /// The result is inconclusive
90 public readonly static ResultState Inconclusive = new ResultState(TestStatus.Inconclusive);
93 /// The test has been skipped.
95 public readonly static ResultState Skipped = new ResultState(TestStatus.Skipped);
98 /// The test has been ignored.
100 public readonly static ResultState Ignored = new ResultState(TestStatus.Skipped, "Ignored");
103 /// The test was skipped because it is explicit
105 public readonly static ResultState Explicit = new ResultState(TestStatus.Skipped, "Explicit");
108 /// The test succeeded
110 public readonly static ResultState Success = new ResultState(TestStatus.Passed);
115 public readonly static ResultState Failure = new ResultState(TestStatus.Failed);
118 /// The test encountered an unexpected exception
120 public readonly static ResultState Error = new ResultState(TestStatus.Failed, "Error");
123 /// The test was cancelled by the user
125 public readonly static ResultState Cancelled = new ResultState(TestStatus.Failed, "Cancelled");
128 /// The test was not runnable.
130 public readonly static ResultState NotRunnable = new ResultState(TestStatus.Failed, "Invalid");
133 /// A suite failed because one or more child tests failed or had errors
135 public readonly static ResultState ChildFailure = ResultState.Failure.WithSite(FailureSite.Child);
138 /// A suite failed in its OneTimeSetUp
140 public readonly static ResultState SetUpFailure = ResultState.Failure.WithSite(FailureSite.SetUp);
143 /// A suite had an unexpected exception in its OneTimeSetUp
145 public readonly static ResultState SetUpError = ResultState.Error.WithSite(FailureSite.SetUp);
148 /// A suite had an unexpected exception in its OneTimeDown
150 public readonly static ResultState TearDownError = ResultState.Error.WithSite(FailureSite.TearDown);
157 /// Gets the TestStatus for the test.
159 /// <value>The status.</value>
160 public TestStatus Status { get; private set; }
163 /// Gets the label under which this test result is
164 /// categorized, if any.
166 public string Label { get; private set; }
169 /// Gets the stage of test execution in which
170 /// the failure or other result took place.
172 public FailureSite Site { get; private set; }
175 /// Get a new ResultState, which is the same as the current
176 /// one but with the FailureSite set to the specified value.
178 /// <param name="site">The FailureSite to use</param>
179 /// <returns>A new ResultState</returns>
180 public ResultState WithSite(FailureSite site)
182 return new ResultState(this.Status, this.Label, site);
187 #region Equals Override
190 /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
192 /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
194 /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
196 public override bool Equals(object obj)
198 var other = obj as ResultState;
199 if (other == null) return false;
201 return Status.Equals(other.Status) && Label.Equals(other.Label) && Site.Equals(other.Site);
205 /// Returns a hash code for this instance.
208 /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
210 public override int GetHashCode()
212 return (int)Status << 8 + (int)Site ^ Label.GetHashCode(); ;
217 #region ToString Override
220 /// Returns a <see cref="System.String"/> that represents this instance.
223 /// A <see cref="System.String"/> that represents this instance.
225 public override string ToString()
227 var sb = new StringBuilder(Status.ToString());
229 if (Label != null && Label.Length > 0)
230 sb.AppendFormat(":{0}", Label);
231 if (Site != FailureSite.Test)
232 sb.AppendFormat("({0})", Site.ToString());
234 return sb.ToString();
241 /// The FailureSite enum indicates the stage of a test
242 /// in which an error or failure occurred.
244 public enum FailureSite
247 /// Failure in the test itself
252 /// Failure in the SetUp method
257 /// Failure in the TearDown method
262 /// Failure of a parent test
267 /// Failure of a child test