Best Nunit code snippet using NUnit.Framework.EnumDataSource
RandomAttribute.cs
Source:RandomAttribute.cs
...159 _source = new SByteDataSource(_count);160 else if (parmType == typeof(decimal))161 _source = new DecimalDataSource(_count);162 else if (parmType.GetTypeInfo().IsEnum)163 _source = new EnumDataSource(_count);164 else // Default165 _source = new IntDataSource(_count);166 }167 else if (_source.DataType != parmType && WeConvert(_source.DataType, parmType))168 {169 _source = new RandomDataConverter(_source);170 }171 return _source.GetData(parameter);172 //// Copy the random _values into the data array173 //// and call the base class which may need to174 //// convert them to another type.175 //this.data = new object[values.Count];176 //for (int i = 0; i < values.Count; i++)177 // this.data[i] = values[i];178 //return base.GetData(parameter);179 }180 private bool WeConvert(Type sourceType, Type targetType)181 {182 if (targetType == typeof(short) || targetType == typeof(ushort) || targetType == typeof(byte) || targetType == typeof(sbyte))183 return sourceType == typeof(int);184 if (targetType == typeof(decimal))185 return sourceType == typeof(int) || sourceType == typeof(double);186 187 return false;188 }189 #endregion190 #region Nested DataSource Classes191 #region RandomDataSource192 abstract class RandomDataSource : IParameterDataSource193 {194 public Type DataType { get; protected set; }195 public abstract IEnumerable GetData(IParameterInfo parameter);196 }197 abstract class RandomDataSource<T> : RandomDataSource198 {199 private T _min;200 private T _max;201 private int _count;202 private bool _inRange;203 protected Randomizer _randomizer;204 protected RandomDataSource(int count)205 {206 _count = count;207 _inRange = false;208 DataType = typeof(T);209 }210 protected RandomDataSource(T min, T max, int count)211 {212 _min = min;213 _max = max;214 _count = count;215 _inRange = true;216 DataType = typeof(T);217 }218 public override IEnumerable GetData(IParameterInfo parameter)219 {220 //Guard.ArgumentValid(parameter.ParameterType == typeof(T), "Parameter type must be " + typeof(T).Name, "parameter");221 _randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);222 for (int i = 0; i < _count; i++)223 yield return _inRange224 ? GetNext(_min, _max)225 : GetNext();226 }227 protected abstract T GetNext();228 protected abstract T GetNext(T min, T max);229 }230 #endregion231 #region RandomDataConverter232 class RandomDataConverter : RandomDataSource233 {234 IParameterDataSource _source;235 public RandomDataConverter(IParameterDataSource source)236 {237 _source = source;238 }239 public override IEnumerable GetData(IParameterInfo parameter)240 {241 Type parmType = parameter.ParameterType;242 foreach (object obj in _source.GetData(parameter))243 {244 if (obj is int)245 {246 int ival = (int)obj; // unbox first247 if (parmType == typeof(short))248 yield return (short)ival;249 else if (parmType == typeof(ushort))250 yield return (ushort)ival;251 else if (parmType == typeof(byte))252 yield return (byte)ival;253 else if (parmType == typeof(sbyte))254 yield return (sbyte)ival;255 else if (parmType == typeof(decimal))256 yield return (decimal)ival;257 }258 else if (obj is double)259 {260 double d = (double)obj; // unbox first261 if (parmType == typeof(decimal))262 yield return (decimal)d;263 }264 }265 }266 }267 #endregion268 #region IntDataSource269 class IntDataSource : RandomDataSource<int>270 {271 public IntDataSource(int count) : base(count) { }272 public IntDataSource(int min, int max, int count) : base(min, max, count) { }273 protected override int GetNext()274 {275 return _randomizer.Next();276 }277 protected override int GetNext(int min, int max)278 {279 return _randomizer.Next(min, max);280 }281 }282 #endregion283 #region UIntDataSource284 class UIntDataSource : RandomDataSource<uint>285 {286 public UIntDataSource(int count) : base(count) { }287 public UIntDataSource(uint min, uint max, int count) : base(min, max, count) { }288 protected override uint GetNext()289 {290 return _randomizer.NextUInt();291 }292 protected override uint GetNext(uint min, uint max)293 {294 return _randomizer.NextUInt(min, max);295 }296 }297 #endregion298 #region LongDataSource299 class LongDataSource : RandomDataSource<long>300 {301 public LongDataSource(int count) : base(count) { }302 public LongDataSource(long min, long max, int count) : base(min, max, count) { }303 protected override long GetNext()304 {305 return _randomizer.NextLong();306 }307 protected override long GetNext(long min, long max)308 {309 return _randomizer.NextLong(min, max);310 }311 }312 #endregion313 #region ULongDataSource314 class ULongDataSource : RandomDataSource<ulong>315 {316 public ULongDataSource(int count) : base(count) { }317 public ULongDataSource(ulong min, ulong max, int count) : base(min, max, count) { }318 protected override ulong GetNext()319 {320 return _randomizer.NextULong();321 }322 protected override ulong GetNext(ulong min, ulong max)323 {324 return _randomizer.NextULong(min, max);325 }326 }327 #endregion328 #region ShortDataSource329 class ShortDataSource : RandomDataSource<short>330 {331 public ShortDataSource(int count) : base(count) { }332 public ShortDataSource(short min, short max, int count) : base(min, max, count) { }333 protected override short GetNext()334 {335 return _randomizer.NextShort();336 }337 protected override short GetNext(short min, short max)338 {339 return _randomizer.NextShort(min, max);340 }341 }342 #endregion343 #region UShortDataSource344 class UShortDataSource : RandomDataSource<ushort>345 {346 public UShortDataSource(int count) : base(count) { }347 public UShortDataSource(ushort min, ushort max, int count) : base(min, max, count) { }348 protected override ushort GetNext()349 {350 return _randomizer.NextUShort();351 }352 protected override ushort GetNext(ushort min, ushort max)353 {354 return _randomizer.NextUShort(min, max);355 }356 }357 #endregion358 #region DoubleDataSource359 class DoubleDataSource : RandomDataSource<double>360 {361 public DoubleDataSource(int count) : base(count) { }362 public DoubleDataSource(double min, double max, int count) : base(min, max, count) { }363 protected override double GetNext()364 {365 return _randomizer.NextDouble();366 }367 protected override double GetNext(double min, double max)368 {369 return _randomizer.NextDouble(min, max);370 }371 }372 #endregion373 #region FloatDataSource374 class FloatDataSource : RandomDataSource<float>375 {376 public FloatDataSource(int count) : base(count) { }377 public FloatDataSource(float min, float max, int count) : base(min, max, count) { }378 protected override float GetNext()379 {380 return _randomizer.NextFloat();381 }382 protected override float GetNext(float min, float max)383 {384 return _randomizer.NextFloat(min, max);385 }386 }387 #endregion388 #region ByteDataSource389 class ByteDataSource : RandomDataSource<byte>390 {391 public ByteDataSource(int count) : base(count) { }392 public ByteDataSource(byte min, byte max, int count) : base(min, max, count) { }393 protected override byte GetNext()394 {395 return _randomizer.NextByte();396 }397 protected override byte GetNext(byte min, byte max)398 {399 return _randomizer.NextByte(min, max);400 }401 }402 #endregion403 #region SByteDataSource404 class SByteDataSource : RandomDataSource<sbyte>405 {406 public SByteDataSource(int count) : base(count) { }407 public SByteDataSource(sbyte min, sbyte max, int count) : base(min, max, count) { }408 protected override sbyte GetNext()409 {410 return _randomizer.NextSByte();411 }412 protected override sbyte GetNext(sbyte min, sbyte max)413 {414 return _randomizer.NextSByte(min, max);415 }416 }417 #endregion418 #region EnumDataSource419 class EnumDataSource : RandomDataSource420 {421 private int _count;422 public EnumDataSource(int count)423 {424 _count = count;425 DataType = typeof(Enum);426 }427 public override IEnumerable GetData(IParameterInfo parameter)428 {429 Guard.ArgumentValid(parameter.ParameterType.GetTypeInfo().IsEnum, "EnumDataSource requires an enum parameter", "parameter");430 Randomizer randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);431 DataType = parameter.ParameterType;432 for (int i = 0; i < _count; i++ )433 yield return randomizer.NextEnum(parameter.ParameterType);434 }435 }436 #endregion437 #region DecimalDataSource438 // Currently, Randomizer doesn't implement methods for decimal439 // so we use random Ulongs and convert them. This doesn't cover440 // the full range of decimal, so it's temporary.441 class DecimalDataSource : RandomDataSource<decimal>442 {443 public DecimalDataSource(int count) : base(count) { }...
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 [TestCaseSource(typeof(EnumDataSource), "TestCases")]10 public void TestMethod1(int a, int b)11 {12 Console.WriteLine("a: " + a + " b: " + b);13 }14 }15}16using NUnit.Framework;17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22{23 {24 [TestCaseSource(typeof(EnumDataSource), "TestCases")]25 public void TestMethod1(int a, int b)26 {27 Console.WriteLine("a: " + a + " b: " + b);28 }29 }30}31using NUnit.Framework;32using System;33using System.Collections.Generic;34using System.Linq;35using System.Text;36using System.Threading.Tasks;37{38 {39 [TestCaseSource(typeof(EnumDataSource), "TestCases")]40 public void TestMethod1(int a, int b)41 {42 Console.WriteLine("a: " + a + " b: " + b);43 }44 }45}46using NUnit.Framework;47using System;48using System.Collections.Generic;49using System.Linq;50using System.Text;51using System.Threading.Tasks;52{53 {54 [TestCaseSource(typeof(EnumDataSource), "TestCases")]55 public void TestMethod1(int a, int b)56 {57 Console.WriteLine("a: " + a + " b: " + b);58 }59 }60}61using NUnit.Framework;62using System;63using System.Collections.Generic;64using System.Linq;65using System.Text;66using System.Threading.Tasks;67{
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 [TestCaseSource(typeof(EnumDataSource), "GetEnumValues", new object[] { typeof(DayOfWeek) })]10 public void Test1(DayOfWeek day)11 {12 Console.WriteLine(day);13 }14 }15}16using System;17using System.Collections;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22{23 {24 public static IEnumerable GetEnumValues(Type enumType)25 {26 return Enum.GetValues(enumType).Cast<Enum>();27 }28 }29}30using NUnit.Framework;31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36{37 {38 [TestCaseSource(typeof(EnumDataSource), "GetEnumValues", new object[] { typeof(DayOfWeek) })]39 public void Test1(DayOfWeek day)40 {41 Console.WriteLine(day);42 }43 }44}45using System;46using System.Collections;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51{52 {53 public static IEnumerable GetEnumValues(Type enumType)54 {55 return Enum.GetValues(enumType).Cast<Enum>();56 }57 }58}59using NUnit.Framework;60using System;61using System.Collections.Generic;62using System.Linq;63using System.Text;64using System.Threading.Tasks;65{66 {67 [TestCaseSource(typeof(EnumDataSource), "GetEnumValues", new object[] { typeof(DayOfWeek) })]68 public void Test1(DayOfWeek day)69 {70 Console.WriteLine(day);71 }72 }73}
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 var test = new TestClass();12 test.TestMethod(1);13 test.TestMethod(2);14 test.TestMethod(3);15 }16 }17}18using NUnit.Framework;19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24{25 {26 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]27 public void TestMethod(int value)28 {29 Console.WriteLine(value);30 }31 }32 {33 public static IEnumerable<int> GetEnum()34 {35 yield return 1;36 yield return 2;37 yield return 3;38 }39 }40}41using NUnit.Framework;42using System;43using System.Collections.Generic;44using System.Linq;45using System.Text;46using System.Threading.Tasks;47{48 {49 static void Main(string[] args)50 {51 var test = new TestClass();52 test.TestMethod(1);53 test.TestMethod(2);54 test.TestMethod(3);55 }56 }57}58using NUnit.Framework;59using System;60using System.Collections.Generic;61using System.Linq;62using System.Text;63using System.Threading.Tasks;64{65 {66 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]67 public void TestMethod(int value)68 {69 Console.WriteLine(value);70 }71 }72 {73 public static IEnumerable<int> GetEnum()74 {75 yield return 1;76 yield return 2;77 yield return 3;78 }79 }80}81using NUnit.Framework;82using System;83using System.Collections.Generic;84using System.Linq;85using System.Text;86using System.Threading.Tasks;87{88 {
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]10 public void TestMethod1(Colors c)11 {12 Console.WriteLine(c);13 }14 }15 {16 public static IEnumerable<Colors> GetEnum()17 {18 foreach (Colors c in Enum.GetValues(typeof(Colors)))19 {20 yield return c;21 }22 }23 }24 {25 }26}27using NUnit.Framework;28using System;29using System.Collections.Generic;30using System.Linq;31using System.Text;32using System.Threading.Tasks;33{34 {35 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]36 public void TestMethod1(Colors c)37 {38 Console.WriteLine(c);39 }40 }41 {42 public static IEnumerable<Colors> GetEnum()43 {44 foreach (Colors c in Enum.GetValues(typeof(Colors)))45 {46 yield return c;47 }48 }49 }50 {51 }52}53using NUnit.Framework;54using System;55using System.Collections.Generic;56using System.Linq;57using System.Text;58using System.Threading.Tasks;59{60 {61 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]62 public void TestMethod1(Colors c)63 {64 Console.WriteLine(c);65 }66 }67 {68 public static IEnumerable<Colors> GetEnum()69 {70 foreach (Colors c in Enum.GetValues(typeof(Colors)))71 {72 yield return c;73 }74 }75 }76 {77 }78}79using NUnit.Framework;80using System;81using System.Collections.Generic;
EnumDataSource
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using NUnit.Framework;6{7 {8 [TestCaseSource(typeof(EnumDataSource), "GetEnumData", new object[] { typeof(TestEnum), typeof(TestEnum) })]9 public void TestMethod1(TestEnum a, TestEnum b)10 {11 Console.WriteLine("a={0}, b={1}", a, b);12 }13 }14}15using System;16using System.Collections.Generic;17using System.Linq;18using System.Text;19using NUnit.Framework;20{21 {22 [TestCaseSource(typeof(EnumDataSource), "GetEnumData", new object[] { typeof(TestEnum), typeof(TestEnum) })]23 public void TestMethod1(TestEnum a, TestEnum b)24 {25 Console.WriteLine("a={0}, b={1}", a, b);26 }27 }28}29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using NUnit.Framework;34{35 {36 [TestCaseSource(typeof(EnumDataSource), "GetEnumData", new object[] { typeof(TestEnum), typeof(TestEnum) })]37 public void TestMethod1(TestEnum a, TestEnum b)38 {39 Console.WriteLine("a={0}, b={1}", a, b);40 }41 }42}43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using NUnit.Framework;48{49 {50 [TestCaseSource(typeof(EnumDataSource), "GetEnumData", new object[] { typeof(TestEnum), typeof(TestEnum) })]51 public void TestMethod1(TestEnum a, TestEnum b)52 {53 Console.WriteLine("a={0}, b={1}", a, b);54 }55 }56}
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]10 public void TestMethod1(CarTypes carType)11 {12 Assert.AreEqual(1, (int)carType);13 }14 }15}16using NUnit.Framework;17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22{23 {24 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]25 public void TestMethod1(CarTypes carType)26 {27 Assert.AreEqual(1, (int)carType);28 }29 }30}31using NUnit.Framework;32using System;33using System.Collections.Generic;34using System.Linq;35using System.Text;36using System.Threading.Tasks;37{38 {39 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]40 public void TestMethod1(CarTypes carType)41 {42 Assert.AreEqual(1, (int)carType);43 }44 }45}46using NUnit.Framework;47using System;48using System.Collections.Generic;49using System.Linq;50using System.Text;51using System.Threading.Tasks;52{53 {54 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]55 public void TestMethod1(CarTypes carType)56 {57 Assert.AreEqual(1, (int)carType);58 }59 }60}61using NUnit.Framework;62using System;63using System.Collections.Generic;64using System.Linq;65using System.Text;66using System.Threading.Tasks;67{68 {69 [TestCaseSource(typeof(EnumDataSource), "GetEnum")]70 public void TestMethod1(Car
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 public static IEnumerable<TestCaseData> TestCases<T>()10 {11 return Enum.GetValues(typeof(T)).Cast<T>().Select(e => new TestCaseData(e));12 }13 }14}15using NUnit.Framework;16using System;17using System.Collections.Generic;18using System.Linq;19using System.Text;20using System.Threading.Tasks;21{22 {23 public static IEnumerable<TestCaseData> TestCases<T>()24 {25 return Enum.GetValues(typeof(T)).Cast<T>().Select(e => new TestCaseData(e));26 }27 }28}29using NUnit.Framework;30using System;31using System.Collections.Generic;32using System.Linq;33using System.Text;34using System.Threading.Tasks;35{36 {37 public static IEnumerable<TestCaseData> TestCases<T>()38 {39 return Enum.GetValues(typeof(T)).Cast<T>().Select(e => new TestCaseData(e));40 }41 }42}43using NUnit.Framework;44using System;45using System.Collections.Generic;46using System.Linq;47using System.Text;48using System.Threading.Tasks;49{50 {51 public static IEnumerable<TestCaseData> TestCases<T>()52 {53 return Enum.GetValues(typeof(T)).Cast<T>().Select(e => new TestCaseData(e));54 }55 }56}57using NUnit.Framework;58using System;59using System.Collections.Generic;60using System.Linq;61using System.Text;62using System.Threading.Tasks;63{64 {65 public static IEnumerable<TestCaseData> TestCases<T>()66 {67 return Enum.GetValues(typeof(T)).Cast<T>().Select(e => new TestCaseData(e));68 }69 }70}71using NUnit.Framework;72using System;73using System.Collections.Generic;74using System.Linq;75using System.Text;76using System.Threading.Tasks;
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2{3 {4 [TestCaseSource(typeof(EnumDataSource), "GetEnumValues", new object[] { typeof(TestEnum) })]5 public void Test1(TestEnum value)6 {7 Assert.IsTrue(true);8 }9 }10 {11 }12}13Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "5", "5.csproj", "{C7A9A9B4-7D4D-4F1A-9A5E-6B7C8A8A2B2C}"14 GlobalSection(SolutionConfigurationPlatforms) = preSolution15 GlobalSection(ProjectConfigurationPlatforms) = postSolution16 {C7A9A9B4-7D4D-4F1A-9A5E-6B7C8A8A2B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU17 {C7A9A9B4-7D4D-4F1A-9A5E-6B7C8A8A2B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU18 {C7A9A9B4-7D4D-4
EnumDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 {10 }11 [Test, TestCaseSource(typeof(EnumDataSource), "EnumValues")]12 public void Test(TestEnum testEnum)13 {14 Console.WriteLine(testEnum);15 }16 {17 {18 foreach (TestEnum testEnum in Enum.GetValues(typeof(TestEnum)))19 {20 yield return new TestCaseData(testEnum);21 }22 }23 }24 }25}26Recommended Posts: C# | Enum.GetNames() Method27C# | Enum.GetUnderlyingType() Method28C# | Enum.GetValues() Method29C# | Enum.GetNames() Method30C# | Enum.GetValues() Method31C# | Enum.GetUnderlyingType() Method32C# | Enum.Parse() Method33C# | Enum.GetUnderlyingType() Method34C# | Enum.HasFlag() Method35C# | Enum.Parse() Method36C# | Enum.IsDefined() Method37C# | Enum.HasFlag() Method38C# | Enum.IsDefined() Method
Nunit 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!!