1 // *********************************************************************** 2 // Copyright (c) 2011 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 NUnit.Framework.Api; 27 using NUnit.Framework.Internal; 28 29 namespace NUnit.Framework 30 { 31 /// <summary> 32 /// Provide the context information of the current test. 33 /// This is an adapter for the internal ExecutionContext 34 /// class, hiding the internals from the user test. 35 /// </summary> 36 public class TestContext 37 { 38 private TestExecutionContext ec; 39 private TestAdapter test; 40 private ResultAdapter result; 41 42 #region Constructor 43 44 /// <summary> 45 /// Construct a TestContext for an ExecutionContext 46 /// </summary> 47 /// <param name="ec">The ExecutionContext to adapt</param> TestContext(TestExecutionContext ec)48 public TestContext(TestExecutionContext ec) 49 { 50 this.ec = ec; 51 } 52 53 #endregion 54 55 #region Properties 56 57 /// <summary> 58 /// Get the current test context. This is created 59 /// as needed. The user may save the context for 60 /// use within a test, but it should not be used 61 /// outside the test for which it is created. 62 /// </summary> 63 public static TestContext CurrentContext 64 { 65 get { return new TestContext(TestExecutionContext.CurrentContext); } 66 } 67 68 /// <summary> 69 /// Get a representation of the current test. 70 /// </summary> 71 public TestAdapter Test 72 { 73 get 74 { 75 if (test == null) 76 test = new TestAdapter(ec.CurrentTest); 77 78 return test; 79 } 80 } 81 82 /// <summary> 83 /// Gets a Representation of the TestResult for the current test. 84 /// </summary> 85 public ResultAdapter Result 86 { 87 get 88 { 89 if (result == null) 90 result = new ResultAdapter(ec.CurrentResult); 91 92 return result; 93 } 94 } 95 96 #if !NETCF 97 /// <summary> 98 /// Gets the directory containing the current test assembly. 99 /// </summary> 100 public string TestDirectory 101 { 102 get 103 { 104 return AssemblyHelper.GetDirectoryName(ec.CurrentTest.FixtureType.Assembly); 105 } 106 } 107 #endif 108 109 /// <summary> 110 /// Gets the directory to be used for outputing files created 111 /// by this test run. 112 /// </summary> 113 public string WorkDirectory 114 { 115 get 116 { 117 return ec.WorkDirectory; 118 } 119 } 120 121 public RandomGenerator Random 122 { 123 get 124 { 125 return ec.RandomGenerator; 126 } 127 } 128 129 #endregion 130 131 #region Nested TestAdapter Class 132 133 /// <summary> 134 /// TestAdapter adapts a Test for consumption by 135 /// the user test code. 136 /// </summary> 137 public class TestAdapter 138 { 139 private Test test; 140 141 #region Constructor 142 143 /// <summary> 144 /// Construct a TestAdapter for a Test 145 /// </summary> 146 /// <param name="test">The Test to be adapted</param> TestAdapter(Test test)147 public TestAdapter(Test test) 148 { 149 this.test = test; 150 } 151 152 #endregion 153 154 #region Properties 155 156 /// <summary> 157 /// Gets the unique Id of a test 158 /// </summary> 159 public int ID 160 { 161 get { return test.Id; } 162 } 163 164 /// <summary> 165 /// The name of the test, which may or may not be 166 /// the same as the method name. 167 /// </summary> 168 public string Name 169 { 170 get 171 { 172 return test.Name; 173 } 174 } 175 176 /// <summary> 177 /// The name of the method representing the test. 178 /// </summary> 179 public string MethodName 180 { 181 get 182 { 183 return test is TestMethod 184 ? ((TestMethod)test).Method.Name 185 : null; 186 } 187 } 188 189 /// <summary> 190 /// The FullName of the test 191 /// </summary> 192 public string FullName 193 { 194 get 195 { 196 return test.FullName; 197 } 198 } 199 200 /// <summary> 201 /// The properties of the test. 202 /// </summary> 203 public IPropertyBag Properties 204 { 205 get 206 { 207 return test.Properties; 208 } 209 } 210 211 #endregion 212 } 213 214 #endregion 215 216 #region Nested ResultAdapter Class 217 218 /// <summary> 219 /// ResultAdapter adapts a TestResult for consumption by 220 /// the user test code. 221 /// </summary> 222 public class ResultAdapter 223 { 224 private TestResult result; 225 226 #region Constructor 227 228 /// <summary> 229 /// Construct a ResultAdapter for a TestResult 230 /// </summary> 231 /// <param name="result">The TestResult to be adapted</param> ResultAdapter(TestResult result)232 public ResultAdapter(TestResult result) 233 { 234 this.result = result; 235 } 236 237 #endregion 238 239 #region Properties 240 241 /// <summary> 242 /// Gets a ResultState representing the outcome of the test. 243 /// </summary> 244 public ResultState Outcome 245 { 246 get 247 { 248 return result.ResultState; 249 } 250 } 251 252 #endregion 253 } 254 255 #endregion 256 } 257 } 258