How to use FloatDataSource class of NUnit.Framework package

Best Nunit code snippet using NUnit.Framework.FloatDataSource

RandomAttribute.cs

Source:RandomAttribute.cs Github

copy

Full Screen

...110        /// Construct a set of floats within a specified range111        /// </summary>112        public RandomAttribute(float min, float max, int count)113        {114            _source = new FloatDataSource(min, max, count);115        }116        /// <summary>117        /// Construct a set of bytes within a specified range118        /// </summary>119        public RandomAttribute(byte min, byte max, int count)120        {121            _source = new ByteDataSource(min, max, count);122        }123        /// <summary>124        /// Construct a set of sbytes within a specified range125        /// </summary>126        //[CLSCompliant(false)]127        public RandomAttribute(sbyte min, sbyte max, int count)128        {129            _source = new SByteDataSource(min, max, count);130        }131        #endregion132        #region IParameterDataSource Interface133        /// <summary>134        /// Get the collection of _values to be used as arguments.135        /// </summary>136        public IEnumerable GetData(IParameterInfo parameter)137        {138            // Since a separate Randomizer is used for each parameter,139            // we can't fill in the data in the constructor of the140            // attribute. Only now, when GetData is called, do we have141            // sufficient information to create the values in a 142            // repeatable manner.143            Type parmType = parameter.ParameterType;144            if (_source == null)145            {146                if (parmType == typeof(int))147                    _source = new IntDataSource(_count);148                else if (parmType == typeof(uint))149                    _source = new UIntDataSource(_count);150                else if (parmType == typeof(long))151                    _source = new LongDataSource(_count);152                else if (parmType == typeof(ulong))153                    _source = new ULongDataSource(_count);154                else if (parmType == typeof(short))155                    _source = new ShortDataSource(_count);156                else if (parmType == typeof(ushort))157                    _source = new UShortDataSource(_count);158                else if (parmType == typeof(double))159                    _source = new DoubleDataSource(_count);160                else if (parmType == typeof(float))161                    _source = new FloatDataSource(_count);162                else if (parmType == typeof(byte))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) { }...

Full Screen

Full Screen

FloatDataSource

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        [Test, TestCaseSource("GetFloatData")]10        public void AddFloatTest(float a, float b, float expected)11        {12            float actual = a + b;13            Assert.AreEqual(expected, actual);14        }15        {16            new object[] { 1.1f, 2.2f, 3.3f },17            new object[] { 2.2f, 3.3f, 5.5f },18            new object[] { 3.3f, 4.4f, 7.7f },19            new object[] { 4.4f, 5.5f, 9.9f },20            new object[] { 5.5f, 6.6f, 12.1f }21        };22    }23}24at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)25   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)26   at System.Reflection.RuntimeMethodInfo.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)27   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)28   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)29   at NUnit.Framework.Internal.Reflect.InvokeMethod(MethodInfo method, Object fixture, Object[] args)30   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)31   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.Execute(TestExecutionContext context)32   at NUnit.Framework.Internal.Commands.SetUpTearDownCommand.Execute(TestExecutionContext context)33   at NUnit.Framework.Internal.Commands.SetUpTearDownCommand.Execute(TestExecutionContext context)34   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)35   at NUnit.Framework.Internal.Commands.SetUpTearDownCommand.Execute(TestExecutionContext context)

Full Screen

Full Screen

FloatDataSource

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        [TestCase(1.1, 1.1)]10        [TestCase(1.1, 1.2)]11        public void TestMethod1(float a, float b)12        {13            Assert.AreEqual(a, b);14        }15    }16}17using NUnit.Framework;18using System;19using System.Collections.Generic;20using System.Linq;21using System.Text;22using System.Threading.Tasks;23{24    {25        [TestCase(1.1, 1.1)]26        [TestCase(1.1, 1.2)]27        public void TestMethod1(double a, double b)28        {29            Assert.AreEqual(a, b);30        }31    }32}33using NUnit.Framework;34using System;35using System.Collections.Generic;36using System.Linq;37using System.Text;38using System.Threading.Tasks;39{40    {41        [TestCase(1.1, 1.1)]42        [TestCase(1.1, 1.2)]43        public void TestMethod1(decimal a, decimal b)44        {45            Assert.AreEqual(a, b);46        }47    }48}49using NUnit.Framework;50using System;51using System.Collections.Generic;52using System.Linq;53using System.Text;54using System.Threading.Tasks;55{56    {57        [TestCase("a", "a")]58        [TestCase("a", "b")]59        public void TestMethod1(string a, string b)60        {61            Assert.AreEqual(a, b);62        }63    }64}65using NUnit.Framework;66using System;67using System.Collections.Generic;68using System.Linq;69using System.Text;70using System.Threading.Tasks;71{72    {73        [TestCase("a", "

Full Screen

Full Screen

FloatDataSource

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections;3using System.Collections.Generic;4using System.Text;5using NUnit.Framework;6{7    {8        {9            {10                yield return new TestCaseData(1.2f, 1.2f);11                yield return new TestCaseData(1.3f, 1.3f);12                yield return new TestCaseData(1.4f, 1.4f);13            }14        }15    }16}17using System;18using System.Collections;19using System.Collections.Generic;20using System.Text;21using NUnit.Framework;22{23    {24        {25            {26                yield return new TestCaseData(1.2, 1.2);27                yield return new TestCaseData(1.3, 1.3);28                yield return new TestCaseData(1.4, 1.4);29            }30        }31    }32}33using System;34using System.Collections;35using System.Collections.Generic;36using System.Text;37using NUnit.Framework;38{39    {40        {41            {42                yield return new TestCaseData("abc", "abc");43                yield return new TestCaseData("def", "def");44                yield return new TestCaseData("ghi", "ghi");45            }46        }47    }48}49using System;50using System.Collections;51using System.Collections.Generic;52using System.Text;53using NUnit.Framework;54{55    {56        {57            {58                yield return new TestCaseData(1.2m, 1.2m);59                yield return new TestCaseData(1.3m, 1.3m);60                yield return new TestCaseData(1.4m, 1.4m);61            }62        }63    }64}65using System;66using System.Collections;67using System.Collections.Generic;68using System.Text;

Full Screen

Full Screen

FloatDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2using System;3{4    {5        [TestCase(2.5, 3.5, 6)]6        [TestCase(2.5, -3.5, -1)]7        [TestCase(-2.5, 3.5, 1)]8        [TestCase(-2.5, -3.5, -6)]9        public void TestAdd(double a, double b, double expected)10        {11            var calc = new Calculator();12            var result = calc.Add(a, b);13            Assert.AreEqual(expected, result);14        }15    }16}17using NUnit.Framework;18using System;19{20    {21        [TestCase(2.5, 3.5, 6)]22        [TestCase(2.5, -3.5, -1)]23        [TestCase(-2.5, 3.5, 1)]24        [TestCase(-2.5, -3.5, -6)]25        public void TestAdd(double a, double b, double expected)26        {27            var calc = new Calculator();28            var result = calc.Add(a, b);29            Assert.AreEqual(expected, result);30        }31    }32}33using NUnit.Framework;34using System;35{36    {37        [TestCase(2.5, 3.5, 6)]38        [TestCase(2.5, -3.5, -1)]39        [TestCase(-2.5, 3.5, 1)]40        [TestCase(-2.5, -3.5, -6)]41        public void TestAdd(double a, double b, double expected)42        {43            var calc = new Calculator();44            var result = calc.Add(a, b);45            Assert.AreEqual(expected, result);46        }47    }48}

Full Screen

Full Screen

FloatDataSource

Using AI Code Generation

copy

Full Screen

1using System;2using NUnit.Framework;3using NUnit.Framework.SyntaxHelpers;4{5    {6        [Test, FloatDataSource(1.0, 2.0, 3.0, 4.0, 5.0)]7        public void TestFloatDataSource1(float value)8        {9            Assert.That(value, Is.GreaterThan(0.0));10        }11    }12}13using System;14using NUnit.Framework;15using NUnit.Framework.SyntaxHelpers;16{17    {18        [Test, DoubleDataSource(1.0, 2.0, 3.0, 4.0, 5.0)]19        public void TestDoubleDataSource1(double value)20        {21            Assert.That(value, Is.GreaterThan(0.0));22        }23    }24}25using System;26using NUnit.Framework;27using NUnit.Framework.SyntaxHelpers;28{29    {30        [Test, DecimalDataSource(1.0, 2.0, 3.0, 4.0, 5.0)]31        public void TestDecimalDataSource1(decimal value)32        {33            Assert.That(value, Is.GreaterThan(0.0));34        }35    }36}37using System;38using NUnit.Framework;39using NUnit.Framework.SyntaxHelpers;40{41    {42        [Test, StringDataSource("a", "b", "c", "d", "e")]43        public void TestStringDataSource1(string value)44        {45            Assert.That(value, Is.Not.Empty);46        }47    }48}49using System;50using NUnit.Framework;51using NUnit.Framework.SyntaxHelpers;52{53    {54        {55        }56        [Test, EnumDataSource(typeof(TestEnum

Full Screen

Full Screen

FloatDataSource

Using AI Code Generation

copy

Full Screen

1using System;2using NUnit.Framework;3{4    {5        public void TestMethod()6        {7            var f = new FloatDataSource(1.1f);8            Assert.AreEqual(1.1f, f.GetValue());9        }10    }11}12using System;13using NUnit.Framework;14{15    {16        public void TestMethod()17        {18            var f = new FloatDataSource(1.1f);19            Assert.AreEqual(1.1f, f.GetValue());20        }21    }22}23using System;24using NUnit.Framework;25{26    {27        public void TestMethod()28        {29            var f = new FloatDataSource(1.1f);30            Assert.AreEqual(1.1f, f.GetValue());31        }32    }33}34using System;35using NUnit.Framework;36{37    {38        public void TestMethod()39        {40            var f = new FloatDataSource(1.1f);41            Assert.AreEqual(1.1f, f.GetValue());42        }43    }44}45using System;46using NUnit.Framework;47{48    {49        public void TestMethod()50        {51            var f = new FloatDataSource(1.1f);52            Assert.AreEqual(1.1f, f.GetValue());53        }54    }55}56using System;57using NUnit.Framework;58{59    {60        public void TestMethod()61        {62            var f = new FloatDataSource(1.1f);63            Assert.AreEqual(1.1f, f.GetValue());64        }65    }66}67using System;68using NUnit.Framework;69{

Full Screen

Full Screen

FloatDataSource

Using AI Code Generation

copy

Full Screen

1using NUnit.Framework;2using System;3{4    {5        [TestCaseSource(typeof(FloatDataSource), "GetFloat")]6        public void TestFloat(float value)7        {8            Console.WriteLine(value);9        }10    }11}12using NUnit.Framework;13using System.Collections;14{15    {16        {17            {18                yield return new TestCaseData(1.1f);19                yield return new TestCaseData(2.2f);20                yield return new TestCaseData(3.3f);21            }22        }23    }24}

Full Screen

Full Screen

FloatDataSource

Using AI Code Generation

copy

Full Screen

1using System;2using NUnit.Framework;3{4    {5        {6            new object[] { 3.14f, 3.14f, 0.0f },7            new object[] { 3.14f, 0.0f, 3.14f },8            new object[] { 0.0f, 3.14f, 3.14f },9            new object[] { 0.0f, 0.0f, 0.0f },10            new object[] { 3.14f, 3.14f, 0.0f },11            new object[] { 3.14f, 0.0f, 3.14f },12            new object[] { 0.0f, 3.14f, 3.14f },13            new object[] { 0.0f, 0.0f, 0.0f },14        };15    }16    {17        [Test, TestCaseSource(typeof(FloatDataSource), "TestCases")]18        public void AdditionTest(float first, float second, float expectedResult)19        {20            Assert.AreEqual(expectedResult, first + second);21        }22    }23}24using System;25using NUnit.Framework;26{27    {28        {29            new object[] { 3.14f, 3.14f, 0.0f },30            new object[] { 3.14f, 0.0f, 3.14f },31            new object[] { 0.0f, 3.14f, 3.14f },32            new object[] { 0.0f, 0.0f, 0.0f },33            new object[] { 3.14f, 3.14f, 0.0f },34            new object[] { 3.14f, 0.0f, 3.14f },35            new object[] { 0.0f, 3.14f, 3.14f },36            new object[] { 0.0f,

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