Best Nunit code snippet using NUnit.Framework.Attributes.DataSourceClass.HandlesExceptionInTestCaseSource_GuiDisplay
TestCaseSourceTests.cs
Source:TestCaseSourceTests.cs  
1// ***********************************************************************2// Copyright (c) 2009 Charlie Poole3//4// Permission is hereby granted, free of charge, to any person obtaining5// a copy of this software and associated documentation files (the6// "Software"), to deal in the Software without restriction, including7// without limitation the rights to use, copy, modify, merge, publish,8// distribute, sublicense, and/or sell copies of the Software, and to9// permit persons to whom the Software is furnished to do so, subject to10// the following conditions:11// 12// The above copyright notice and this permission notice shall be13// 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 OF17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.22// ***********************************************************************23using System.Collections;24using System.Collections.Generic;25using System.Reflection;26using NUnit.Framework.Interfaces;27using NUnit.Framework.Internal;28using NUnit.TestData.TestCaseSourceAttributeFixture;29using NUnit.TestUtilities;30namespace NUnit.Framework.Attributes31{32    [TestFixture]33    public class TestCaseSourceTests34    {35        [Test, TestCaseSource("StaticProperty")]36        public void SourceCanBeStaticProperty(string source)37        {38            Assert.AreEqual("StaticProperty", source);39        }40        static IEnumerable StaticProperty41        {42            get { return new object[] { new object[] { "StaticProperty" } }; }43        }44        [Test, TestCaseSource("InstanceProperty")]45        public void SourceCanBeInstanceProperty(string source)46        {47            Assert.AreEqual("InstanceProperty", source);48        }49        IEnumerable InstanceProperty50        {51            get { return new object[] { new object[] { "InstanceProperty" } }; }52        }53        [Test, TestCaseSource("StaticMethod")]54        public void SourceCanBeStaticMethod(string source)55        {56            Assert.AreEqual("StaticMethod", source);57        }58        static IEnumerable StaticMethod()59        {60            return new object[] { new object[] { "StaticMethod" } };61        }62        [Test, TestCaseSource("InstanceMethod")]63        public void SourceCanBeInstanceMethod(string source)64        {65            Assert.AreEqual("InstanceMethod", source);66        }67        IEnumerable InstanceMethod()68        {69            return new object[] { new object[] { "InstanceMethod" } };70        }71        [Test, TestCaseSource("StaticField")]72        public void SourceCanBeStaticField(string source)73        {74            Assert.AreEqual("StaticField", source);75        }76        static object[] StaticField =77            { new object[] { "StaticField" } };78        [Test, TestCaseSource("InstanceField")]79        public void SourceCanBeInstanceField(string source)80        {81            Assert.AreEqual("InstanceField", source);82        }83        static object[] InstanceField =84            { new object[] { "InstanceField" } };85        [Test, TestCaseSource(typeof(DataSourceClass))]86        public void SourceCanBeInstanceOfIEnumerable(string source)87        {88            Assert.AreEqual("DataSourceClass", source);89        }90        [Test, TestCaseSource(typeof(DataSourceClass), null, "foo")]91        public void SourceCanBeInstanceOfIEnumerableWithParameters(string source)92        {93            Assert.AreEqual("foo", source);94        }95        class DataSourceClass : IEnumerable96        {97            public DataSourceClass()98            {99            }100            private readonly string enumeratorOverride;101            public DataSourceClass(string enumeratorOverride)102            {103                this.enumeratorOverride = enumeratorOverride;104            }105            public IEnumerator GetEnumerator()106            {107                yield return this.enumeratorOverride ?? "DataSourceClass";108            }109        }110        [Test, TestCaseSource(typeof(DataSourceClassWithMethod), "GetStuff", "foo")]111        public void SourceCanHaveMethodProducingIEnumerableWithParameters(string source)112        {113            string[] items = new[] { "foo1", "foo2", "foo3" };114            bool found = false;115            for (int i = 0; i < items.Length; i++)116            {117                if (items[i] == source)118                {119                    found = true;120                }121            }122            Assert.IsTrue(found);123        }124        class DataSourceClassWithMethod125        {126            private readonly string enumeratorPrepend;127            public DataSourceClassWithMethod(string enumeratorPrepend)128            {129                this.enumeratorPrepend = enumeratorPrepend;130            }131            public IEnumerable<string> GetStuff()132            {133                return new[] { enumeratorPrepend + "1", enumeratorPrepend + "2", enumeratorPrepend + "3" };134            }135        }136        [Test, TestCaseSource("MyData")]137        public void SourceMayReturnArgumentsAsObjectArray(int n, int d, int q)138        {139            Assert.AreEqual(q, n / d);140        }141        [TestCaseSource("MyData")]142        public void TestAttributeIsOptional(int n, int d, int q)143        {144            Assert.AreEqual(q, n / d);145        }146        [Test, TestCaseSource("MyIntData")]147        public void SourceMayReturnArgumentsAsIntArray(int n, int d, int q)148        {149            Assert.AreEqual(q, n / d);150        }151        [Test, TestCaseSource("EvenNumbers")]152        public void SourceMayReturnSinglePrimitiveArgumentAlone(int n)153        {154            Assert.AreEqual(0, n % 2);155        }156        [Test, TestCaseSource("Params")]157        public int SourceMayReturnArgumentsAsParamSet(int n, int d)158        {159            return n / d;160        }161        [Test]162        [TestCaseSource("MyData")]163        [TestCaseSource("MoreData", Category="Extra")]164        [TestCase(12, 2, 6)]165        public void TestMayUseMultipleSourceAttributes(int n, int d, int q)166        {167            Assert.AreEqual(q, n / d);168        }169        [Test, TestCaseSource("FourArgs")]170        public void TestWithFourArguments(int n, int d, int q, int r)171        {172            Assert.AreEqual(q, n / d);173            Assert.AreEqual(r, n % d);174        }175        [Test, Category("Top"), TestCaseSource(typeof(DivideDataProvider), "HereIsTheData")]176        public void SourceMayBeInAnotherClass(int n, int d, int q)177        {178            Assert.AreEqual(q, n / d);179        }180        [Test, TestCaseSource(typeof(DivideDataProviderWithReturnValue), "TestCases")]181        public int SourceMayBeInAnotherClassWithReturn(int n, int d)182        {183            return n / d;184        }185        [Test]186        public void IgnoreTakesPrecedenceOverExpectedException()187        {188            ITestResult result = TestBuilder.RunParameterizedMethodSuite(189                typeof(TestCaseSourceAttributeFixture), "MethodCallsIgnore").Children[0];190            Assert.AreEqual(ResultState.Ignored, result.ResultState);191            Assert.AreEqual("Ignore this", result.Message);192        }193        [Test]194        public void CanIgnoreIndividualTestCases()195        {196            TestSuite suite = TestBuilder.MakeParameterizedMethodSuite(197                typeof(TestCaseSourceAttributeFixture), "MethodWithIgnoredTestCases");198            Test testCase = TestFinder.Find("MethodWithIgnoredTestCases(1)", suite, false);199            Assert.That(testCase.RunState, Is.EqualTo(RunState.Runnable));200 201            testCase = TestFinder.Find("MethodWithIgnoredTestCases(2)", suite, false);202            Assert.That(testCase.RunState, Is.EqualTo(RunState.Ignored));203 204            testCase = TestFinder.Find("MethodWithIgnoredTestCases(3)", suite, false);205            Assert.That(testCase.RunState, Is.EqualTo(RunState.Ignored));206            Assert.That(testCase.Properties.Get(PropertyNames.SkipReason), Is.EqualTo("Don't Run Me!"));207        }208        [Test]209        public void CanMarkIndividualTestCasesExplicit()210        {211            TestSuite suite = TestBuilder.MakeParameterizedMethodSuite(212                typeof(TestCaseSourceAttributeFixture), "MethodWithExplicitTestCases");213            Test testCase = TestFinder.Find("MethodWithExplicitTestCases(1)", suite, false);214            Assert.That(testCase.RunState, Is.EqualTo(RunState.Runnable));215 216            testCase = TestFinder.Find("MethodWithExplicitTestCases(2)", suite, false);217            Assert.That(testCase.RunState, Is.EqualTo(RunState.Explicit));218 219            testCase = TestFinder.Find("MethodWithExplicitTestCases(3)", suite, false);220            Assert.That(testCase.RunState, Is.EqualTo(RunState.Explicit));221            Assert.That(testCase.Properties.Get(PropertyNames.SkipReason), Is.EqualTo("Connection failing"));222        }223        [Test]224        public void HandlesExceptionInTestCaseSource()225        {226            var testMethod = (TestMethod)TestBuilder.MakeParameterizedMethodSuite(227                typeof(TestCaseSourceAttributeFixture), "MethodWithSourceThrowingException").Tests[0];228            Assert.AreEqual(RunState.NotRunnable, testMethod.RunState);229            ITestResult result = TestBuilder.RunTest(testMethod, null);230            Assert.AreEqual(ResultState.NotRunnable, result.ResultState);231            Assert.AreEqual("System.Exception : my message", result.Message);232        }233        [TestCaseSource("exception_source"), Explicit]234        public void HandlesExceptioninTestCaseSource_GuiDisplay(string lhs, string rhs)235        {236            Assert.AreEqual(lhs, rhs);237        }238        object[] testCases =239        {240            new TestCaseData(241                new string[] { "A" },242                new string[] { "B" })243        };244        [Test, TestCaseSource("testCases")]245        public void MethodTakingTwoStringArrays(string[] a, string[] b)246        {247            Assert.That(a, Is.TypeOf(typeof(string[])));248            Assert.That(b, Is.TypeOf(typeof(string[])));249        }250        #region Sources used by the tests251        static object[] MyData = new object[] {252            new object[] { 12, 3, 4 },253            new object[] { 12, 4, 3 },254            new object[] { 12, 6, 2 } };255        static object[] MyIntData = new object[] {256            new int[] { 12, 3, 4 },257            new int[] { 12, 4, 3 },258            new int[] { 12, 6, 2 } };259        static object[] FourArgs = new object[] {260            new TestCaseData( 12, 3, 4, 0 ),261            new TestCaseData( 12, 4, 3, 0 ),262            new TestCaseData( 12, 5, 2, 2 ) };263        static int[] EvenNumbers = new int[] { 2, 4, 6, 8 };264        static object[] MoreData = new object[] {265            new object[] { 12, 1, 12 },266            new object[] { 12, 2, 6 } };267        static object[] Params = new object[] {268            new TestCaseData(24, 3).Returns(8),269            new TestCaseData(24, 2).Returns(12) };270        private class DivideDataProvider271        {272            public static IEnumerable HereIsTheData273            {274                get275                {276                    //yield return new TestCaseData(0, 0, 0)277                    //    .SetName("ThisOneShouldThrow")278                    //    .SetDescription("Demonstrates use of ExpectedException")279                    //    .SetCategory("Junk")280                    //    .SetProperty("MyProp", "zip")281                    //    .Throws(typeof(System.DivideByZeroException));282                    yield return new object[] { 100, 20, 5 };283                    yield return new object[] { 100, 4, 25 };284                }285            }286        }287        public class DivideDataProviderWithReturnValue288        {289            public static IEnumerable TestCases290            {291                get292                {293                    return new object[] {294                        new TestCaseData(12, 3).Returns(4).SetName("TC1"),295                        new TestCaseData(12, 2).Returns(6).SetName("TC2"),296                        new TestCaseData(12, 4).Returns(3).SetName("TC3")297                    };298                }299            }300        }301        private static IEnumerable exception_source302        {303            get304            {305                yield return new TestCaseData("a", "a");306                yield return new TestCaseData("b", "b");307                throw new System.Exception("my message");308            }309        }310        #endregion311    }312}...HandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using NUnit.Framework;2using NUnit.Framework.Attributes;3using System.Collections;4using System.Collections.Generic;5using System.Text;6{7    {8        {9            {10                yield return new TestCaseData("1").Returns("1");11                yield return new TestCaseData("2").Returns("2");12                yield return new TestCaseData("3").Returns("3");13                yield return new TestCaseData("4").Returns("4");14                yield return new TestCaseData("5").Returns("5");15                throw new System.Exception("Exception from HandlesExceptionInTestCaseSource_GuiDisplay");16                yield return new TestCaseData("6").Returns("6");17            }18        }19    }20}21using NUnit.Framework;22{23    {24        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]25        public string TestMethod(string value)26        {27            return value;28        }29    }30}HandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using NUnit.Framework;6using NUnit.Framework.Attributes;7{8    {9        [TestCaseSource(typeof(DataSourceClass), "TestCases")]10        public void TestMethod1(int a, int b, int c)11        {12            Assert.AreEqual(c, a + b);13        }14    }15}16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using NUnit.Framework;21using NUnit.Framework.Attributes;22{23    {24        [TestCaseSource(typeof(DataSourceClass), "TestCases")]25        public void TestMethod2(int a, int b, int c)26        {27            Assert.AreEqual(c, a + b);28        }29    }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using NUnit.Framework;36using NUnit.Framework.Attributes;37{38    {39        [TestCaseSource(typeof(DataSourceClass), "TestCases")]40        public void TestMethod3(int a, int b, int c)41        {42            Assert.AreEqual(c, a + b);43        }44    }45}HandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using NUnit.Framework;2using NUnit.Framework.Attributes;3using System;4using System.Collections;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10    {11        [TestCaseSource(typeof(DataSourceClass), "TestCaseSourceMethod", new object[] { "test" })]12        public void TestMethod(string s)13        {14            Assert.AreEqual("test", s);15        }16    }17}18using NUnit.Framework;19using NUnit.Framework.Attributes;20using System;21using System.Collections;22using System.Collections.Generic;23using System.Linq;24using System.Text;25using System.Threading.Tasks;26{27    {28        [TestCaseSource(typeof(DataSourceClass), "TestCaseSourceMethod", new object[] { "test" })]29        public void TestMethod(string s)30        {31            Assert.AreEqual("test", s);32        }33    }34}35using NUnit.Framework;36using NUnit.Framework.Attributes;37using System;38using System.Collections;39using System.Collections.Generic;40using System.Linq;41using System.Text;42using System.Threading.Tasks;43{44    {45        [TestCaseSource(typeof(DataSourceClass), "TestCaseSourceMethod", new object[] { "test" })]46        public void TestMethod(string s)47        {48            Assert.AreEqual("test", s);49        }50    }51}52using NUnit.Framework;53using NUnit.Framework.Attributes;54using System;55using System.Collections;56using System.Collections.Generic;57using System.Linq;58using System.Text;59using System.Threading.Tasks;60{61    {62        [TestCaseSource(typeof(DataSourceClass), "TestCaseSourceMethod", new object[] { "test" })]63        public void TestMethod(string s)64        {65            Assert.AreEqual("test", sHandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using System;2using System.Collections;3using NUnit.Framework;4{5    {6        [Test, TestCaseSource("GetData")]7        public void TestMethod(int i)8        {9            Assert.AreEqual(1, i);10        }11        static IEnumerable GetData()12        {13            yield return new TestCaseData(1);14            yield return new TestCaseData(2);15            throw new Exception("Exception in GetData method");16        }17    }18}19using NUnit.Framework;20using System;21using System.Collections;22using System.Reflection;23{24    {25        [Test, TestCaseSource("GetData")]26        public void TestMethod(int i)27        {28            Assert.AreEqual(1, i);29        }30        static IEnumerable GetData()31        {32            yield return new TestCaseData(1);33            yield return new TestCaseData(2);34            throw new Exception("Exception in GetData method");35        }36    }37}38using NUnit.Framework;39using System;40using System.Collections;41using System.Reflection;42{43    {44        [Test, TestCaseSource("GetData")]45        public void TestMethod(int i)46        {47            Assert.AreEqual(1, i);48        }49        static IEnumerable GetData()50        {51            yield return new TestCaseData(1);52            yield return new TestCaseData(2);53            throw new Exception("Exception in GetData method");54        }55    }56}57using NUnit.Framework;58using System;59using System.Collections;60using System.Reflection;61{62    {63        [Test, TestCaseSource("GetData")]64        public void TestMethod(int i)65        {66            Assert.AreEqual(1, i);67        }68        static IEnumerable GetData()69        {70            yield return new TestCaseData(1HandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using NUnit.Framework;2using NUnit.Framework.Attributes;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8using System.Windows.Forms;9{10    {11        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]12        public void Test1(string s)13        {14            MessageBox.Show(s);15        }16    }17}18using NUnit.Framework;19using NUnit.Framework.Attributes;20using System;21using System.Collections.Generic;22using System.Linq;23using System.Text;24using System.Threading.Tasks;25using System.Windows.Forms;26{27    {28        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]29        public void Test1(string s)30        {31            MessageBox.Show(s);32        }33    }34}35using NUnit.Framework;36using NUnit.Framework.Attributes;37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42using System.Windows.Forms;43{44    {45        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]46        public void Test1(string s)47        {48            MessageBox.Show(s);49        }50    }51}52using NUnit.Framework;53using NUnit.Framework.Attributes;54using System;55using System.Collections.Generic;56using System.Linq;57using System.Text;58using System.Threading.Tasks;59using System.Windows.Forms;60{61    {62        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInHandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using System;2using System.Collections;3using NUnit.Framework;4{5    {6        [Test, TestCaseSource("GetData")]7        public void TestMethod(int i)8        {9            Assert.AreEqual(1, i);10        }11        static IEnumerable GetData()12        {13            yield return new TestCaseData(1);14            yield return new TestCaseData(2);15            throw new Exception("Exception in GetData method");16        }17    }18}19using NUnit.Framework;20using System;21using System.Collections;22using System.Reflection;23{24    {25        [Test, TestCaseSource("GetData")]26        public void TestMethod(int i)27        {28            Assert.AreEqual(1, i);29        }30        static IEnumerable GetData()31        {32            yield return new TestCaseData(1);33            yield return new TestCaseData(2);34            throw new Exception("Exception in GetData method");35        }36    }37}38using NUnit.Framework;39using System;40using System.Collections;41using System.Reflection;42{43    {44        [Test, TestCaseSource("GetData")]45        public void TestMethod(int i)46        {47            Assert.AreEqual(1, i);48        }49        static IEnumerable GetData()50        {51            yield return new TestCaseData(1);52            yield return new TestCaseData(2);53            throw new Exception("Exception in GetData method");54        }55    }56}57using NUnit.Framework;58using System;59using System.Collections;60using System.Reflection;61{62    {63        [Test, TestCaseSource("GetData")]64        public void TestMethod(int i)65        {66            Assert.AreEqual(1, i);67        }68        static IEnumerable GetData()69        {70            yield return new TestCaseData(1HandlesExceptionInTestCaseSource_GuiDisplay
Using AI Code Generation
1using NUnit.Framework;2using NUnit.Framework.Attributes;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8using System.Windows.Forms;9{10    {11        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]12        public void Test1(string s)13        {14            MessageBox.Show(s);15        }16    }17}18using NUnit.Framework;19using NUnit.Framework.Attributes;20using System;21using System.Collections.Generic;22using System.Linq;23using System.Text;24using System.Threading.Tasks;25using System.Windows.Forms;26{27    {28        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]29        public void Test1(string s)30        {31            MessageBox.Show(s);32        }33    }34}35using NUnit.Framework;36using NUnit.Framework.Attributes;37using System;38using System.Collections.Generic;39using System.Linq;40using System.Text;41using System.Threading.Tasks;42using System.Windows.Forms;43{44    {45        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInTestCaseSource_GuiDisplay")]46        public void Test1(string s)47        {48            MessageBox.Show(s);49        }50    }51}52using NUnit.Framework;53using NUnit.Framework.Attributes;54using System;55using System.Collections.Generic;56using System.Linq;57using System.Text;58using System.Threading.Tasks;59using System.Windows.Forms;60{61    {62        [Test, TestCaseSource(typeof(DataSourceClass), "HandlesExceptionInNunit is a well-known open-source unit testing framework for C#. This framework is easy to work with and user-friendly. LambdaTest’s NUnit Testing Tutorial provides a structured and detailed learning environment to help you leverage knowledge about the NUnit framework. The NUnit tutorial covers chapters from basics such as environment setup to annotations, assertions, Selenium WebDriver commands, and parallel execution using the NUnit framework.
You can also check out the LambdaTest Certification to enhance your learning in Selenium Automation Testing using the NUnit framework.
Watch this tutorial on the LambdaTest Channel to learn how to set up the NUnit framework, run tests and also execute parallel testing.
Get 100 minutes of automation test minutes FREE!!
