1 // *********************************************************************** 2 // Copyright (c) 2009 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.Collections; 25 using NUnit.Framework.Api; 26 using NUnit.Framework.Internal; 27 using NUnit.TestData.TestCaseSourceAttributeFixture; 28 using NUnit.TestUtilities; 29 30 namespace NUnit.Framework.Tests 31 { 32 [TestFixture] 33 public class TestCaseSourceTests 34 { 35 [Test, TestCaseSource("StaticProperty")] SourceCanBeStaticProperty(string source)36 public void SourceCanBeStaticProperty(string source) 37 { 38 Assert.AreEqual("StaticProperty", source); 39 } 40 41 internal static IEnumerable StaticProperty 42 { 43 get { return new object[] { new object[] { "StaticProperty" } }; } 44 } 45 46 [Test, TestCaseSource("InstanceProperty")] SourceCanBeInstanceProperty(string source)47 public void SourceCanBeInstanceProperty(string source) 48 { 49 Assert.AreEqual("InstanceProperty", source); 50 } 51 52 internal IEnumerable InstanceProperty 53 { 54 get { return new object[] { new object[] { "InstanceProperty" } }; } 55 } 56 57 [Test, TestCaseSource("StaticMethod")] SourceCanBeStaticMethod(string source)58 public void SourceCanBeStaticMethod(string source) 59 { 60 Assert.AreEqual("StaticMethod", source); 61 } 62 StaticMethod()63 internal static IEnumerable StaticMethod() 64 { 65 return new object[] { new object[] { "StaticMethod" } }; 66 } 67 68 [Test, TestCaseSource("InstanceMethod")] SourceCanBeInstanceMethod(string source)69 public void SourceCanBeInstanceMethod(string source) 70 { 71 Assert.AreEqual("InstanceMethod", source); 72 } 73 InstanceMethod()74 internal IEnumerable InstanceMethod() 75 { 76 return new object[] { new object[] { "InstanceMethod" } }; 77 } 78 79 [Test, TestCaseSource("StaticField")] SourceCanBeStaticField(string source)80 public void SourceCanBeStaticField(string source) 81 { 82 Assert.AreEqual("StaticField", source); 83 } 84 85 internal static object[] StaticField = 86 { new object[] { "StaticField" } }; 87 88 [Test, TestCaseSource("InstanceField")] SourceCanBeInstanceField(string source)89 public void SourceCanBeInstanceField(string source) 90 { 91 Assert.AreEqual("InstanceField", source); 92 } 93 94 internal static object[] InstanceField = 95 { new object[] { "InstanceField" } }; 96 97 #if CLR_2_0 || CLR_4_0 98 [Test, TestCaseSource(typeof(DataSourceClass))] SourceCanBeInstanceOfIEnumerable(string source)99 public void SourceCanBeInstanceOfIEnumerable(string source) 100 { 101 Assert.AreEqual("DataSourceClass", source); 102 } 103 104 internal class DataSourceClass : IEnumerable 105 { GetEnumerator()106 public IEnumerator GetEnumerator() 107 { 108 yield return "DataSourceClass"; 109 } 110 } 111 #endif 112 113 [Test, TestCaseSource("MyData")] SourceMayReturnArgumentsAsObjectArray(int n, int d, int q)114 public void SourceMayReturnArgumentsAsObjectArray(int n, int d, int q) 115 { 116 Assert.AreEqual(q, n / d); 117 } 118 119 [TestCaseSource("MyData")] TestAttributeIsOptional(int n, int d, int q)120 public void TestAttributeIsOptional(int n, int d, int q) 121 { 122 Assert.AreEqual(q, n / d); 123 } 124 125 [Test, TestCaseSource("MyIntData")] SourceMayReturnArgumentsAsIntArray(int n, int d, int q)126 public void SourceMayReturnArgumentsAsIntArray(int n, int d, int q) 127 { 128 Assert.AreEqual(q, n / d); 129 } 130 131 [Test, TestCaseSource("EvenNumbers")] SourceMayReturnSinglePrimitiveArgumentAlone(int n)132 public void SourceMayReturnSinglePrimitiveArgumentAlone(int n) 133 { 134 Assert.AreEqual(0, n % 2); 135 } 136 137 [Test, TestCaseSource("Params")] SourceMayReturnArgumentsAsParamSet(int n, int d)138 public int SourceMayReturnArgumentsAsParamSet(int n, int d) 139 { 140 return n / d; 141 } 142 143 [Test] 144 [TestCaseSource("MyData")] 145 [TestCaseSource("MoreData", Category="Extra")] 146 [TestCase(12, 0, 0, ExpectedException = typeof(System.DivideByZeroException))] TestMayUseMultipleSourceAttributes(int n, int d, int q)147 public void TestMayUseMultipleSourceAttributes(int n, int d, int q) 148 { 149 Assert.AreEqual(q, n / d); 150 } 151 152 [Test, TestCaseSource("FourArgs")] TestWithFourArguments(int n, int d, int q, int r)153 public void TestWithFourArguments(int n, int d, int q, int r) 154 { 155 Assert.AreEqual(q, n / d); 156 Assert.AreEqual(r, n % d); 157 } 158 159 #if CLR_2_0 || CLR_4_0 160 [Test, TestCaseSource(typeof(DivideDataProvider), "HereIsTheData")] 161 //[Category("Top")] SourceMayBeInAnotherClass(int n, int d, int q)162 public void SourceMayBeInAnotherClass(int n, int d, int q) 163 { 164 Assert.AreEqual(q, n / d); 165 } 166 #endif 167 168 [Test, TestCaseSource(typeof(DivideDataProviderWithReturnValue), "TestCases")] SourceMayBeInAnotherClassWithReturn(int n, int d)169 public int SourceMayBeInAnotherClassWithReturn(int n, int d) 170 { 171 return n / d; 172 } 173 174 [Test] CanSpecifyExpectedException()175 public void CanSpecifyExpectedException() 176 { 177 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 178 typeof(TestCaseSourceAttributeFixture), "MethodThrowsExpectedException").Children[0]; 179 Assert.AreEqual(ResultState.Success, result.ResultState); 180 } 181 182 [Test] CanSpecifyExpectedException_WrongException()183 public void CanSpecifyExpectedException_WrongException() 184 { 185 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 186 typeof(TestCaseSourceAttributeFixture), "MethodThrowsWrongException").Children[0]; 187 Assert.AreEqual(ResultState.Failure, result.ResultState); 188 Assert.That(result.Message, Is.StringStarting("An unexpected exception type was thrown")); 189 } 190 191 [Test] CanSpecifyExpectedException_NoneThrown()192 public void CanSpecifyExpectedException_NoneThrown() 193 { 194 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 195 typeof(TestCaseSourceAttributeFixture), "MethodThrowsNoException").Children[0]; 196 Assert.AreEqual(ResultState.Failure, result.ResultState); 197 Assert.AreEqual("System.ArgumentNullException was expected", result.Message); 198 } 199 200 [Test] IgnoreTakesPrecedenceOverExpectedException()201 public void IgnoreTakesPrecedenceOverExpectedException() 202 { 203 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 204 typeof(TestCaseSourceAttributeFixture), "MethodCallsIgnore").Children[0]; 205 Assert.AreEqual(ResultState.Ignored, result.ResultState); 206 Assert.AreEqual("Ignore this", result.Message); 207 } 208 209 [Test] CanIgnoreIndividualTestCases()210 public void CanIgnoreIndividualTestCases() 211 { 212 TestSuite test = (TestSuite)TestBuilder.MakeTestCase( 213 typeof(TestCaseSourceAttributeFixture), "MethodWithIgnoredTestCases"); 214 215 Test testCase = TestFinder.MustFind("MethodWithIgnoredTestCases(1)", test, false); 216 Assert.That(testCase.RunState, Is.EqualTo(RunState.Runnable)); 217 218 testCase = TestFinder.MustFind("MethodWithIgnoredTestCases(2)", test, false); 219 Assert.That(testCase.RunState, Is.EqualTo(RunState.Ignored)); 220 221 testCase = TestFinder.MustFind("MethodWithIgnoredTestCases(3)", test, false); 222 Assert.That(testCase.RunState, Is.EqualTo(RunState.Ignored)); 223 Assert.That(testCase.Properties.GetSetting(PropertyNames.SkipReason, ""), Is.EqualTo("Don't Run Me!")); 224 } 225 226 [Test] CanMarkIndividualTestCasesExplicit()227 public void CanMarkIndividualTestCasesExplicit() 228 { 229 TestSuite test = (TestSuite)TestBuilder.MakeTestCase( 230 typeof(TestCaseSourceAttributeFixture), "MethodWithExplicitTestCases"); 231 232 Test testCase = TestFinder.Find("MethodWithExplicitTestCases(1)", test, false); 233 Assert.That(testCase.RunState, Is.EqualTo(RunState.Runnable)); 234 235 testCase = TestFinder.Find("MethodWithExplicitTestCases(2)", test, false); 236 Assert.That(testCase.RunState, Is.EqualTo(RunState.Explicit)); 237 238 testCase = TestFinder.Find("MethodWithExplicitTestCases(3)", test, false); 239 Assert.That(testCase.RunState, Is.EqualTo(RunState.Explicit)); 240 Assert.That(testCase.Properties.GetSetting(PropertyNames.SkipReason, ""), Is.EqualTo("Connection failing")); 241 } 242 243 #if CLR_2_0 || CLR_4_0 244 [Test] HandlesExceptionInTestCaseSource()245 public void HandlesExceptionInTestCaseSource() 246 { 247 Test test = (Test)TestBuilder.MakeParameterizedMethodSuite( 248 typeof(TestCaseSourceAttributeFixture), "MethodWithSourceThrowingException").Tests[0]; 249 Assert.AreEqual(RunState.NotRunnable, test.RunState); 250 ITestResult result = TestBuilder.RunTest(test, null); 251 Assert.AreEqual(ResultState.NotRunnable, result.ResultState); 252 Assert.AreEqual("System.Exception : my message", result.Message); 253 } 254 #endif 255 256 #if !NUNITLITE 257 [TestCaseSource("exception_source"), Explicit] HandlesExceptioninTestCaseSource_GuiDisplay(string lhs, string rhs)258 public void HandlesExceptioninTestCaseSource_GuiDisplay(string lhs, string rhs) 259 { 260 Assert.AreEqual(lhs, rhs); 261 } 262 #endif 263 264 internal object[] testCases = 265 { 266 new TestCaseData( 267 new string[] { "A" }, 268 new string[] { "B" }) 269 }; 270 271 [Test, TestCaseSource("testCases")] MethodTakingTwoStringArrays(string[] a, string[] b)272 public void MethodTakingTwoStringArrays(string[] a, string[] b) 273 { 274 Assert.That(a, Is.TypeOf(typeof(string[]))); 275 Assert.That(b, Is.TypeOf(typeof(string[]))); 276 } 277 278 #region Sources used by the tests 279 internal static object[] MyData = new object[] { 280 new object[] { 12, 3, 4 }, 281 new object[] { 12, 4, 3 }, 282 new object[] { 12, 6, 2 } }; 283 284 internal static object[] MyIntData = new object[] { 285 new int[] { 12, 3, 4 }, 286 new int[] { 12, 4, 3 }, 287 new int[] { 12, 6, 2 } }; 288 289 internal static object[] FourArgs = new object[] { 290 new TestCaseData( 12, 3, 4, 0 ), 291 new TestCaseData( 12, 4, 3, 0 ), 292 new TestCaseData( 12, 5, 2, 2 ) }; 293 294 internal static int[] EvenNumbers = new int[] { 2, 4, 6, 8 }; 295 296 internal static object[] MoreData = new object[] { 297 new object[] { 12, 1, 12 }, 298 new object[] { 12, 2, 6 } }; 299 300 internal static object[] Params = new object[] { 301 new TestCaseData(24, 3).Returns(8), 302 new TestCaseData(24, 2).Returns(12) }; 303 304 #if CLR_2_0 || CLR_4_0 305 public class DivideDataProvider 306 { 307 public static IEnumerable HereIsTheData 308 { 309 get 310 { 311 yield return new TestCaseData(0, 0, 0) 312 .SetName("ThisOneShouldThrow") 313 .SetDescription("Demonstrates use of ExpectedException") 314 .SetCategory("Junk") 315 .SetProperty("MyProp", "zip") 316 .Throws(typeof(System.DivideByZeroException)); 317 yield return new object[] { 100, 20, 5 }; 318 yield return new object[] { 100, 4, 25 }; 319 } 320 } 321 } 322 #endif 323 324 public class DivideDataProviderWithReturnValue 325 { 326 public static IEnumerable TestCases 327 { 328 get 329 { 330 return new object[] { 331 new TestCaseData(12, 3).Returns(5).Throws(typeof(AssertionException)).SetName("TC1"), 332 new TestCaseData(12, 2).Returns(6).SetName("TC2"), 333 new TestCaseData(12, 4).Returns(3).SetName("TC3") 334 }; 335 } 336 } 337 } 338 339 #if CLR_2_0 || CLR_4_0 340 internal static IEnumerable exception_source 341 { 342 get 343 { 344 yield return new TestCaseData("a", "a"); 345 yield return new TestCaseData("b", "b"); 346 347 throw new System.Exception("my message"); 348 } 349 } 350 #endif 351 352 #endregion 353 } 354 } 355