1 // *********************************************************************** 2 // Copyright (c) 2008 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 NUnit.Framework.Api; 26 using NUnit.Framework.Internal; 27 using NUnit.TestData.TestCaseAttributeFixture; 28 using NUnit.TestUtilities; 29 using System.Collections; 30 31 namespace NUnit.Framework.Tests 32 { 33 [TestFixture] 34 public class TestCaseAttributeTests 35 { 36 [TestCase(12, 3, 4)] 37 [TestCase(12, 2, 6)] 38 [TestCase(12, 4, 3)] 39 [TestCase(12, 0, 0, ExpectedException = typeof(System.DivideByZeroException))] 40 [TestCase(12, 0, 0, ExpectedExceptionName = "System.DivideByZeroException")] IntegerDivisionWithResultPassedToTest(int n, int d, int q)41 public void IntegerDivisionWithResultPassedToTest(int n, int d, int q) 42 { 43 Assert.AreEqual(q, n / d); 44 } 45 46 [TestCase(12, 3, ExpectedResult = 4)] 47 [TestCase(12, 2, ExpectedResult = 6)] 48 [TestCase(12, 4, ExpectedResult = 3)] 49 [TestCase(12, 0, ExpectedException = typeof(System.DivideByZeroException))] 50 [TestCase(12, 0, ExpectedExceptionName = "System.DivideByZeroException", 51 TestName = "DivisionByZeroThrowsException")] IntegerDivisionWithResultCheckedByNUnit(int n, int d)52 public int IntegerDivisionWithResultCheckedByNUnit(int n, int d) 53 { 54 return n / d; 55 } 56 57 [TestCase(2, 2, ExpectedResult=4)] CanConvertIntToDouble(double x, double y)58 public double CanConvertIntToDouble(double x, double y) 59 { 60 return x + y; 61 } 62 63 [TestCase("2.2", "3.3", ExpectedResult = 5.5)] CanConvertStringToDecimal(decimal x, decimal y)64 public decimal CanConvertStringToDecimal(decimal x, decimal y) 65 { 66 return x + y; 67 } 68 69 [TestCase(2.2, 3.3, ExpectedResult = 5.5)] CanConvertDoubleToDecimal(decimal x, decimal y)70 public decimal CanConvertDoubleToDecimal(decimal x, decimal y) 71 { 72 return x + y; 73 } 74 75 [TestCase(5, 2, ExpectedResult = 7)] CanConvertIntToDecimal(decimal x, decimal y)76 public decimal CanConvertIntToDecimal(decimal x, decimal y) 77 { 78 return x + y; 79 } 80 81 [TestCase(5, 2, ExpectedResult = 7)] CanConvertSmallIntsToShort(short x, short y)82 public short CanConvertSmallIntsToShort(short x, short y) 83 { 84 return (short)(x + y); 85 } 86 87 [TestCase(5, 2, ExpectedResult = 7)] CanConvertSmallIntsToByte(byte x, byte y)88 public byte CanConvertSmallIntsToByte(byte x, byte y) 89 { 90 return (byte)(x + y); 91 } 92 93 [TestCase(5, 2, ExpectedResult = 7)] CanConvertSmallIntsToSByte(sbyte x, sbyte y)94 public sbyte CanConvertSmallIntsToSByte(sbyte x, sbyte y) 95 { 96 return (sbyte)(x + y); 97 } 98 99 [Test] ConversionOverflowMakesTestNotRunnable()100 public void ConversionOverflowMakesTestNotRunnable() 101 { 102 Test test = (Test)TestBuilder.MakeParameterizedMethodSuite( 103 typeof(TestCaseAttributeFixture), "MethodCausesConversionOverflow").Tests[0]; 104 Assert.AreEqual(RunState.NotRunnable, test.RunState); 105 } 106 107 [TestCase("12-October-1942")] CanConvertStringToDateTime(DateTime dt)108 public void CanConvertStringToDateTime(DateTime dt) 109 { 110 Assert.AreEqual(1942, dt.Year); 111 } 112 113 [TestCase(42, ExpectedException = typeof(System.Exception), 114 ExpectedMessage = "Test Exception")] CanSpecifyExceptionMessage(int a)115 public void CanSpecifyExceptionMessage(int a) 116 { 117 throw new System.Exception("Test Exception"); 118 } 119 120 [TestCase(42, ExpectedException = typeof(System.Exception), 121 ExpectedMessage = "Test Exception", 122 MatchType=MessageMatch.StartsWith)] CanSpecifyExceptionMessageAndMatchType(int a)123 public void CanSpecifyExceptionMessageAndMatchType(int a) 124 { 125 throw new System.Exception("Test Exception thrown here"); 126 } 127 128 #if CLR_2_0 || CLR_4_0 129 [TestCase(null)] CanPassNullAsFirstArgument(object a)130 public void CanPassNullAsFirstArgument(object a) 131 { 132 Assert.IsNull(a); 133 } 134 #endif 135 136 [TestCase(new object[] { 1, "two", 3.0 })] 137 [TestCase(new object[] { "zip" })] CanPassObjectArrayAsFirstArgument(object[] a)138 public void CanPassObjectArrayAsFirstArgument(object[] a) 139 { 140 } 141 142 [TestCase(new object[] { "a", "b" })] CanPassArrayAsArgument(object[] array)143 public void CanPassArrayAsArgument(object[] array) 144 { 145 Assert.AreEqual("a", array[0]); 146 Assert.AreEqual("b", array[1]); 147 } 148 149 [TestCase("a", "b")] ArgumentsAreCoalescedInObjectArray(object[] array)150 public void ArgumentsAreCoalescedInObjectArray(object[] array) 151 { 152 Assert.AreEqual("a", array[0]); 153 Assert.AreEqual("b", array[1]); 154 } 155 156 [TestCase(1, "b")] ArgumentsOfDifferentTypeAreCoalescedInObjectArray(object[] array)157 public void ArgumentsOfDifferentTypeAreCoalescedInObjectArray(object[] array) 158 { 159 Assert.AreEqual(1, array[0]); 160 Assert.AreEqual("b", array[1]); 161 } 162 163 #if CLR_2_0 || CLR_4_0 164 [TestCase(ExpectedResult = null)] ResultCanBeNull()165 public object ResultCanBeNull() 166 { 167 return null; 168 } 169 #endif 170 171 [TestCase("a", "b")] HandlesParamsArrayAsSoleArgument(params string[] array)172 public void HandlesParamsArrayAsSoleArgument(params string[] array) 173 { 174 Assert.AreEqual("a", array[0]); 175 Assert.AreEqual("b", array[1]); 176 } 177 178 [TestCase("a")] HandlesParamsArrayWithOneItemAsSoleArgument(params string[] array)179 public void HandlesParamsArrayWithOneItemAsSoleArgument(params string[] array) 180 { 181 Assert.AreEqual("a", array[0]); 182 } 183 184 [TestCase("a", "b", "c", "d")] HandlesParamsArrayAsLastArgument(string s1, string s2, params object[] array)185 public void HandlesParamsArrayAsLastArgument(string s1, string s2, params object[] array) 186 { 187 Assert.AreEqual("a", s1); 188 Assert.AreEqual("b", s2); 189 Assert.AreEqual("c", array[0]); 190 Assert.AreEqual("d", array[1]); 191 } 192 193 [TestCase("a", "b")] HandlesParamsArrayWithNoItemsAsLastArgument(string s1, string s2, params object[] array)194 public void HandlesParamsArrayWithNoItemsAsLastArgument(string s1, string s2, params object[] array) 195 { 196 Assert.AreEqual("a", s1); 197 Assert.AreEqual("b", s2); 198 Assert.AreEqual(0, array.Length); 199 } 200 201 [TestCase("a", "b", "c")] HandlesParamsArrayWithOneItemAsLastArgument(string s1, string s2, params object[] array)202 public void HandlesParamsArrayWithOneItemAsLastArgument(string s1, string s2, params object[] array) 203 { 204 Assert.AreEqual("a", s1); 205 Assert.AreEqual("b", s2); 206 Assert.AreEqual("c", array[0]); 207 } 208 209 [Test] CanSpecifyDescription()210 public void CanSpecifyDescription() 211 { 212 Test test = (Test)TestBuilder.MakeParameterizedMethodSuite( 213 typeof(TestCaseAttributeFixture), "MethodHasDescriptionSpecified").Tests[0]; 214 Assert.AreEqual("My Description", test.Properties.Get(PropertyNames.Description)); 215 } 216 217 [Test] CanSpecifyTestName()218 public void CanSpecifyTestName() 219 { 220 Test test = (Test)TestBuilder.MakeParameterizedMethodSuite( 221 typeof(TestCaseAttributeFixture), "MethodHasTestNameSpecified").Tests[0]; 222 Assert.AreEqual("XYZ", test.Name); 223 Assert.AreEqual("NUnit.TestData.TestCaseAttributeFixture.TestCaseAttributeFixture.XYZ", test.FullName); 224 } 225 226 [Test] CanSpecifyCategory()227 public void CanSpecifyCategory() 228 { 229 Test test = (Test)TestBuilder.MakeTestCase( 230 typeof(TestCaseAttributeFixture), "MethodHasSingleCategory").Tests[0]; 231 IList categories = test.Properties["Category"]; 232 Assert.AreEqual(new string[] { "XYZ" }, categories); 233 } 234 235 [Test] CanSpecifyMultipleCategories()236 public void CanSpecifyMultipleCategories() 237 { 238 Test test = (Test)TestBuilder.MakeTestCase( 239 typeof(TestCaseAttributeFixture), "MethodHasMultipleCategories").Tests[0]; 240 IList categories = test.Properties["Category"]; 241 Assert.AreEqual(new string[] { "X", "Y", "Z" }, categories); 242 } 243 244 [Test] CanSpecifyExpectedException()245 public void CanSpecifyExpectedException() 246 { 247 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 248 typeof(TestCaseAttributeFixture), "MethodThrowsExpectedException").Children[0]; 249 Assert.AreEqual(ResultState.Success, result.ResultState); 250 } 251 252 [Test] CanSpecifyExpectedException_WrongException()253 public void CanSpecifyExpectedException_WrongException() 254 { 255 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 256 typeof(TestCaseAttributeFixture), "MethodThrowsWrongException").Children[0]; 257 Assert.AreEqual(ResultState.Failure, result.ResultState); 258 Assert.That(result.Message, Is.StringStarting("An unexpected exception type was thrown")); 259 } 260 261 [Test] CanSpecifyExpectedException_WrongMessage()262 public void CanSpecifyExpectedException_WrongMessage() 263 { 264 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 265 typeof(TestCaseAttributeFixture), "MethodThrowsExpectedExceptionWithWrongMessage").Children[0]; 266 Assert.AreEqual(ResultState.Failure, result.ResultState); 267 Assert.That(result.Message, Is.StringStarting("The exception message text was incorrect")); 268 } 269 270 [Test] CanSpecifyExpectedException_NoneThrown()271 public void CanSpecifyExpectedException_NoneThrown() 272 { 273 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 274 typeof(TestCaseAttributeFixture), "MethodThrowsNoException").Children[0]; 275 Assert.AreEqual(ResultState.Failure, result.ResultState); 276 Assert.AreEqual("System.ArgumentNullException was expected", result.Message); 277 } 278 279 [Test] IgnoreTakesPrecedenceOverExpectedException()280 public void IgnoreTakesPrecedenceOverExpectedException() 281 { 282 ITestResult result = (ITestResult)TestBuilder.RunTestCase( 283 typeof(TestCaseAttributeFixture), "MethodCallsIgnore").Children[0]; 284 Assert.AreEqual(ResultState.Ignored, result.ResultState); 285 Assert.AreEqual("Ignore this", result.Message); 286 } 287 288 [Test] CanIgnoreIndividualTestCases()289 public void CanIgnoreIndividualTestCases() 290 { 291 TestSuite test = (TestSuite)TestBuilder.MakeTestCase( 292 typeof(TestCaseAttributeFixture), "MethodWithIgnoredTestCases"); 293 294 Test testCase = TestFinder.Find("MethodWithIgnoredTestCases(1)", test, false); 295 Assert.That(testCase.RunState, Is.EqualTo(RunState.Runnable)); 296 297 testCase = TestFinder.Find("MethodWithIgnoredTestCases(2)", test, false); 298 Assert.That(testCase.RunState, Is.EqualTo(RunState.Ignored)); 299 300 testCase = TestFinder.Find("MethodWithIgnoredTestCases(3)", test, false); 301 Assert.That(testCase.RunState, Is.EqualTo(RunState.Ignored)); 302 Assert.That(testCase.Properties.GetSetting(PropertyNames.SkipReason, ""), Is.EqualTo("Don't Run Me!")); 303 } 304 305 [Test] CanMarkIndividualTestCasesExplicit()306 public void CanMarkIndividualTestCasesExplicit() 307 { 308 TestSuite test = (TestSuite)TestBuilder.MakeTestCase( 309 typeof(TestCaseAttributeFixture), "MethodWithExplicitTestCases"); 310 311 Test testCase = TestFinder.Find("MethodWithExplicitTestCases(1)", test, false); 312 Assert.That(testCase.RunState, Is.EqualTo(RunState.Runnable)); 313 314 testCase = TestFinder.Find("MethodWithExplicitTestCases(2)", test, false); 315 Assert.That(testCase.RunState, Is.EqualTo(RunState.Explicit)); 316 317 testCase = TestFinder.Find("MethodWithExplicitTestCases(3)", test, false); 318 Assert.That(testCase.RunState, Is.EqualTo(RunState.Explicit)); 319 Assert.That(testCase.Properties.GetSetting(PropertyNames.SkipReason, ""), Is.EqualTo("Connection failing")); 320 } 321 } 322 } 323