How to use SourceData class of NUnit.TestData.TestFixtureSourceData package

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

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// ***********************************************************************
2// Copyright (c) 2015 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
24using System.Collections;
25using NUnit.Framework;
26using NUnit.Framework.Internal;
27
28namespace NUnit.TestData.TestFixtureSourceData
29{
30    public abstract class TestFixtureSourceTest
31    {
32        private string Arg;
33        private string Expected;
34
35        public TestFixtureSourceTest(string arg, string expected)
36        {
37            Arg = arg;
38            Expected = expected;
39        }
40
41        [Test]
42        public void CheckSource()
43        {
44            Assert.That(Arg, Is.EqualTo(Expected));
45        }
46    }
47
48    public abstract class TestFixtureSourceDivideTest
49    {
50        private int X;
51        private int Y;
52        private int Z;
53
54        public TestFixtureSourceDivideTest(int x, int y, int z)
55        {
56            X = x;
57            Y = y;
58            Z = z;
59        }
60
61        [Test]
62        public void CheckSource()
63        {
64            Assert.That(X / Y, Is.EqualTo(Z));
65        }
66    }
67
68    [TestFixtureSource("StaticField")]
69    public class StaticField_SameClass : TestFixtureSourceTest
70    {
71        public StaticField_SameClass(string arg) : base(arg, "StaticFieldInClass") { }
72
73        static object[] StaticField = new object[] { "StaticFieldInClass" };
74    }
75
76    [TestFixtureSource("StaticProperty")]
77    public class StaticProperty_SameClass : TestFixtureSourceTest
78    {
79        public StaticProperty_SameClass(string arg) : base(arg, "StaticPropertyInClass") { }
80
81        public StaticProperty_SameClass(string arg, string expected) : base(arg, expected) { }
82
83        public static object[] StaticProperty
84        {
85            get { return new object[] { new object[] { "StaticPropertyInClass" } }; }
86        }
87    }
88
89    [TestFixtureSource("StaticProperty")]
90    public class StaticProperty_InheritedClass : StaticProperty_SameClass 
91    {
92        public StaticProperty_InheritedClass (string arg) : base(arg, "StaticPropertyInClass") { }
93    }
94
95    [TestFixtureSource("StaticMethod")]
96    public class StaticMethod_SameClass : TestFixtureSourceTest
97    {
98        public StaticMethod_SameClass(string arg) : base(arg, "StaticMethodInClass") { }
99
100        static object[] StaticMethod()
101        {
102            return new object[] { new object[] { "StaticMethodInClass" } };
103        }
104    }
105
106    [TestFixtureSource("InstanceField")]
107    public class InstanceField_SameClass : TestFixtureSourceTest
108    {
109        public InstanceField_SameClass(string arg) : base(arg, "InstanceFieldInClass") { }
110
111        object[] InstanceField = new object[] { "InstanceFieldInClass" };
112    }
113
114    [TestFixtureSource("InstanceProperty")]
115    public class InstanceProperty_SameClass : TestFixtureSourceTest
116    {
117        public InstanceProperty_SameClass(string arg) : base(arg, "InstancePropertyInClass") { }
118
119        object[] InstanceProperty
120        {
121            get { return new object[] { new object[] { "InstancePropertyInClass" } }; }
122        }
123    }
124
125    [TestFixtureSource("InstanceMethod")]
126    public class InstanceMethod_SameClass : TestFixtureSourceTest
127    {
128        public InstanceMethod_SameClass(string arg) : base(arg, "InstanceMethodInClass") { }
129
130        object[] InstanceMethod()
131        {
132            return new object[] { new object[] { "InstanceMethodInClass" } };
133        }
134    }
135
136    [TestFixtureSource(typeof(SourceData), "StaticField")]
137    public class StaticField_DifferentClass : TestFixtureSourceTest
138    {
139        public StaticField_DifferentClass(string arg) : base(arg, "StaticField") { }
140    }
141
142    [TestFixtureSource(typeof(SourceData), "StaticProperty")]
143    public class StaticProperty_DifferentClass : TestFixtureSourceTest
144    {
145        public StaticProperty_DifferentClass(string arg) : base(arg, "StaticProperty") { }
146    }
147
148    [TestFixtureSource(typeof(SourceData), "StaticMethod")]
149    public class StaticMethod_DifferentClass : TestFixtureSourceTest
150    {
151        public StaticMethod_DifferentClass(string arg) : base(arg, "StaticMethod") { }
152    }
153
154    [TestFixtureSource(typeof(SourceData_IEnumerable))]
155    public class IEnumerableSource : TestFixtureSourceTest
156    {
157        public IEnumerableSource(string arg) : base(arg, "SourceData_IEnumerable") { }
158    }
159
160    [TestFixtureSource("MyData")]
161    public class SourceReturnsObjectArray : TestFixtureSourceDivideTest
162    {
163        public SourceReturnsObjectArray(int x, int y, int z) : base(x, y, z) { }
164
165        static IEnumerable MyData()
166        {
167            yield return new object[] { 12, 4, 3 };
168            yield return new object[] { 12, 3, 4 };
169            yield return new object[] { 12, 6, 2 };
170        }
171    }
172
173    [TestFixtureSource("MyData")]
174    public class SourceReturnsFixtureParameters : TestFixtureSourceDivideTest
175    {
176        public SourceReturnsFixtureParameters(int x, int y, int z) : base(x, y, z) { }
177
178        static IEnumerable MyData()
179        {
180            yield return new TestFixtureParameters(12, 4, 3);
181            yield return new TestFixtureParameters(12, 3, 4);
182            yield return new TestFixtureParameters(12, 6, 2);
183        }
184    }
185
186    [TestFixture]
187    [TestFixtureSource("MyData")]
188    public class ExtraTestFixtureAttributeIsIgnored : TestFixtureSourceDivideTest
189    {
190        public ExtraTestFixtureAttributeIsIgnored(int x, int y, int z) : base(x, y, z) { }
191
192        static IEnumerable MyData()
193        {
194            yield return new object[] { 12, 4, 3 };
195            yield return new object[] { 12, 3, 4 };
196            yield return new object[] { 12, 6, 2 };
197        }
198    }
199
200    [TestFixture]
201    [TestFixtureSource("MyData")]
202    [TestFixtureSource("MoreData", Category = "Extra")]
203    [TestFixture(12, 12, 1)]
204    public class TestFixtureMayUseMultipleSourceAttributes : TestFixtureSourceDivideTest
205    {
206        public TestFixtureMayUseMultipleSourceAttributes(int n, int d, int q) : base(n, d, q) { }
207
208        static IEnumerable MyData()
209        {
210            yield return new object[] { 12, 4, 3 };
211            yield return new object[] { 12, 3, 4 };
212            yield return new object[] { 12, 6, 2 };
213        }
214
215        static object[] MoreData = new object[] {
216            new object[] { 12, 1, 12 },
217            new object[] { 12, 2, 6 } };
218    }
219
220    [TestFixtureSource("IgnoredData")]
221    public class IndividualInstancesMayBeIgnored : TestFixtureSourceTest
222    {
223        public IndividualInstancesMayBeIgnored(string arg) : base(arg, "IgnoredData") { }
224
225        static IEnumerable IgnoredData()
226        {
227            yield return new TestFixtureData("GoodData");
228            yield return new TestFixtureData("IgnoredData").Ignore("There must be a reason");
229            yield return new TestFixtureData("MoreGoodData");
230        }
231    }
232
233    [TestFixtureSource("ExplicitData")]
234    public class IndividualInstancesMayBeExplicit : TestFixtureSourceTest
235    {
236        public IndividualInstancesMayBeExplicit(string arg) : base(arg, "ExplicitData") { }
237
238        static IEnumerable ExplicitData()
239        {
240            yield return new TestFixtureData("GoodData");
241            yield return new TestFixtureData("ExplicitData").Explicit("Runs long");
242            yield return new TestFixtureData("MoreExplicitData").Explicit();
243        }
244    }
245
246    [TestFixture]
247    public abstract class Issue1118_Root
248    {
249        protected readonly string Browser;
250
251        protected Issue1118_Root(string browser)
252        {
253            Browser = browser;
254        }
255
256        [SetUp]
257        public void Setup()
258        {
259        }
260    }
261
262    [TestFixtureSource(typeof(Issue1118_SourceData))]
263    public class Issue1118_Base : Issue1118_Root
264    {
265        public Issue1118_Base(string browser) : base(browser)
266        {
267        }
268
269        [TearDown]
270        public void Cleanup()
271        {
272        }
273    }
274
275    public class Issue1118_Fixture : Issue1118_Base
276    {
277        public Issue1118_Fixture(string browser) : base(browser)
278        {
279        }
280
281        [Test]
282        public void DoSomethingOnAWebPageWithSelenium()
283        {
284        }
285
286        [Test]
287        public void DoSomethingElseOnAWebPageWithSelenium()
288        {
289        }
290    }
291
292    public class Issue1118_SourceData : IEnumerable
293    {
294        public IEnumerator GetEnumerator()
295        {
296            yield return "Firefox";
297            yield return "Chrome";
298            yield return "Internet Explorer";
299        }
300    }
301
302    #region Source Data Classes
303
304    class SourceData_IEnumerable : IEnumerable
305    {
306        public SourceData_IEnumerable()
307        {
308        }
309
310        public IEnumerator GetEnumerator()
311        {
312            yield return "SourceData_IEnumerable";
313        }
314    }
315
316    class SourceData
317    {
318        public static object[] InheritedStaticProperty
319        {
320            get { return new object[] { new object[] { "StaticProperty" } }; }
321        }
322
323        static object[] StaticField = new object[] { "StaticField" };
324
325        static object[] StaticProperty
326        {
327            get { return new object[] { new object[] { "StaticProperty" } }; }
328        }
329
330        static object[] StaticMethod()
331        {
332            return new object[] { new object[] { "StaticMethod" } };
333        }
334    }
335
336    #endregion
337}
338
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 methods in SourceData

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)