How to use IsEven method of NUnit.TestData.TestFixtureSourceData.SourceData class

Best Nunit code snippet using NUnit.TestData.TestFixtureSourceData.SourceData.IsEven

Run Nunit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

TestFixtureSourceData.cs

Source: TestFixtureSourceData.cs Github

copy
1// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2
3using System.Collections;
4using NUnit.Framework;
5using NUnit.Framework.Interfaces;
6using NUnit.Framework.Internal;
7using System;
8using System.Collections.Generic;
9using System.Linq;
10
11namespace NUnit.TestData.TestFixtureSourceData
12{
13    public abstract class TestFixtureSourceTest
14    {
15        private readonly string Arg;
16        private readonly string Expected;
17
18        public TestFixtureSourceTest(string arg, string expected)
19        {
20            Arg = arg;
21            Expected = expected;
22        }
23
24        [Test]
25        public void CheckSource()
26        {
27            Assert.That(Arg, Is.EqualTo(Expected));
28        }
29    }
30
31    public abstract class TestFixtureSourceDivideTest
32    {
33        private readonly int X;
34        private readonly int Y;
35        private readonly int Z;
36
37        public TestFixtureSourceDivideTest(int x, int y, int z)
38        {
39            X = x;
40            Y = y;
41            Z = z;
42        }
43
44        [Test]
45        public void CheckSource()
46        {
47            Assert.That(X / Y, Is.EqualTo(Z));
48        }
49    }
50
51    [TestFixtureSource("StaticField")]
52    public class StaticField_SameClass : TestFixtureSourceTest
53    {
54        public StaticField_SameClass(string arg) : base(arg, "StaticFieldInClass") { }
55
56#pragma warning disable 414
57        static object[] StaticField = new object[] { "StaticFieldInClass" };
58#pragma warning restore 414
59    }
60
61    [TestFixtureSource("StaticProperty")]
62    public class StaticProperty_SameClass : TestFixtureSourceTest
63    {
64        public StaticProperty_SameClass(string arg) : base(arg, "StaticPropertyInClass") { }
65
66        public StaticProperty_SameClass(string arg, string expected) : base(arg, expected) { }
67
68        public static object[] StaticProperty
69        {
70            get { return new object[] { new object[] { "StaticPropertyInClass" } }; }
71        }
72    }
73
74    [TestFixtureSource("StaticProperty")]
75    public class StaticProperty_InheritedClass : StaticProperty_SameClass
76    {
77        public StaticProperty_InheritedClass(string arg) : base(arg, "StaticPropertyInClass") { }
78    }
79
80    [TestFixtureSource("StaticMethod")]
81    public class StaticMethod_SameClass : TestFixtureSourceTest
82    {
83        public StaticMethod_SameClass(string arg) : base(arg, "StaticMethodInClass") { }
84
85        static object[] StaticMethod()
86        {
87            return new object[] { new object[] { "StaticMethodInClass" } };
88        }
89    }
90
91    [TestFixtureSource("InstanceField")]
92    public class InstanceField_SameClass : TestFixtureSourceTest
93    {
94        public InstanceField_SameClass(string arg) : base(arg, "InstanceFieldInClass") { }
95
96#pragma warning disable 414
97        object[] InstanceField = new object[] { "InstanceFieldInClass" };
98#pragma warning restore 414
99    }
100
101    [TestFixtureSource("InstanceProperty")]
102    public class InstanceProperty_SameClass : TestFixtureSourceTest
103    {
104        public InstanceProperty_SameClass(string arg) : base(arg, "InstancePropertyInClass") { }
105
106        object[] InstanceProperty
107        {
108            get { return new object[] { new object[] { "InstancePropertyInClass" } }; }
109        }
110    }
111
112    [TestFixtureSource("InstanceMethod")]
113    public class InstanceMethod_SameClass : TestFixtureSourceTest
114    {
115        public InstanceMethod_SameClass(string arg) : base(arg, "InstanceMethodInClass") { }
116
117        object[] InstanceMethod()
118        {
119            return new object[] { new object[] { "InstanceMethodInClass" } };
120        }
121    }
122
123    [TestFixtureSource(typeof(SourceData), "StaticField")]
124    public class StaticField_DifferentClass : TestFixtureSourceTest
125    {
126        public StaticField_DifferentClass(string arg) : base(arg, "StaticField") { }
127    }
128
129    [TestFixtureSource(typeof(SourceData), "StaticProperty")]
130    public class StaticProperty_DifferentClass : TestFixtureSourceTest
131    {
132        public StaticProperty_DifferentClass(string arg) : base(arg, "StaticProperty") { }
133    }
134
135    [TestFixtureSource(typeof(SourceData), "StaticMethod")]
136    public class StaticMethod_DifferentClass : TestFixtureSourceTest
137    {
138        public StaticMethod_DifferentClass(string arg) : base(arg, "StaticMethod") { }
139    }
140
141    [TestFixtureSource(typeof(SourceData_IEnumerable))]
142    public class IEnumerableSource : TestFixtureSourceTest
143    {
144        public IEnumerableSource(string arg) : base(arg, "SourceData_IEnumerable") { }
145    }
146
147    [TestFixtureSource("MyData")]
148    public class SourceReturnsObjectArray : TestFixtureSourceDivideTest
149    {
150        public SourceReturnsObjectArray(int x, int y, int z) : base(x, y, z) { }
151
152        static IEnumerable MyData()
153        {
154            yield return new object[] { 12, 4, 3 };
155            yield return new object[] { 12, 3, 4 };
156            yield return new object[] { 12, 6, 2 };
157        }
158    }
159
160    [TestFixtureSource("MyData")]
161    public class SourceReturnsFixtureParameters : TestFixtureSourceDivideTest
162    {
163        public SourceReturnsFixtureParameters(int x, int y, int z) : base(x, y, z) { }
164
165        static IEnumerable MyData()
166        {
167            yield return new TestFixtureParameters(12, 4, 3);
168            yield return new TestFixtureParameters(12, 3, 4);
169            yield return new TestFixtureParameters(12, 6, 2);
170        }
171    }
172
173    [TestFixture]
174    [TestFixtureSource("MyData")]
175    public class ExtraTestFixtureAttributeIsIgnored : TestFixtureSourceDivideTest
176    {
177        public ExtraTestFixtureAttributeIsIgnored(int x, int y, int z) : base(x, y, z) { }
178
179        static IEnumerable MyData()
180        {
181            yield return new object[] { 12, 4, 3 };
182            yield return new object[] { 12, 3, 4 };
183            yield return new object[] { 12, 6, 2 };
184        }
185    }
186
187    [TestFixture]
188    [TestFixtureSource("MyData")]
189    [TestFixtureSource("MoreData", Category = "Extra")]
190    [TestFixture(12, 12, 1)]
191    public class TestFixtureMayUseMultipleSourceAttributes : TestFixtureSourceDivideTest
192    {
193        public TestFixtureMayUseMultipleSourceAttributes(int n, int d, int q) : base(n, d, q) { }
194
195        static IEnumerable MyData()
196        {
197            yield return new object[] { 12, 4, 3 };
198            yield return new object[] { 12, 3, 4 };
199            yield return new object[] { 12, 6, 2 };
200        }
201
202#pragma warning disable 414
203        static object[] MoreData = new object[] {
204            new object[] { 12, 1, 12 },
205            new object[] { 12, 2, 6 } };
206#pragma warning restore 414
207    }
208
209    [TestFixtureSource("IgnoredData")]
210    public class IndividualInstancesMayBeIgnored : TestFixtureSourceTest
211    {
212        public IndividualInstancesMayBeIgnored(string arg) : base(arg, "IgnoredData") { }
213
214        static IEnumerable IgnoredData()
215        {
216            yield return new TestFixtureData("GoodData");
217            yield return new TestFixtureData("IgnoredData").Ignore("There must be a reason");
218            yield return new TestFixtureData("MoreGoodData");
219        }
220    }
221
222    [TestFixtureSource("ExplicitData")]
223    public class IndividualInstancesMayBeExplicit : TestFixtureSourceTest
224    {
225        public IndividualInstancesMayBeExplicit(string arg) : base(arg, "ExplicitData") { }
226
227        static IEnumerable ExplicitData()
228        {
229            yield return new TestFixtureData("GoodData");
230            yield return new TestFixtureData("ExplicitData").Explicit("Runs long");
231            yield return new TestFixtureData("MoreExplicitData").Explicit();
232        }
233    }
234
235    #region Test name tests
236
237    [TestFixtureSource(nameof(IndividualInstanceNameTestDataSource))]
238    public sealed class IndividualInstanceNameTestDataFixture
239    {
240        public IndividualInstanceNameTestDataFixture(params object[] args)
241        {
242        }
243
244        [Test]
245        public void Test() { }
246
247        public static IEnumerable<TestFixtureData> IndividualInstanceNameTestDataSource() =>
248            from spec in TestDataSpec.Specs
249            select new TestFixtureData(spec.Arguments)
250            {
251                Properties = // SetProperty does not exist
252                    {
253                        ["ExpectedTestName"] = { spec.GetFixtureName(nameof(IndividualInstanceNameTestDataFixture)) }
254                    }
255            }
256                .SetArgDisplayNames(spec.ArgDisplayNames);
257    }
258
259    #endregion
260
261    [TestFixture]
262    public abstract class Issue1118_Root
263    {
264        protected readonly string Browser;
265
266        protected Issue1118_Root(string browser)
267        {
268            Browser = browser;
269        }
270
271        [SetUp]
272        public void Setup()
273        {
274        }
275    }
276
277    [TestFixtureSource(typeof(Issue1118_SourceData))]
278    public class Issue1118_Base : Issue1118_Root
279    {
280        public Issue1118_Base(string browser) : base(browser)
281        {
282        }
283
284        [TearDown]
285        public void Cleanup()
286        {
287        }
288    }
289
290    public class Issue1118_Fixture : Issue1118_Base
291    {
292        public Issue1118_Fixture(string browser) : base(browser)
293        {
294        }
295
296        [Test]
297        public void DoSomethingOnAWebPageWithSelenium()
298        {
299        }
300
301        [Test]
302        public void DoSomethingElseOnAWebPageWithSelenium()
303        {
304        }
305    }
306
307    public class Issue1118_SourceData : IEnumerable
308    {
309        public IEnumerator GetEnumerator()
310        {
311            yield return "Firefox";
312            yield return "Chrome";
313            yield return "Internet Explorer";
314        }
315    }
316
317    public class GenericFixtureSource
318    {
319        public static readonly Type[] Source = new Type[]
320        {
321            typeof(short),
322            typeof(int),
323            typeof(long)
324        };
325    }
326
327    [TestFixtureSource(typeof(GenericFixtureSource), "Source")]
328    public class GenericFixtureSourceWithProperArgsProvided<T>
329    {
330        [Test]
331        public void SomeTest()
332        {
333        }
334    }
335
336    public class GenericFixtureWithTypeAndConstructorArgsSource
337    {
338        public static readonly ITestFixtureData[] Source =
339        {
340            new TypedTestFixture<int>(5),
341            new TypedTestFixture<object>(new object())
342        };
343
344        public class TypedTestFixture<T> : Framework.Internal.TestParameters, ITestFixtureData
345        {
346            public TypedTestFixture(params object[] arguments)
347                : base(arguments)
348            {
349                TypeArgs = new[] { typeof(T) };
350            }
351
352            public Type[] TypeArgs { get; }
353        }
354    }
355
356    [TestFixtureSource(typeof(GenericFixtureWithTypeAndConstructorArgsSource), "Source")]
357    public class GenericFixtureSourceWithTypeAndConstructorArgs<T>
358    {
359        private readonly T _arg;
360
361        public GenericFixtureSourceWithTypeAndConstructorArgs(T arg)
362        {
363            _arg = arg;
364        }
365
366        [Test]
367        public void SomeTest()
368        {
369            Assert.That(!EqualityComparer<T>.Default.Equals(_arg, default(T)), "constructor argument was not injected");
370        }
371    }
372
373    public class GenericFixtureWithConstructorArgsSource
374    {
375        public static readonly TestFixtureData[] Source =
376        {
377            new TestFixtureData(5),
378            new TestFixtureData(new object())
379        };
380    }
381
382    [TestFixtureSource(typeof(GenericFixtureWithConstructorArgsSource), "Source")]
383    public class GenericFixtureSourceWithConstructorArgs<T>
384    {
385        private readonly T _arg;
386
387        public GenericFixtureSourceWithConstructorArgs(T arg)
388        {
389            _arg = arg;
390        }
391
392        [Test]
393        public void SomeTest()
394        {
395            Assert.That(!EqualityComparer<T>.Default.Equals(_arg, default(T)), "constructor argument was not injected");
396        }
397    }
398
399
400    #region Source Data Classes
401
402    class SourceData_IEnumerable : IEnumerable
403    {
404        public SourceData_IEnumerable()
405        {
406        }
407
408        public IEnumerator GetEnumerator()
409        {
410            yield return "SourceData_IEnumerable";
411        }
412    }
413
414    class SourceData
415    {
416        public static object[] InheritedStaticProperty
417        {
418            get { return new object[] { new object[] { "StaticProperty" } }; }
419        }
420
421#pragma warning disable 414
422        static object[] StaticField = new object[] { "StaticField" };
423#pragma warning restore 414
424
425        static object[] StaticProperty
426        {
427            get { return new object[] { new object[] { "StaticProperty" } }; }
428        }
429
430        static object[] StaticMethod()
431        {
432            return new object[] { new object[] { "StaticMethod" } };
433        }
434    }
435
436    #endregion
437
438    [TestFixtureSource(nameof(DataSource))]
439    public abstract class DataSourcePrivateFieldInBaseClass
440    {
441        private static readonly int[] DataSource = { 3, 5 };
442
443        protected DataSourcePrivateFieldInBaseClass(int data)
444        {
445            Data = data;
446        }
447
448        protected int Data { get; }
449    }
450
451    public class DerivedClassUsingBaseClassDataSource : DataSourcePrivateFieldInBaseClass
452    {
453        public DerivedClassUsingBaseClassDataSource(int data) : base(data)
454        {
455        }
456
457        [Test]
458        public void IsOdd()
459        {
460            Assert.That(Data % 2 == 1);
461        }
462    }
463
464    public class BaseClassUsingDerivedClassDataSource : DataSourcePrivateFieldInBaseClass
465    {
466        private static readonly int[] DataSource = { 2, 4 };
467
468        public BaseClassUsingDerivedClassDataSource(int data) : base(data)
469        {
470        }
471
472        [Test]
473        public void IsEven()
474        {
475            Assert.That(Data % 2 == 0);
476        }
477    }
478}
479
480[TestFixtureSource("MyData")]
481public class NoNamespaceTestFixtureSourceWithTwoValues
482{
483    public NoNamespaceTestFixtureSourceWithTwoValues(int i) { }
484
485    [Test]
486    public void Test()
487    {
488    }
489
490#pragma warning disable 414
491    static object[] MyData = { 1, 2 };
492#pragma warning restore 414
493}
494
495[TestFixtureSource("MyData")]
496public class NoNamespaceTestFixtureSourceWithSingleValue
497{
498    public NoNamespaceTestFixtureSourceWithSingleValue(int i) { }
499
500    [Test]
501    public void Test()
502    {
503    }
504
505#pragma warning disable 414
506    static object[] MyData = { 1 };
507#pragma warning restore 414
508}
509
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in SourceData

Trigger IsEven code on LambdaTest Cloud Grid

Execute automation tests with IsEven on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)