How to use DecimalDataSource class of NUnit.Framework package

Best Nunit code snippet using NUnit.Framework.DecimalDataSource

RandomAttribute.cs

Source:RandomAttribute.cs Github

copy

Full Screen

...163 _source = new ByteDataSource(_count);164 else if (parmType == typeof(sbyte))165 _source = new SByteDataSource(_count);166 else if (parmType == typeof(decimal))167 _source = new DecimalDataSource(_count);168 else if (parmType.GetTypeInfo().IsEnum)169 _source = new EnumDataSource(_count);170 else // Default171 _source = new IntDataSource(_count);172 }173 else if (_source.DataType != parmType && WeConvert(_source.DataType, parmType))174 {175 _source = new RandomDataConverter(_source);176 }177 return _source.GetData(parameter);178 //// Copy the random _values into the data array179 //// and call the base class which may need to180 //// convert them to another type.181 //this.data = new object[values.Count];182 //for (int i = 0; i < values.Count; i++)183 // this.data[i] = values[i];184 //return base.GetData(parameter);185 }186 private bool WeConvert(Type sourceType, Type targetType)187 {188 if (targetType == typeof(short) || targetType == typeof(ushort) || targetType == typeof(byte) || targetType == typeof(sbyte))189 return sourceType == typeof(int);190 if (targetType == typeof(decimal))191 return sourceType == typeof(int) || sourceType == typeof(double);192 193 return false;194 }195 #endregion196 #region Nested DataSource Classes197 #region RandomDataSource198 abstract class RandomDataSource : IParameterDataSource199 {200 public Type DataType { get; protected set; }201 public abstract IEnumerable GetData(IParameterInfo parameter);202 }203 abstract class RandomDataSource<T> : RandomDataSource204 {205 private T _min;206 private T _max;207 private int _count;208 private bool _inRange;209 protected Randomizer _randomizer;210 protected RandomDataSource(int count)211 {212 _count = count;213 _inRange = false;214 DataType = typeof(T);215 }216 protected RandomDataSource(T min, T max, int count)217 {218 _min = min;219 _max = max;220 _count = count;221 _inRange = true;222 DataType = typeof(T);223 }224 public override IEnumerable GetData(IParameterInfo parameter)225 {226 //Guard.ArgumentValid(parameter.ParameterType == typeof(T), "Parameter type must be " + typeof(T).Name, "parameter");227 _randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);228 for (int i = 0; i < _count; i++)229 yield return _inRange230 ? GetNext(_min, _max)231 : GetNext();232 }233 protected abstract T GetNext();234 protected abstract T GetNext(T min, T max);235 }236 #endregion237 #region RandomDataConverter238 class RandomDataConverter : RandomDataSource239 {240 IParameterDataSource _source;241 public RandomDataConverter(IParameterDataSource source)242 {243 _source = source;244 }245 public override IEnumerable GetData(IParameterInfo parameter)246 {247 Type parmType = parameter.ParameterType;248 foreach (object obj in _source.GetData(parameter))249 {250 if (obj is int)251 {252 int ival = (int)obj; // unbox first253 if (parmType == typeof(short))254 yield return (short)ival;255 else if (parmType == typeof(ushort))256 yield return (ushort)ival;257 else if (parmType == typeof(byte))258 yield return (byte)ival;259 else if (parmType == typeof(sbyte))260 yield return (sbyte)ival;261 else if (parmType == typeof(decimal))262 yield return (decimal)ival;263 }264 else if (obj is double)265 {266 double d = (double)obj; // unbox first267 if (parmType == typeof(decimal))268 yield return (decimal)d;269 }270 }271 }272 }273 #endregion274 #region IntDataSource275 class IntDataSource : RandomDataSource<int>276 {277 public IntDataSource(int count) : base(count) { }278 public IntDataSource(int min, int max, int count) : base(min, max, count) { }279 protected override int GetNext()280 {281 return _randomizer.Next();282 }283 protected override int GetNext(int min, int max)284 {285 return _randomizer.Next(min, max);286 }287 }288 #endregion289 #region UIntDataSource290 class UIntDataSource : RandomDataSource<uint>291 {292 public UIntDataSource(int count) : base(count) { }293 public UIntDataSource(uint min, uint max, int count) : base(min, max, count) { }294 protected override uint GetNext()295 {296 return _randomizer.NextUInt();297 }298 protected override uint GetNext(uint min, uint max)299 {300 return _randomizer.NextUInt(min, max);301 }302 }303 #endregion304 #region LongDataSource305 class LongDataSource : RandomDataSource<long>306 {307 public LongDataSource(int count) : base(count) { }308 public LongDataSource(long min, long max, int count) : base(min, max, count) { }309 protected override long GetNext()310 {311 return _randomizer.NextLong();312 }313 protected override long GetNext(long min, long max)314 {315 return _randomizer.NextLong(min, max);316 }317 }318 #endregion319 #region ULongDataSource320 class ULongDataSource : RandomDataSource<ulong>321 {322 public ULongDataSource(int count) : base(count) { }323 public ULongDataSource(ulong min, ulong max, int count) : base(min, max, count) { }324 protected override ulong GetNext()325 {326 return _randomizer.NextULong();327 }328 protected override ulong GetNext(ulong min, ulong max)329 {330 return _randomizer.NextULong(min, max);331 }332 }333 #endregion334 #region ShortDataSource335 class ShortDataSource : RandomDataSource<short>336 {337 public ShortDataSource(int count) : base(count) { }338 public ShortDataSource(short min, short max, int count) : base(min, max, count) { }339 protected override short GetNext()340 {341 return _randomizer.NextShort();342 }343 protected override short GetNext(short min, short max)344 {345 return _randomizer.NextShort(min, max);346 }347 }348 #endregion349 #region UShortDataSource350 class UShortDataSource : RandomDataSource<ushort>351 {352 public UShortDataSource(int count) : base(count) { }353 public UShortDataSource(ushort min, ushort max, int count) : base(min, max, count) { }354 protected override ushort GetNext()355 {356 return _randomizer.NextUShort();357 }358 protected override ushort GetNext(ushort min, ushort max)359 {360 return _randomizer.NextUShort(min, max);361 }362 }363 #endregion364 #region DoubleDataSource365 class DoubleDataSource : RandomDataSource<double>366 {367 public DoubleDataSource(int count) : base(count) { }368 public DoubleDataSource(double min, double max, int count) : base(min, max, count) { }369 protected override double GetNext()370 {371 return _randomizer.NextDouble();372 }373 protected override double GetNext(double min, double max)374 {375 return _randomizer.NextDouble(min, max);376 }377 }378 #endregion379 #region FloatDataSource380 class FloatDataSource : RandomDataSource<float>381 {382 public FloatDataSource(int count) : base(count) { }383 public FloatDataSource(float min, float max, int count) : base(min, max, count) { }384 protected override float GetNext()385 {386 return _randomizer.NextFloat();387 }388 protected override float GetNext(float min, float max)389 {390 return _randomizer.NextFloat(min, max);391 }392 }393 #endregion394 #region ByteDataSource395 class ByteDataSource : RandomDataSource<byte>396 {397 public ByteDataSource(int count) : base(count) { }398 public ByteDataSource(byte min, byte max, int count) : base(min, max, count) { }399 protected override byte GetNext()400 {401 return _randomizer.NextByte();402 }403 protected override byte GetNext(byte min, byte max)404 {405 return _randomizer.NextByte(min, max);406 }407 }408 #endregion409 #region SByteDataSource410 class SByteDataSource : RandomDataSource<sbyte>411 {412 public SByteDataSource(int count) : base(count) { }413 public SByteDataSource(sbyte min, sbyte max, int count) : base(min, max, count) { }414 protected override sbyte GetNext()415 {416 return _randomizer.NextSByte();417 }418 protected override sbyte GetNext(sbyte min, sbyte max)419 {420 return _randomizer.NextSByte(min, max);421 }422 }423 #endregion424 #region EnumDataSource425 class EnumDataSource : RandomDataSource426 {427 private int _count;428 public EnumDataSource(int count)429 {430 _count = count;431 DataType = typeof(Enum);432 }433 public override IEnumerable GetData(IParameterInfo parameter)434 {435 Guard.ArgumentValid(parameter.ParameterType.GetTypeInfo().IsEnum, "EnumDataSource requires an enum parameter", "parameter");436 Randomizer randomizer = Randomizer.GetRandomizer(parameter.ParameterInfo);437 DataType = parameter.ParameterType;438 for (int i = 0; i < _count; i++ )439 yield return randomizer.NextEnum(parameter.ParameterType);440 }441 }442 #endregion443 #region DecimalDataSource444 // Currently, Randomizer doesn't implement methods for decimal445 // so we use random Ulongs and convert them. This doesn't cover446 // the full range of decimal, so it's temporary.447 class DecimalDataSource : RandomDataSource<decimal>448 {449 public DecimalDataSource(int count) : base(count) { }450 public DecimalDataSource(decimal min, decimal max, int count) : base(min, max, count) { }451 protected override decimal GetNext()452 {453 return _randomizer.NextDecimal();454 }455 protected override decimal GetNext(decimal min, decimal max)456 {457 return _randomizer.NextDecimal(min, max);458 }459 }460 #endregion461 #endregion462 }463}...

Full Screen

Full Screen

DecimalDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 public void Test1()10 {11 decimal d = 10.0m;12 decimal e = 20.0m;13 decimal f = 30.0m;14 decimal g = 40.0m;15 decimal h = 50.0m;16 decimal i = 60.0m;17 decimal j = 70.0m;18 decimal k = 80.0m;19 decimal l = 90.0m;20 decimal m = 100.0m;21 decimal n = 110.0m;22 decimal o = 120.0m;23 decimal p = 130.0m;24 decimal q = 140.0m;25 decimal r = 150.0m;26 decimal s = 160.0m;27 decimal t = 170.0m;28 decimal u = 180.0m;29 decimal v = 190.0m;30 decimal w = 200.0m;31 decimal x = 210.0m;32 decimal y = 220.0m;33 decimal z = 230.0m;34 decimal a = 240.0m;35 decimal b = 250.0m;36 decimal c = 260.0m;37 decimal ab = 270.0m;38 decimal bc = 280.0m;39 decimal cd = 290.0m;40 decimal de = 300.0m;41 decimal ef = 310.0m;42 decimal fg = 320.0m;43 decimal gh = 330.0m;44 decimal hi = 340.0m;45 decimal ij = 350.0m;46 decimal jk = 360.0m;47 decimal kl = 370.0m;48 decimal lm = 380.0m;49 decimal mn = 390.0m;50 decimal no = 400.0m;51 decimal op = 410.0m;52 decimal pq = 420.0m;53 decimal qr = 430.0m;54 decimal rs = 440.0m;

Full Screen

Full Screen

DecimalDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 [TestCaseSource(typeof(DecimalDataSourceClass), "GetDecimalData")]10 public void TestMethod1(decimal a, decimal b, decimal c)11 {12 Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);13 }14 }15 {16 public static IEnumerable<TestCaseData> GetDecimalData()17 {18 yield return new TestCaseData(2.5, 2.5, 5);19 yield return new TestCaseData(1.5, 1.5, 3);20 yield return new TestCaseData(2.5, 1.5, 4);21 }22 }23}24using NUnit.Framework;25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30{31 {32 [TestCaseSource(typeof(StringDataSourceClass), "GetStringData")]33 public void TestMethod1(string a, string b, string c)34 {35 Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);36 }37 }38 {39 public static IEnumerable<TestCaseData> GetStringData()40 {41 yield return new TestCaseData("NUnit", "Framework", "NUnit Framework");

Full Screen

Full Screen

DecimalDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2{3 {4 {5 {6 yield return new TestCaseData(0.1m).Returns(0.1m);7 yield return new TestCaseData(0.2m).Returns(0.2m);8 yield return new TestCaseData(0.3m).Returns(0.3m);9 }10 }11 }12}13using NUnit.Framework;14{15 {16 {17 {18 yield return new TestCaseData("Test1").Returns("Test1");19 yield return new TestCaseData("Test2").Returns("Test2");20 yield return new TestCaseData("Test3").Returns("Test3");21 }22 }23 }24}25using NUnit.Framework;26{27 {28 {29 {30 yield return new TestCaseData(new { Id = 1, Name = "Test1" }).Returns(new { Id = 1, Name = "Test1" });31 yield return new TestCaseData(new { Id = 2, Name = "Test2" }).Returns(new { Id = 2, Name = "Test2" });32 yield return new TestCaseData(new { Id = 3, Name = "Test3" }).Returns(new { Id = 3, Name = "Test3" });33 }34 }35 }36}37using NUnit.Framework;38{39 {40 {41 {42 yield return new TestCaseData(new int[] { 1, 2, 3 }).Returns(new int[] { 1, 2, 3 });43 yield return new TestCaseData(new int[] { 4, 5, 6 }).Returns(new int[] { 4, 5, 6 });44 yield return new TestCaseData(new int[] { 7, 8, 9 }).Returns(new int[]

Full Screen

Full Screen

DecimalDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2{3 {4 public void TestMethod()5 {6 decimal d = 1.23M;7 Assert.AreEqual(1.23M, d, "failed");8 }9 }10}11 at NunitTest.TestClass.TestMethod() in C:\Users\user\Documents\Visual Studio 2015\Projects\NunitTest\NunitTest\5.cs:line 1212Cannot resolve symbol ‘DecimalDataSource’ (CS0246)13Cannot resolve symbol ‘DecimalDataSource’ (CS0246)14Cannot resolve symbol ‘DecimalDataSource’ (CS0246)15Cannot resolve symbol ‘DecimalDataSource’ (CS0246)

Full Screen

Full Screen

DecimalDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2{3 {4 public void Test()5 {6 var dataSource = new DecimalDataSource(5.5m, 5.5m);7 Assert.AreEqual(dataSource.GetData(), 5.5m);8 }9 }10}

Full Screen

Full Screen

DecimalDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2using System;3{4 {5 private readonly string _sourceName;6 public DecimalDataSourceAttribute(string sourceName)7 {8 _sourceName = sourceName;9 }10 public override NUnit.Framework.Interfaces.IDataSource GetData(NUnit.Framework.Interfaces.IMethodInfo method)11 {12 return new DecimalDataSource(_sourceName);13 }14 }15 {16 private readonly string _sourceName;17 public DecimalDataSource(string sourceName)18 {19 _sourceName = sourceName;20 }21 public System.Collections.IEnumerable GetData(NUnit.Framework.Interfaces.IMethodInfo method)22 {23 string[] lines = System.IO.File.ReadAllLines(_sourceName);24 foreach (string line in lines)25 {26 yield return new TestCaseData(Convert.ToDecimal(line));27 }28 }29 }30}31using NUnit.Framework;32using System;33{34 {35 private readonly string _sourceName;36 public DoubleDataSourceAttribute(string sourceName)37 {38 _sourceName = sourceName;39 }40 public override NUnit.Framework.Interfaces.IDataSource GetData(NUnit.Framework.Interfaces.IMethodInfo method)41 {42 return new DoubleDataSource(_sourceName);43 }44 }45 {46 private readonly string _sourceName;47 public DoubleDataSource(string sourceName)48 {49 _sourceName = sourceName;50 }51 public System.Collections.IEnumerable GetData(NUnit.Framework.Interfaces.IMethodInfo method)52 {53 string[] lines = System.IO.File.ReadAllLines(_sourceName);54 foreach (string line in lines)55 {56 yield return new TestCaseData(Convert.ToDouble(line));57 }58 }59 }60}61using NUnit.Framework;62using System;63{64 {65 private readonly string _sourceName;66 public Int16DataSourceAttribute(string sourceName)67 {68 _sourceName = sourceName;69 }70 public override NUnit.Framework.Interfaces.IDataSource GetData(NUnit.Framework.Interfaces.IM

Full Screen

Full Screen

Nunit tutorial

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.

Chapters

  1. NUnit Environment Setup - All the prerequisites and setup environments are provided to help you begin with NUnit testing.
  2. NUnit With Selenium - Learn how to use the NUnit framework with Selenium for automation testing and its installation.
  3. Selenium WebDriver Commands in NUnit - Leverage your knowledge about the top 28 Selenium WebDriver Commands in NUnit For Test Automation. It covers web browser commands, web element commands, and drop-down commands.
  4. NUnit Parameterized Unit Tests - Tests on varied combinations may lead to code duplication or redundancy. This chapter discusses how NUnit Parameterized Unit Tests and their methods can help avoid code duplication.
  5. NUnit Asserts - Learn about the usage of assertions in NUnit using Selenium
  6. NUnit Annotations - Learn how to use and execute NUnit annotations for Selenium Automation Testing
  7. Generating Test Reports In NUnit - Understand how to use extent reports and generate reports with NUnit and Selenium WebDriver. Also, look into how to capture screenshots in NUnit extent reports.
  8. Parallel Execution In NUnit - Parallel testing helps to reduce time consumption while executing a test. Deep dive into the concept of Specflow Parallel Execution in NUnit.

NUnit certification -

You can also check out the LambdaTest Certification to enhance your learning in Selenium Automation Testing using the NUnit framework.

YouTube

Watch this tutorial on the LambdaTest Channel to learn how to set up the NUnit framework, run tests and also execute parallel testing.

Run Nunit automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful