Best Nunit code snippet using NUnit.Framework.GuidDataSource
RandomAttribute.cs
Source:RandomAttribute.cs
...145 _source = new SByteDataSource(_count);146 else if (parmType == typeof(decimal))147 _source = new DecimalDataSource(_count);148 else if (parmType == typeof(Guid))149 _source = new GuidDataSource(_count);150 else if (parmType.GetTypeInfo().IsEnum)151 _source = new EnumDataSource(_count);152 else // Default153 _source = new IntDataSource(_count);154 }155 else if (_source.DataType != parmType && WeConvert(_source.DataType, parmType))156 {157 _source.Distinct = Distinct;158 _source = new RandomDataConverter(_source);159 }160 _source.Distinct = Distinct;161 return _source.GetData(parameter);162 }163 private bool WeConvert(Type sourceType, Type targetType)164 {165 if (targetType == typeof(short) || targetType == typeof(ushort) || targetType == typeof(byte) || targetType == typeof(sbyte))166 return sourceType == typeof(int);167 if (targetType == typeof(decimal))168 return sourceType == typeof(int) || sourceType == typeof(double);169 return false;170 }171 #endregion172 #region Nested DataSource Classes173 #region RandomDataSource174 abstract class RandomDataSource : IParameterDataSource175 {176 protected RandomDataSource(Type dataType) => DataType = dataType;177 public Type DataType { get; protected set; }178 public bool Distinct { get; set; }179 public abstract IEnumerable GetData(IParameterInfo parameter);180 }181 abstract class RandomDataSource<T> : RandomDataSource182 {183 [AllowNull, MaybeNull]184 private readonly T _min;185 [AllowNull, MaybeNull]186 private readonly T _max;187 private readonly int _count;188 private readonly bool _inRange;189 private readonly List<T> previousValues = new List<T>();190 protected RandomDataSource(int count) : base(typeof(T))191 {192 _min = default;193 _max = default;194 _count = count;195 _inRange = false;196 }197 protected RandomDataSource(T min, T max, int count) : base(typeof(T))198 {199 _min = min;200 _max = max;201 _count = count;202 _inRange = true;203 }204 public override IEnumerable GetData(IParameterInfo parameter)205 {206 //Guard.ArgumentValid(parameter.ParameterType == typeof(T), "Parameter type must be " + typeof(T).Name, "parameter");207 var randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);208 Guard.OperationValid(CanUseRange() || !_inRange, $"The value type {parameter.ParameterType} does not support range of values.");209 Guard.OperationValid(!(Distinct && _inRange && !CanBeDistinct(_min!, _max!, _count)), $"The range of values is [{_min}, {_max}[ and the random value count is {_count} so the values cannot be distinct.");210 for (int i = 0; i < _count; i++)211 {212 if (Distinct)213 {214 T next;215 do216 {217 next = _inRange218 ? GetNext(randomizer, _min!, _max!)219 : GetNext(randomizer);220 } while (previousValues.Contains(next));221 previousValues.Add(next);222 yield return next;223 }224 else225 yield return _inRange226 ? GetNext(randomizer, _min!, _max!)227 : GetNext(randomizer);228 }229 }230 protected virtual bool CanUseRange()231 {232 return true;233 }234 protected abstract T GetNext(Randomizer randomizer);235 protected abstract T GetNext(Randomizer randomizer, T min, T max);236 protected abstract bool CanBeDistinct(T min, T max, int count);237 }238 #endregion239 #region RandomDataConverter240 class RandomDataConverter : RandomDataSource241 {242 readonly IParameterDataSource _source;243 public RandomDataConverter(RandomDataSource source) : base(source.DataType)244 {245 _source = source;246 }247 public override IEnumerable GetData(IParameterInfo parameter)248 {249 Type parmType = parameter.ParameterType;250 foreach (object obj in _source.GetData(parameter))251 {252 if (obj is int)253 {254 int ival = (int)obj; // unbox first255 if (parmType == typeof(short))256 yield return (short)ival;257 else if (parmType == typeof(ushort))258 yield return (ushort)ival;259 else if (parmType == typeof(byte))260 yield return (byte)ival;261 else if (parmType == typeof(sbyte))262 yield return (sbyte)ival;263 else if (parmType == typeof(decimal))264 yield return (decimal)ival;265 }266 else if (obj is double)267 {268 double d = (double)obj; // unbox first269 if (parmType == typeof(decimal))270 yield return (decimal)d;271 }272 }273 }274 }275 #endregion276 #region IntDataSource277 class IntDataSource : RandomDataSource<int>278 {279 public IntDataSource(int count) : base(count) { }280 public IntDataSource(int min, int max, int count) : base(min, max, count) { }281 protected override int GetNext(Randomizer randomizer)282 {283 return randomizer.Next();284 }285 protected override int GetNext(Randomizer randomizer, int min, int max)286 {287 return randomizer.Next(min, max);288 }289 protected override bool CanBeDistinct(int min, int max, int count)290 {291 return count <= max - min;292 }293 }294 #endregion295 #region UIntDataSource296 class UIntDataSource : RandomDataSource<uint>297 {298 public UIntDataSource(int count) : base(count) { }299 public UIntDataSource(uint min, uint max, int count) : base(min, max, count) { }300 protected override uint GetNext(Randomizer randomizer)301 {302 return randomizer.NextUInt();303 }304 protected override uint GetNext(Randomizer randomizer, uint min, uint max)305 {306 return randomizer.NextUInt(min, max);307 }308 protected override bool CanBeDistinct(uint min, uint max, int count)309 {310 return count <= max - min;311 }312 }313 #endregion314 #region LongDataSource315 class LongDataSource : RandomDataSource<long>316 {317 public LongDataSource(int count) : base(count) { }318 public LongDataSource(long min, long max, int count) : base(min, max, count) { }319 protected override long GetNext(Randomizer randomizer)320 {321 return randomizer.NextLong();322 }323 protected override long GetNext(Randomizer randomizer, long min, long max)324 {325 return randomizer.NextLong(min, max);326 }327 protected override bool CanBeDistinct(long min, long max, int count)328 {329 return count <= max - min;330 }331 }332 #endregion333 #region ULongDataSource334 class ULongDataSource : RandomDataSource<ulong>335 {336 public ULongDataSource(int count) : base(count) { }337 public ULongDataSource(ulong min, ulong max, int count) : base(min, max, count) { }338 protected override ulong GetNext(Randomizer randomizer)339 {340 return randomizer.NextULong();341 }342 protected override ulong GetNext(Randomizer randomizer, ulong min, ulong max)343 {344 return randomizer.NextULong(min, max);345 }346 protected override bool CanBeDistinct(ulong min, ulong max, int count)347 {348 return (uint)count <= max - min;349 }350 }351 #endregion352 #region ShortDataSource353 class ShortDataSource : RandomDataSource<short>354 {355 public ShortDataSource(int count) : base(count) { }356 public ShortDataSource(short min, short max, int count) : base(min, max, count) { }357 protected override short GetNext(Randomizer randomizer)358 {359 return randomizer.NextShort();360 }361 protected override short GetNext(Randomizer randomizer, short min, short max)362 {363 return randomizer.NextShort(min, max);364 }365 protected override bool CanBeDistinct(short min, short max, int count)366 {367 return count <= max - min;368 }369 }370 #endregion371 #region UShortDataSource372 class UShortDataSource : RandomDataSource<ushort>373 {374 public UShortDataSource(int count) : base(count) { }375 public UShortDataSource(ushort min, ushort max, int count) : base(min, max, count) { }376 protected override ushort GetNext(Randomizer randomizer)377 {378 return randomizer.NextUShort();379 }380 protected override ushort GetNext(Randomizer randomizer, ushort min, ushort max)381 {382 return randomizer.NextUShort(min, max);383 }384 protected override bool CanBeDistinct(ushort min, ushort max, int count)385 {386 return count <= max - min;387 }388 }389 #endregion390 #region DoubleDataSource391 class DoubleDataSource : RandomDataSource<double>392 {393 public DoubleDataSource(int count) : base(count) { }394 public DoubleDataSource(double min, double max, int count) : base(min, max, count) { }395 protected override double GetNext(Randomizer randomizer)396 {397 return randomizer.NextDouble();398 }399 protected override double GetNext(Randomizer randomizer, double min, double max)400 {401 return randomizer.NextDouble(min, max);402 }403 protected override bool CanBeDistinct(double min, double max, int count)404 {405 return true;406 }407 }408 #endregion409 #region FloatDataSource410 class FloatDataSource : RandomDataSource<float>411 {412 public FloatDataSource(int count) : base(count) { }413 public FloatDataSource(float min, float max, int count) : base(min, max, count) { }414 protected override float GetNext(Randomizer randomizer)415 {416 return randomizer.NextFloat();417 }418 protected override float GetNext(Randomizer randomizer, float min, float max)419 {420 return randomizer.NextFloat(min, max);421 }422 protected override bool CanBeDistinct(float min, float max, int count)423 {424 return true;425 }426 }427 #endregion428 #region ByteDataSource429 class ByteDataSource : RandomDataSource<byte>430 {431 public ByteDataSource(int count) : base(count) { }432 public ByteDataSource(byte min, byte max, int count) : base(min, max, count) { }433 protected override byte GetNext(Randomizer randomizer)434 {435 return randomizer.NextByte();436 }437 protected override byte GetNext(Randomizer randomizer, byte min, byte max)438 {439 return randomizer.NextByte(min, max);440 }441 protected override bool CanBeDistinct(byte min, byte max, int count)442 {443 return count <= max - min;444 }445 }446 #endregion447 #region SByteDataSource448 class SByteDataSource : RandomDataSource<sbyte>449 {450 public SByteDataSource(int count) : base(count) { }451 public SByteDataSource(sbyte min, sbyte max, int count) : base(min, max, count) { }452 protected override sbyte GetNext(Randomizer randomizer)453 {454 return randomizer.NextSByte();455 }456 protected override sbyte GetNext(Randomizer randomizer, sbyte min, sbyte max)457 {458 return randomizer.NextSByte(min, max);459 }460 protected override bool CanBeDistinct(sbyte min, sbyte max, int count)461 {462 return count <= max - min;463 }464 }465 #endregion466 #region EnumDataSource467 class EnumDataSource : RandomDataSource468 {469 private readonly int _count;470 private readonly List<object> previousValues = new List<object>();471 public EnumDataSource(int count) : base(typeof(Enum))472 {473 _count = count;474 }475 public override IEnumerable GetData(IParameterInfo parameter)476 {477 Guard.ArgumentValid(parameter.ParameterType.GetTypeInfo().IsEnum, "EnumDataSource requires an enum parameter", nameof(parameter));478 Randomizer randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);479 DataType = parameter.ParameterType;480 int valueCount = Enum.GetValues(DataType).Cast<int>().Distinct().Count();481 Guard.OperationValid(!(Distinct && _count > valueCount), $"The enum \"{DataType.Name}\" has {valueCount} values and the random value count is {_count} so the values cannot be distinct.");482 for (int i = 0; i < _count; i++)483 {484 if (Distinct)485 {486 object next;487 do488 {489 next = randomizer.NextEnum(parameter.ParameterType);490 } while (previousValues.Contains(next));491 previousValues.Add(next);492 yield return next;493 }494 else495 yield return randomizer.NextEnum(parameter.ParameterType);496 }497 }498 }499 #endregion500 #region DecimalDataSource501 class DecimalDataSource : RandomDataSource<decimal>502 {503 public DecimalDataSource(int count) : base(count) { }504 protected override decimal GetNext(Randomizer randomizer)505 {506 return randomizer.NextDecimal();507 }508 protected override decimal GetNext(Randomizer randomizer, decimal min, decimal max)509 {510 return randomizer.NextDecimal(min, max);511 }512 protected override bool CanBeDistinct(decimal min, decimal max, int count)513 {514 return true;515 }516 }517 #endregion518 #region GuidDataSource519 class GuidDataSource : RandomDataSource<Guid>520 {521 public GuidDataSource(int count) : base(count) { }522 protected override Guid GetNext(Randomizer randomizer)523 {524 return randomizer.NextGuid();525 }526 protected override Guid GetNext(Randomizer randomizer, Guid min, Guid max)527 {528 throw new NotSupportedException($"{typeof(Guid)} does not support range of parameters being specified.");529 }530 protected override bool CanBeDistinct(Guid min, Guid max, int count)531 {532 throw new NotSupportedException($"{typeof(Guid)} does not support range of parameters being specified.");533 }534 protected override bool CanUseRange()535 {...
GuidDataSource
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 void TestMethod1()10 {11 GuidDataSource ds = new GuidDataSource();12 Guid[] data = ds.GetData();13 }14 }15}
GuidDataSource
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(GuidDataSource), "Guids")]10 public void TestMethod1(Guid guid)11 {12 Console.WriteLine(guid);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 {25 {26 yield return Guid.NewGuid();27 yield return Guid.NewGuid();28 yield return Guid.NewGuid();29 yield return Guid.NewGuid();30 yield return Guid.NewGuid();31 }32 }33 }34}35using NUnit.Framework;36using System;37using System.Collections.Generic;38using System.Linq;39using System.Text;40using System.Threading.Tasks;41{42 {43 [TestCaseSource(typeof(GuidDataSource), "Guids")]44 public void TestMethod1(Guid guid)45 {46 Console.WriteLine(guid);47 }48 }49}50using NUnit.Framework;51using System;52using System.Collections.Generic;53using System.Linq;54using System.Text;55using System.Threading.Tasks;56{57 {58 {59 {60 yield return Guid.NewGuid();61 yield return Guid.NewGuid();62 yield return Guid.NewGuid();63 yield return Guid.NewGuid();64 yield return Guid.NewGuid();65 }66 }67 }68}69using NUnit.Framework;70using System;71using System.Collections.Generic;72using System.Linq;73using System.Text;74using System.Threading.Tasks;75{76 {77 [TestCaseSource(typeof(GuidDataSource), "Guids")]78 public void TestMethod1(Guid guid)79 {
GuidDataSource
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 [Test, TestCaseSource(typeof(GuidDataSource), "Guids")]10 public void TestGuid(Guid guid)11 {12 Console.WriteLine(guid);13 }14 {15 {16 for (int i = 0; i < 10; i++)17 {18 yield return Guid.NewGuid();19 }20 }21 }22 }23}
GuidDataSource
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 [Test, TestCaseSource(typeof(GuidDataSource), "GuidValues")]10 public void GuidTest(Guid guid)11 {12 Console.WriteLine(guid);13 }14 {15 {16 yield return Guid.NewGuid();17 yield return Guid.NewGuid();18 yield return Guid.NewGuid();19 }20 }21 }22}23{b5f5f5d5-9b7d-4a4f-8e4d-4d4a4f4d4f4d}24{b5f5f5d5-9b7d-4a4f-8e4d-4d4a4f4d4f4d}25{b5f5f5d5-9b7d-4a4f-8e4d-4d4a4f4d4f4d}26public static IEnumerable<Guid> GuidValues()27{28 yield return Guid.NewGuid();29 yield return Guid.NewGuid();30 yield return Guid.NewGuid();31}32public static IEnumerable<Guid> GuidValues()33{34 yield return Guid.NewGuid();35 yield return Guid.NewGuid();36 yield return Guid.NewGuid();37}
GuidDataSource
Using AI Code Generation
1using System;2using NUnit.Framework;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 [Test, TestCaseSource(typeof(GuidDataSource), "GetGuid")]10 public void GuidTest(Guid guid)11 {12 Console.WriteLine(guid);13 }14 {15 {16 yield return new Guid("b1c2d3e4-f5a6-b7c8-d9e0-f1a2b3c4d5e6");17 yield return new Guid("b1c2d3e4-f5a6-b7c8-d9e0-f1a2b3c4d5e6");18 yield return new Guid("b1c2d3e4-f5a6-b7c8-d9e0-f1a2b3c4d5e6");19 yield return new Guid("b1c2d3e4-f5a6-b7c8-d9e0-f1a2b3c4d5e6");20 yield return new Guid("b1c2d3e4-f5a6-b7c8-d9e0-f1a2b3c4d5e6");21 }22 }23 }24}
GuidDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3{4 {5 public void TestMethod()6 {7 GuidDataSource gds = new GuidDataSource();8 Guid guid = gds.GetGuid();9 Console.WriteLine(guid);10 }11 }12}13Microsoft (R) Visual C# Compiler version 1.0.3705.601814for Microsoft (R) .NET Framework version 2.0.50727.1433155.cs(3,7): error CS0246: The type or namespace name `NUnit' could not be found. Are you missing an assembly reference?165.cs(4,7): error CS0246: The type or namespace name `NUnit' could not be found. Are you missing an assembly reference?175.cs(7,2): error CS0246: The type or namespace name `TestFixture' could not be found. Are you missing an assembly reference?185.cs(9,2): error CS0246: The type or namespace name `Test' could not be found. Are you missing an assembly reference?195.cs(13,9): error CS0246: The type or namespace name `GuidDataSource' could not be found. Are you missing an assembly reference?205.cs(13,27): error CS0246: The type or namespace name `GuidDataSource' could not be found. Are you missing an assembly reference?21using NUnit.Framework;22Microsoft (R) Visual C# Compiler version 1.0.3705.601823for Microsoft (R) .NET Framework version 2.0.50727.1433
GuidDataSource
Using AI Code Generation
1using NUnit.Framework;2{3 {4 public void TestMethod()5 {6 Assert.AreEqual(5, 5);7 }8 }9}10using NUnit.Framework;11{12 {13 public void TestMethod()14 {15 Assert.AreEqual(6, 6);16 }17 }18}19using NUnit.Framework;20{21 {22 public void TestMethod()23 {24 Assert.AreEqual(7, 7);25 }26 }27}28using NUnit.Framework;29{30 {31 public void TestMethod()32 {33 Assert.AreEqual(8, 8);34 }35 }36}37using NUnit.Framework;38{39 {40 public void TestMethod()41 {42 Assert.AreEqual(9, 9);43 }44 }45}46using NUnit.Framework;47{48 {49 public void TestMethod()50 {51 Assert.AreEqual(10, 10);52 }53 }54}55using NUnit.Framework;56{57 {58 public void TestMethod()59 {60 Assert.AreEqual(11, 11);61 }62 }63}64using NUnit.Framework;65{66 {67 public void TestMethod()68 {
GuidDataSource
Using AI Code Generation
1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using System.Data.SqlClient;8using System.Data;9using System.Configuration;10using System.IO;11{12 {13 [Test, TestCaseSource("GetGuidData")]14 public void TestMethod1(string guid)15 {16 var guid1 = Guid.Parse(guid);17 var guid2 = Guid.NewGuid();18 Assert.AreEqual(guid1, guid2);19 }20 public static IEnumerable<string> GetGuidData()21 {22 var lines = File.ReadAllLines(@"C:\Users\Public\Documents\GuidData.txt");23 foreach (var line in lines)24 {25 yield return line;26 }27 }28 }29}
GuidDataSource
Using AI Code Generation
1using System;2using System.Data;3using System.Data.OleDb;4using System.Data.SqlClient;5using System.Diagnostics;6using System.IO;7using System.Reflection;8using System.Text;9using System.Threading;10using System.Windows.Forms;11using System.Xml;12using NUnit.Framework;13{14 {15 public GuidDataSource()16 {17 }18 public IEnumerable GetData(MethodInfo method)19 {20 yield return new object[] { Guid.NewGuid() };21 }22 public string GetDisplayName(MethodInfo method, object[] data)23 {24 return "GUID";25 }26 }27}28using NUnit.Framework;29using System;30using System.Data;31using System.Data.OleDb;32using System.Data.SqlClient;33using System.Diagnostics;34using System.IO;35using System.Reflection;36using System.Text;37using System.Threading;38using System.Windows.Forms;39using System.Xml;40{41 {42 [Test, TestCaseSource(typeof(GuidDataSource))]43 public void TestMethod(Guid guid)44 {45 Assert.IsNotNull(guid);46 }47 }48}49using System;50using System.Data;51using System.Data.OleDb;52using System.Data.SqlClient;53using System.Diagnostics;54using System.IO;55using System.Reflection;56using System.Text;57using System.Threading;58using System.Windows.Forms;59using System.Xml;60using NUnit.Framework;61{62 {63 [Test, TestCaseSource(typeof(GuidDataSource))]64 public void TestMethod(Guid guid)65 {66 Assert.IsNotNull(guid);67 }68 }69}
GuidDataSource
Using AI Code Generation
1using NUnit.Framework;2{3 public void Test()4 {5 GuidDataSource dataSource = new GuidDataSource();6 Assert.AreEqual(dataSource.GetGuid(), new Guid("1B6E11F6-9E9C-4F5F-8FCD-6E1D6F1B6E11"));7 }8}9using NUnit.Framework;10{11 [TestCaseSource(typeof(GuidDataSource), "GetGuid")]12 public void Test(Guid guid)13 {14 Assert.AreEqual(guid, new Guid("1B6E11F6-9E9C-4F5F-8FCD-6E1D6F1B6E11"));15 }16}17using NUnit.Framework;18{19 [TestCaseSource(typeof(GuidDataSource), "GetGuid", new object[] { "1B6E11F6-9E9C-4F5F-8FCD-6E1D6F1B6E11" })]20 public void Test(Guid guid)21 {22 Assert.AreEqual(guid, new Guid("1B6E11F6-9E9C-4F5F-8FCD-6E1D6F1B6E11"));23 }24}25using NUnit.Framework;26{27 [TestCaseSource(typeof(GuidDataSource), "GetGuid", new object[] { "1B6E11F6-9E9C-4F5F-8FCD-6E1D6F1B6E11" })]28 public void Test(Guid guid)29 {30 Assert.AreEqual(guid, new Guid("1B6E11F6-9E9C-4F5F-8FCD-6E1D6F1B6E11"));31 }32}33using NUnit.Framework;34{35 [TestCaseSource(typeof(GuidDataSource), "
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!!