Best Nunit code snippet using ExampleTest.BaseTest
EqualConstraintTests.cs
Source:EqualConstraintTests.cs
1// ***********************************************************************2// Copyright (c) 2007-2013 Charlie Poole, Rob Prouse3//4// Permission is hereby granted, free of charge, to any person obtaining5// a copy of this software and associated documentation files (the6// "Software"), to deal in the Software without restriction, including7// without limitation the rights to use, copy, modify, merge, publish,8// distribute, sublicense, and/or sell copies of the Software, and to9// permit persons to whom the Software is furnished to do so, subject to10// the following conditions:11//12// The above copyright notice and this permission notice shall be13// 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 OF17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.22// ***********************************************************************23using System;24using System.Collections;25using System.Collections.Generic;26using System.Globalization;27using NUnit.Framework.Internal;28using NUnit.TestUtilities.Comparers;29namespace NUnit.Framework.Constraints30{31 [TestFixture]32 public class EqualConstraintTests : ConstraintTestBase33 {34 [SetUp]35 public void SetUp()36 {37 theConstraint = new EqualConstraint(4);38 expectedDescription = "4";39 stringRepresentation = "<equal 4>";40 }41 static object[] SuccessData = new object[] {4, 4.0f, 4.0d, 4.0000m};42 static object[] FailureData = new object[]43 {44 new TestCaseData(5, "5"),45 new TestCaseData(null, "null"),46 new TestCaseData("Hello", "\"Hello\""),47 new TestCaseData(double.NaN, double.NaN.ToString()),48 new TestCaseData(double.PositiveInfinity, double.PositiveInfinity.ToString())49 };50 #region DateTimeEquality51 public class DateTimeEquality52 {53 [Test]54 public void CanMatchDates()55 {56 DateTime expected = new DateTime(2007, 4, 1);57 DateTime actual = new DateTime(2007, 4, 1);58 Assert.That(actual, new EqualConstraint(expected));59 }60 [Test]61 public void CanMatchDatesWithinTimeSpan()62 {63 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);64 DateTime actual = new DateTime(2007, 4, 1, 13, 1, 0);65 TimeSpan tolerance = TimeSpan.FromMinutes(5.0);66 Assert.That(actual, new EqualConstraint(expected).Within(tolerance));67 }68 [Test]69 public void CanMatchDatesWithinDays()70 {71 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);72 DateTime actual = new DateTime(2007, 4, 4, 13, 0, 0);73 Assert.That(actual, new EqualConstraint(expected).Within(5).Days);74 }75 [Test]76 public void CanMatchDatesWithinHours()77 {78 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);79 DateTime actual = new DateTime(2007, 4, 1, 16, 0, 0);80 Assert.That(actual, new EqualConstraint(expected).Within(5).Hours);81 }82 [Test]83 public void CanMatchUsingIsEqualToWithinTimeSpan()84 {85 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);86 DateTime actual = new DateTime(2007, 4, 1, 13, 1, 0);87 Assert.That(actual, Is.EqualTo(expected).Within(TimeSpan.FromMinutes(2)));88 }89 [Test]90 public void CanMatchDatesWithinMinutes()91 {92 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);93 DateTime actual = new DateTime(2007, 4, 1, 13, 1, 0);94 Assert.That(actual, new EqualConstraint(expected).Within(5).Minutes);95 }96 [Test]97 public void CanMatchTimeSpanWithinMinutes()98 {99 TimeSpan expected = new TimeSpan(10, 0, 0);100 TimeSpan actual = new TimeSpan(10, 2, 30);101 Assert.That(actual, new EqualConstraint(expected).Within(5).Minutes);102 }103 [Test]104 public void CanMatchDatesWithinSeconds()105 {106 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);107 DateTime actual = new DateTime(2007, 4, 1, 13, 1, 0);108 Assert.That(actual, new EqualConstraint(expected).Within(300).Seconds);109 }110 [Test]111 public void CanMatchDatesWithinMilliseconds()112 {113 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);114 DateTime actual = new DateTime(2007, 4, 1, 13, 1, 0);115 Assert.That(actual, new EqualConstraint(expected).Within(300000).Milliseconds);116 }117 [Test]118 public void CanMatchDatesWithinTicks()119 {120 DateTime expected = new DateTime(2007, 4, 1, 13, 0, 0);121 DateTime actual = new DateTime(2007, 4, 1, 13, 1, 0);122 Assert.That(actual, new EqualConstraint(expected).Within(TimeSpan.TicksPerMinute*5).Ticks);123 }124 [Test]125 public void ErrorIfDaysPrecedesWithin()126 {127 Assert.Throws<InvalidOperationException>(() => Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now).Days.Within(5)));128 }129 [Test]130 public void ErrorIfHoursPrecedesWithin()131 {132 Assert.Throws<InvalidOperationException>(() => Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now).Hours.Within(5)));133 }134 [Test]135 public void ErrorIfMinutesPrecedesWithin()136 {137 Assert.Throws<InvalidOperationException>(() => Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now).Minutes.Within(5)));138 }139 [Test]140 public void ErrorIfSecondsPrecedesWithin()141 {142 Assert.Throws<InvalidOperationException>(() => Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now).Seconds.Within(5)));143 }144 [Test]145 public void ErrorIfMillisecondsPrecedesWithin()146 {147 Assert.Throws<InvalidOperationException>(() => Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now).Milliseconds.Within(5)));148 }149 [Test]150 public void ErrorIfTicksPrecedesWithin()151 {152 Assert.Throws<InvalidOperationException>(() => Assert.That(DateTime.Now, Is.EqualTo(DateTime.Now).Ticks.Within(5)));153 }154 }155 #endregion156 #region DateTimeOffsetEquality157 public class DateTimeOffsetShouldBeSame158 {159 [Datapoints]160 public static readonly DateTimeOffset[] sameDateTimeOffsets =161 {162 new DateTimeOffset(new DateTime(2014, 1, 30, 12, 34, 56), new TimeSpan(6, 15, 0)),163 new DateTimeOffset(new DateTime(2014, 1, 30, 9, 19, 56), new TimeSpan(3, 0, 0)),164 new DateTimeOffset(new DateTime(2014, 1, 30, 9, 19, 56), new TimeSpan(3, 1, 0)),165 new DateTimeOffset(new DateTime(2014, 1, 30, 9, 19, 55), new TimeSpan(3, 0, 0)),166 new DateTimeOffset(new DateTime(2014, 1, 30, 9, 19, 55), new TimeSpan(3, 50, 0))167 };168 [Theory]169 public void PositiveEqualityTest(DateTimeOffset value1, DateTimeOffset value2)170 {171 Assume.That(value1 == value2);172 Assert.That(value1, Is.EqualTo(value2));173 }174 [Theory]175 public void NegativeEqualityTest(DateTimeOffset value1, DateTimeOffset value2)176 {177 Assume.That(value1 != value2);178 Assert.That(value1, Is.Not.EqualTo(value2));179 }180 [Theory]181 public void PositiveEqualityTestWithTolerance(DateTimeOffset value1, DateTimeOffset value2)182 {183 Assume.That((value1 - value2).Duration() <= new TimeSpan(0, 1, 0));184 Assert.That(value1, Is.EqualTo(value2).Within(1).Minutes);185 }186 [Theory]187 public void NegativeEqualityTestWithTolerance(DateTimeOffset value1, DateTimeOffset value2)188 {189 Assume.That((value1 - value2).Duration() > new TimeSpan(0, 1, 0));190 Assert.That(value1, Is.Not.EqualTo(value2).Within(1).Minutes);191 }192 [Theory]193 public void NegativeEqualityTestWithToleranceAndWithSameOffset(DateTimeOffset value1, DateTimeOffset value2)194 {195 Assume.That((value1 - value2).Duration() > new TimeSpan(0, 1, 0));196 Assert.That(value1, Is.Not.EqualTo(value2).Within(1).Minutes.WithSameOffset);197 }198 [Theory]199 public void PositiveEqualityTestWithToleranceAndWithSameOffset(DateTimeOffset value1, DateTimeOffset value2)200 {201 Assume.That((value1 - value2).Duration() <= new TimeSpan(0, 1, 0));202 Assume.That(value1.Offset == value2.Offset);203 Assert.That(value1, Is.EqualTo(value2).Within(1).Minutes.WithSameOffset);204 }205 [Theory]206 public void NegativeEqualityTestWithinToleranceAndWithSameOffset(DateTimeOffset value1, DateTimeOffset value2)207 {208 Assume.That((value1 - value2).Duration() <= new TimeSpan(0, 1, 0));209 Assume.That(value1.Offset != value2.Offset);210 Assert.That(value1, Is.Not.EqualTo(value2).Within(1).Minutes.WithSameOffset);211 }212 }213 public class DateTimeOffSetEquality214 {215 [Test]216 public void CanMatchDates()217 {218 var expected = new DateTimeOffset(new DateTime(2007, 4, 1));219 var actual = new DateTimeOffset(new DateTime(2007, 4, 1));220 Assert.That(actual, new EqualConstraint(expected));221 }222 [Test]223 public void CanMatchDatesWithinTimeSpan()224 {225 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));226 var actual = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 1, 0));227 var tolerance = TimeSpan.FromMinutes(5.0);228 Assert.That(actual, new EqualConstraint(expected).Within(tolerance));229 }230 [Test]231 public void CanMatchDatesWithinDays()232 {233 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));234 var actual = new DateTimeOffset(new DateTime(2007, 4, 4, 13, 0, 0));235 Assert.That(actual, new EqualConstraint(expected).Within(5).Days);236 }237 [Test]238 public void CanMatchUsingIsEqualToWithinTimeSpan()239 {240 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));241 var actual = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 1, 0));242 Assert.That(actual, Is.EqualTo(expected).Within(TimeSpan.FromMinutes(2)));243 }244 [Test]245 public void CanMatchDatesWithinMinutes()246 {247 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));248 var actual = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 1, 0));249 Assert.That(actual, new EqualConstraint(expected).Within(5).Minutes);250 }251 [Test]252 public void CanMatchDatesWithinSeconds()253 {254 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));255 var actual = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 1, 0));256 Assert.That(actual, new EqualConstraint(expected).Within(300).Seconds);257 }258 [Test]259 public void CanMatchDatesWithinMilliseconds()260 {261 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));262 var actual = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 1, 0));263 Assert.That(actual, new EqualConstraint(expected).Within(300000).Milliseconds);264 }265 [Test]266 public void CanMatchDatesWithinTicks()267 {268 var expected = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 0, 0));269 var actual = new DateTimeOffset(new DateTime(2007, 4, 1, 13, 1, 0));270 Assert.That(actual, new EqualConstraint(expected).Within(TimeSpan.TicksPerMinute*5).Ticks);271 }272 [Test]273 public void DTimeOffsetCanMatchDatesWithinHours()274 {275 var a = DateTimeOffset.Parse("2012-01-01T12:00Z");276 var b = DateTimeOffset.Parse("2012-01-01T12:01Z");277 Assert.That(a, Is.EqualTo(b).Within(TimeSpan.FromMinutes(2)));278 }279 }280 #endregion281 #region DictionaryEquality282 public class DictionaryEquality283 {284 [Test]285 public void CanMatchDictionaries_SameOrder()286 {287 Assert.AreEqual(new Dictionary<int, int> {{0, 0}, {1, 1}, {2, 2}},288 new Dictionary<int, int> {{0, 0}, {1, 1}, {2, 2}});289 }290 [Test]291 public void CanMatchDictionaries_Failure()292 {293 Assert.Throws<AssertionException>(294 () => Assert.AreEqual(new Dictionary<int, int> {{0, 0}, {1, 1}, {2, 2}},295 new Dictionary<int, int> {{0, 0}, {1, 5}, {2, 2}}));296 }297 [Test]298 public void CanMatchDictionaries_DifferentOrder()299 {300 Assert.AreEqual(new Dictionary<int, int> {{0, 0}, {1, 1}, {2, 2}},301 new Dictionary<int, int> {{0, 0}, {2, 2}, {1, 1}});302 }303#if !NETSTANDARD1_3 && !NETSTANDARD1_6304 [Test]305 public void CanMatchHashtables_SameOrder()306 {307 Assert.AreEqual(new Hashtable {{0, 0}, {1, 1}, {2, 2}},308 new Hashtable {{0, 0}, {1, 1}, {2, 2}});309 }310 [Test]311 public void CanMatchHashtables_Failure()312 {313 Assert.Throws<AssertionException>(314 () => Assert.AreEqual(new Hashtable {{0, 0}, {1, 1}, {2, 2}},315 new Hashtable {{0, 0}, {1, 5}, {2, 2}}));316 }317 [Test]318 public void CanMatchHashtables_DifferentOrder()319 {320 Assert.AreEqual(new Hashtable {{0, 0}, {1, 1}, {2, 2}},321 new Hashtable {{0, 0}, {2, 2}, {1, 1}});322 }323 [Test]324 public void CanMatchHashtableWithDictionary()325 {326 Assert.AreEqual(new Hashtable {{0, 0}, {1, 1}, {2, 2}},327 new Dictionary<int, int> {{0, 0}, {2, 2}, {1, 1}});328 }329#endif330 }331 #endregion332 #region FloatingPointEquality333 public class FloatingPointEquality334 {335 [TestCase(double.NaN)]336 [TestCase(double.PositiveInfinity)]337 [TestCase(double.NegativeInfinity)]338 [TestCase(float.NaN)]339 [TestCase(float.PositiveInfinity)]340 [TestCase(float.NegativeInfinity)]341 public void CanMatchSpecialFloatingPointValues(object value)342 {343 Assert.That(value, new EqualConstraint(value));344 }345 [TestCase(20000000000000004.0)]346 [TestCase(19999999999999996.0)]347 public void CanMatchDoublesWithUlpTolerance(object value)348 {349 Assert.That(value, new EqualConstraint(20000000000000000.0).Within(1).Ulps);350 }351 [TestCase(20000000000000008.0)]352 [TestCase(19999999999999992.0)]353 public void FailsOnDoublesOutsideOfUlpTolerance(object value)354 {355 var ex = Assert.Throws<AssertionException>(() => Assert.That(value, new EqualConstraint(20000000000000000.0).Within(1).Ulps));356 Assert.That(ex.Message, Does.Contain("+/- 1 Ulps"));357 }358 [TestCase(19999998.0f)]359 [TestCase(20000002.0f)]360 public void CanMatchSinglesWithUlpTolerance(object value)361 {362 Assert.That(value, new EqualConstraint(20000000.0f).Within(1).Ulps);363 }364 [TestCase(19999996.0f)]365 [TestCase(20000004.0f)]366 public void FailsOnSinglesOutsideOfUlpTolerance(object value)367 {368 var ex = Assert.Throws<AssertionException>(() => Assert.That(value, new EqualConstraint(20000000.0f).Within(1).Ulps));369 Assert.That(ex.Message, Does.Contain("+/- 1 Ulps"));370 }371 [TestCase(9500.0)]372 [TestCase(10000.0)]373 [TestCase(10500.0)]374 public void CanMatchDoublesWithRelativeTolerance(object value)375 {376 Assert.That(value, new EqualConstraint(10000.0).Within(10.0).Percent);377 }378 [TestCase(8500.0)]379 [TestCase(11500.0)]380 public void FailsOnDoublesOutsideOfRelativeTolerance(object value)381 {382 var ex = Assert.Throws<AssertionException>(() => Assert.That(value, new EqualConstraint(10000.0).Within(10.0).Percent));383 Assert.That(ex.Message, Does.Contain("+/- 10.0d Percent"));384 }385 [TestCase(9500.0f)]386 [TestCase(10000.0f)]387 [TestCase(10500.0f)]388 public void CanMatchSinglesWithRelativeTolerance(object value)389 {390 Assert.That(value, new EqualConstraint(10000.0f).Within(10.0f).Percent);391 }392 [TestCase(8500.0f)]393 [TestCase(11500.0f)]394 public void FailsOnSinglesOutsideOfRelativeTolerance(object value)395 {396 var ex = Assert.Throws<AssertionException>(() => Assert.That(value, new EqualConstraint(10000.0f).Within(10.0f).Percent));397 Assert.That(ex.Message, Does.Contain("+/- 10.0f Percent"));398 }399 /// <summary>Applies both the Percent and Ulps modifiers to cause an exception</summary>400 [Test]401 public void ErrorWithPercentAndUlpsToleranceModes()402 {403 Assert.Throws<InvalidOperationException>(() =>404 {405 var shouldFail = new EqualConstraint(100.0f).Within(10.0f).Percent.Ulps;406 });407 }408 /// <summary>Applies both the Ulps and Percent modifiers to cause an exception</summary>409 [Test]410 public void ErrorWithUlpsAndPercentToleranceModes()411 {412 Assert.Throws<InvalidOperationException>(() =>413 {414 EqualConstraint shouldFail = new EqualConstraint(100.0f).Within(10.0f).Ulps.Percent;415 });416 }417 [Test]418 public void ErrorIfPercentPrecedesWithin()419 {420 Assert.Throws<InvalidOperationException>(() => Assert.That(1010, Is.EqualTo(1000).Percent.Within(5)));421 }422 [Test]423 public void ErrorIfUlpsPrecedesWithin()424 {425 Assert.Throws<InvalidOperationException>(() => Assert.That(1010.0, Is.EqualTo(1000.0).Ulps.Within(5)));426 }427 [TestCase(1000, 1010)]428 [TestCase(1000U, 1010U)]429 [TestCase(1000L, 1010L)]430 [TestCase(1000UL, 1010UL)]431 public void ErrorIfUlpsIsUsedOnIntegralType(object x, object y)432 {433 Assert.Throws<InvalidOperationException>(() => Assert.That(y, Is.EqualTo(x).Within(2).Ulps));434 }435 [Test]436 public void ErrorIfUlpsIsUsedOnDecimal()437 {438 Assert.Throws<InvalidOperationException>(() => Assert.That(100m, Is.EqualTo(100m).Within(2).Ulps));439 }440 }441 #endregion442 #region UsingModifier443 public class UsingModifier444 {445 [Test]446 public void UsesProvidedIComparer()447 {448 var comparer = new ObjectComparer();449 Assert.That(2 + 2, Is.EqualTo(4).Using(comparer));450 Assert.That(comparer.WasCalled, "Comparer was not called");451 }452 [Test]453 public void CanCompareUncomparableTypes()454 {455 Assert.That(2 + 2, Is.Not.EqualTo("4"));456 var comparer = new ConvertibleComparer();457 Assert.That(2 + 2, Is.EqualTo("4").Using(comparer));458 }459 [Test]460 public void UsesProvidedEqualityComparer()461 {462 var comparer = new ObjectEqualityComparer();463 Assert.That(2 + 2, Is.EqualTo(4).Using(comparer));464 Assert.That(comparer.Called, "Comparer was not called");465 }466 [Test]467 public void UsesProvidedGenericEqualityComparer()468 {469 var comparer = new GenericEqualityComparer<int>();470 Assert.That(2 + 2, Is.EqualTo(4).Using(comparer));471 Assert.That(comparer.WasCalled, "Comparer was not called");472 }473 [Test]474 public void UsesProvidedGenericComparer()475 {476 var comparer = new GenericComparer<int>();477 Assert.That(2 + 2, Is.EqualTo(4).Using(comparer));478 Assert.That(comparer.WasCalled, "Comparer was not called");479 }480 [Test]481 public void UsesProvidedGenericComparison()482 {483 var comparer = new GenericComparison<int>();484 Assert.That(2 + 2, Is.EqualTo(4).Using(comparer.Delegate));485 Assert.That(comparer.WasCalled, "Comparer was not called");486 }487 [Test]488 public void UsesProvidedGenericEqualityComparison()489 {490 var comparer = new GenericEqualityComparison<int>();491 Assert.That(2 + 2, Is.EqualTo(4).Using<int>(comparer.Delegate));492 Assert.That(comparer.WasCalled, "Comparer was not called");493 }494 [Test]495 public void UsesBooleanReturningDelegate()496 {497 Assert.That(2 + 2, Is.EqualTo(4).Using<int>((x, y) => x.Equals(y)));498 }499 [Test]500 public void UsesProvidedLambda_IntArgs()501 {502 Assert.That(2 + 2, Is.EqualTo(4).Using<int>((x, y) => x.CompareTo(y)));503 }504 [Test]505 public void UsesProvidedLambda_StringArgs()506 {507 Assert.That("hello", Is.EqualTo("HELLO").Using<string>((x, y) => StringUtil.Compare(x, y, true)));508 }509 [Test]510 public void UsesProvidedListComparer()511 {512 var list1 = new List<int>() {2, 3};513 var list2 = new List<int>() {3, 4};514 var list11 = new List<List<int>>() {list1};515 var list22 = new List<List<int>>() {list2};516 var comparer = new IntListEqualComparer();517 Assert.That(list11, new CollectionEquivalentConstraint(list22).Using(comparer));518 }519 public class IntListEqualComparer : IEqualityComparer<List<int>>520 {521 public bool Equals(List<int> x, List<int> y)522 {523 return x.Count == y.Count;524 }525 public int GetHashCode(List<int> obj)526 {527 return obj.Count.GetHashCode();528 }529 }530 [Test]531 public void UsesProvidedArrayComparer()532 {533 var array1 = new int[] {2, 3};534 var array2 = new int[] {3, 4};535 var list11 = new List<int[]>() {array1};536 var list22 = new List<int[]>() {array2};537 var comparer = new IntArrayEqualComparer();538 Assert.That(list11, new CollectionEquivalentConstraint(list22).Using(comparer));539 }540 public class IntArrayEqualComparer : IEqualityComparer<int[]>541 {542 public bool Equals(int[] x, int[] y)543 {544 return x.Length == y.Length;545 }546 public int GetHashCode(int[] obj)547 {548 return obj.Length.GetHashCode();549 }550 }551 [Test]552 public void HasMemberHonorsUsingWhenCollectionsAreOfDifferentTypes()553 {554 ICollection strings = new List<string> { "1", "2", "3" };555 Assert.That(strings, Has.Member(2).Using<string, int>((s, i) => i.ToString() == s));556 }557 }558 #endregion559 #region TypeEqualityMessages560 private readonly string NL = Environment.NewLine;561 private static IEnumerable DifferentTypeSameValueTestData562 {563 get564 {565 var ptr = new System.IntPtr(0);566 var ExampleTestA = new ExampleTest.classA(0);567 var ExampleTestB = new ExampleTest.classB(0);568 var clipTestA = new ExampleTest.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Clip.ReallyLongClassNameShouldBeHere();569 var clipTestB = new ExampleTest.Clip.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Clip.ReallyLongClassNameShouldBeHere();570 yield return new object[] { 0, ptr };571 yield return new object[] { ExampleTestA, ExampleTestB };572 yield return new object[] { clipTestA, clipTestB };573 }574 }575 [Test]576 public void SameValueDifferentTypeExactMessageMatch()577 {578 var ex = Assert.Throws<AssertionException>(() => Assert.AreEqual(0, new System.IntPtr(0)));579 Assert.AreEqual(ex.Message, " Expected: 0 (Int32)"+ NL + " But was: 0 (IntPtr)"+ NL);580 }581 class Dummy582 {583 internal readonly int value;584 public Dummy(int value)585 {586 this.value = value;587 }588 public override string ToString()589 {590 return "Dummy " + value;591 }592 }593 class Dummy1594 {595 internal readonly int value;596 public Dummy1(int value)597 {598 this.value = value;599 }600 public override string ToString()601 {602 return "Dummy " + value;603 }604 }605 class DummyGenericClass<T>606 {607 private object _obj;608 public DummyGenericClass(object obj)609 {610 _obj = obj;611 }612 public override string ToString()613 {614 return _obj.ToString();615 }616 }617 [Test]618 public void TestSameValueDifferentTypeUsingGenericTypes()619 {620 var d1 = new Dummy(12);621 var d2 = new Dummy1(12);622 var dc1 = new DummyGenericClass<Dummy>(d1);623 var dc2 = new DummyGenericClass<Dummy1>(d2);624 var ex = Assert.Throws<AssertionException>(() => Assert.AreEqual(dc1, dc2));625 var expectedMsg =626 " Expected: <Dummy 12> (EqualConstraintTests+DummyGenericClass`1[EqualConstraintTests+Dummy])" + Environment.NewLine +627 " But was: <Dummy 12> (EqualConstraintTests+DummyGenericClass`1[EqualConstraintTests+Dummy1])" + Environment.NewLine;628 Assert.AreEqual(expectedMsg, ex.Message);629 }630 [Test]631 public void SameValueAndTypeButDifferentReferenceShowNotShowTypeDifference()632 {633 var ex = Assert.Throws<AssertionException>(() => Assert.AreEqual(Is.Zero, Is.Zero));634 Assert.AreEqual(ex.Message, " Expected: <<equal 0>>"+ NL + " But was: <<equal 0>>"+ NL);635 }636 [Test, TestCaseSource("DifferentTypeSameValueTestData")]637 public void SameValueDifferentTypeRegexMatch(object expected, object actual)638 {639 var ex = Assert.Throws<AssertionException>(() => Assert.AreEqual(expected, actual));640 Assert.That(ex.Message, Does.Match(@"\s*Expected\s*:\s*.*\s*\(.+\)\r?\n\s*But\s*was\s*:\s*.*\s*\(.+\)"));641 }642 }643 namespace ExampleTest.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Clip {644 class ReallyLongClassNameShouldBeHere {645 public override bool Equals(object obj)646 {647 if (obj == null || GetType() != obj.GetType())648 {649 return false;650 }651 return obj.ToString() == this.ToString();652 }653 public override int GetHashCode()654 {655 return "a".GetHashCode();656 }657 public override string ToString()658 {659 return "a";660 }661 }662 }663 namespace ExampleTest.Clip.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Outer.Middle.Inner.Clip664 {665 class ReallyLongClassNameShouldBeHere {666 public override bool Equals(object obj)667 {668 if (obj == null || GetType() != obj.GetType())669 {670 return false;671 }672 return obj.ToString()==this.ToString();673 }674 public override int GetHashCode()675 {676 return "a".GetHashCode();677 }678 public override string ToString()679 {680 return "a";681 }682 }683 }684 namespace ExampleTest {685 class baseTest {686 readonly int _value;687 public baseTest()688 {689 _value = 0;690 }691 public baseTest(int value) {692 _value = value;693 }694 public override bool Equals(object obj)695 {696 if (obj == null || GetType() != obj.GetType())697 {698 return false;699 }700 return _value.Equals(((baseTest)obj)._value);701 }702 public override string ToString()703 {704 return _value.ToString();705 }706 public override int GetHashCode()707 {708 return _value.GetHashCode();709 }710 }711 class classA : baseTest {712 public classA(int x) : base(x) { }713 }714 class classB : baseTest715 {716 public classB(int x) : base(x) { }717 }718 }719 #endregion720 /// <summary>721 /// ConvertibleComparer is used in testing to ensure that objects722 /// of different types can be compared when appropriate.723 /// </summary>724 /// <remark>Introduced when testing issue 1897.725 /// https://github.com/nunit/nunit/issues/1897726 /// </remark>727 public class ConvertibleComparer : IComparer<IConvertible>728 {729 public int Compare(IConvertible x, IConvertible y)730 {731 var str1 = Convert.ToString(x, CultureInfo.InvariantCulture);732 var str2 = Convert.ToString(y, CultureInfo.InvariantCulture);733 return string.Compare(str1, str2, StringComparison.Ordinal);734 }735 }736}...
EmployeeRepoTests.cs
Source:EmployeeRepoTests.cs
...7using System.Threading.Tasks;8using Xunit;9namespace Api.RepositoryTests10{11 public class EmployeeRepoTests : BaseTest12 {13 private EmployeeRepository _repo;14 public EmployeeRepoTests()15 {16 _repo = new EmployeeRepository(Context);17 }18 #region Get All Employees19 [Fact]20 public async Task GetAll_Success_Test()21 {22 var result = await _repo.GetAll().ConfigureAwait(false);23 Assert.NotNull(result);24 Assert.NotEmpty(result);25 }...
EmployeeServiceTests.cs
Source:EmployeeServiceTests.cs
...11using System.Threading.Tasks;12using Xunit;13namespace Api.ServiceTests14{15 public class EmployeeServiceTests : BaseTest16 {17 private readonly IEmployeeService _service;18 private readonly IMapper _mapper;19 public EmployeeServiceTests()20 {21 var repo = new EmployeeRepository(Context);22 var mapperConfig = new MapperConfiguration(c =>23 {24 c.AddProfile<DataAccessMapping>();25 });26 _mapper = mapperConfig.CreateMapper();27 _service = new EmployeeService(repo, _mapper);28 }29 #region Add...
ExampleNotepadTest.cs
Source:ExampleNotepadTest.cs
...3using Microsoft.VisualStudio.TestTools.UnitTesting;4namespace Notepad.AutomatedTests.Tests5{6 [CodedUITest]7 public class ExampleNotepadTest : BaseTest8 {9 //These may be turned into data drive tests for conciseness and flexibility10 [TestMethod]11 [TestCategory("ExampleTest")]12 public void NoCharactersEntered()13 {14 NotePadWindowHelpers.DeleteDocument( SaveLocation, "testNoCharacters.txt");15 NotepadWindow.NotepadEdit.Text = Empty;16 NotePadWindowHelpers.SaveDocument(SaveLocation, "testNoCharacters.txt");17 //Assert.AreEqual( NotePadWindowHelpers.CheckTextFileForExpectedText(SaveLocation, "testNoCharacters.txt", Empty), "Unexpected text was found" );18 Assert.IsTrue( NotePadWindowHelpers.CheckTextFileForExpectedText( SaveLocation, "testNoCharacters.txt", Empty ) );19 NotePadWindowHelpers.DeleteDocument(SaveLocation, "testNoCharacters.txt");20 }21 [TestMethod]...
HomePageTest.cs
Source:HomePageTest.cs
...5using PagesProject.WebPages;6namespace WebTestProject.ExampleTest7{8 [TestFixture]9 public class HomePageTest : BaseTest10 {11 TestContext testContext; 12 HomePage HomePage;13 IWebDriver driver;14 [SetUp] 15 public void Initialize()16 {17 testContext = TestContext.CurrentContext;18 reportHelper.StartTest(testContext); 19 }20 [Test] 21 [TestCaseSource(typeof(BaseTest), "BrowserToRunWith")] 22 public void Test1(string browserName)23 {24 driver = GetWebDriver(browserName);25 HomePage = new HomePage(driver);26 HomePage.GoTo();27 HomePage.BuscarGoogle("Selenium");28 Assert.IsTrue(true);29 }30 [Test] 31 [TestCaseSource(typeof(BaseTest), "BrowserToRunWith")]32 public void TestWebAndMobile(string browserName)33 {34 driver = GetWebDriver(browserName);35 HomePage = new HomePage(driver);36 37 HomePage.GoTo();38 HomePage.BuscarGoogle("Segundo test");39 40 var _driver = GetMobileDriver(DriverType.Android);41 AndroidCalculator androidCalculator = new AndroidCalculator(_driver);42 androidCalculator.RealizarSuma();43 _driver.Quit();44 Assert.IsTrue(true);45 }...
ExampleTest.cs
Source:ExampleTest.cs
...10namespace NUnitExample.Tests11{12 [AllureSuite("Search engine test")]13 [AllureSeverity(SeverityLevel.minor)]14 public class ExampleTest : BaseTest15 {16 private static IEnumerable TestCases17 {18 get19 {20 var csvFile = TestContext.Parameters.Get("testData", "testData\\prod.csv");21 using var reader = new StreamReader(csvFile);22 using var csv = new CsvReader(reader);23 foreach (var record in csv.GetRecords<TestData>())24 yield return new TestCaseData(record.Query, record.Index, record.Text);25 }26 }27 private SearchEngine _engine;28 [SetUp]...
LoginTest.cs
Source:LoginTest.cs
...7using System;8namespace WebTestProject.ExampleTest9{10 [TestFixture]11 public class LoginTest : BaseTest12 {13 TestContext testContext;14 LoginPage loign;15 IWebDriver driver;16 [SetUp]17 public void Initialize()18 {19 testContext = TestContext.CurrentContext;20 driver = GetWebDriver(DriverType.Chrome);21 loign = new LoginPage(driver);22 }23 [Test]24 [TestCase(TestName = "Login No Valido - Vates", Category = "Login, test")]25 public void LoginNoValidoVates()...
LoginPageTest.cs
Source:LoginPageTest.cs
...3using PagesProject.WebPages;4namespace WebTestProject.ExampleTest5{6 [TestFixture]7 public class LoginPageTest : BaseTest8 {9 TestContext testContext;10 LoginPage LoginPage;11 IWebDriver driver;12 [SetUp] 13 public void Initialize()14 { 15 testContext = TestContext.CurrentContext;16 reportHelper.StartTest(testContext); 17 }18 [Test]19 [TestCaseSource(typeof(BaseTest), "BrowserToRunWith")]20 public void TestReportLoginPage(string browserName)21 {22 driver = GetWebDriver(browserName);23 LoginPage = new LoginPage(driver);24 LoginPage.GoTo();25 LoginPage.BuscarGoogle("Hola Mundo"); 26 Assert.IsTrue(true);27 }28 [TearDown]29 public void CleanUp()30 {31 reportHelper.AddTestToReport(testContext);32 driver.Quit();33 LoginPage.KillDriverProcesses();...
BaseTest
Using AI Code Generation
1using ExampleTest;2{3 {4 public void Method1()5 {6 }7 }8}9using ExampleTest;10{11 {12 public void Method2()13 {14 }15 }16}17The type or namespace name 'BaseTest' could not be found (are you missing a using directive or an assembly reference?)18I have a class named BaseTest which is used in multiple projects. The problem is that the BaseTest class is not recognized in the other projects. I have tried using the using ExampleTest; statement but it does not work. I am using Visual Studio 2010. I am getting the following error while trying to use the BaseTest class in the other projects. The type or namespace name 'BaseTest' could not be found (are you missing a using directive or an assembly reference?)
BaseTest
Using AI Code Generation
1using ExampleTest;2using System;3{4 {5 public virtual void Test()6 {7 Console.WriteLine("BaseTest.Test() called");8 }9 }10}11using ExampleTest;12using System;13{14 {15 public override void Test()16 {17 Console.WriteLine("DerivedTest.Test() called");18 }19 }20}21using ExampleTest;22using System;23{24 {25 public static void Main(string[] args)26 {27 BaseTest baseTest = new BaseTest();28 baseTest.Test();29 DerivedTest derivedTest = new DerivedTest();30 derivedTest.Test();31 }32 }33}34BaseTest.Test() called35DerivedTest.Test() called36{37}38using ExampleTest;39using System;40{41 {42 public abstract void Test();43 }44}45using ExampleTest;46using System;47{48 {49 public override void Test()50 {51 Console.WriteLine("DerivedTest.Test() called");52 }53 }54}55using ExampleTest;56using System;57{58 {59 public static void Main(string[] args)60 {61 DerivedTest derivedTest = new DerivedTest();62 derivedTest.Test();63 }64 }65}66DerivedTest.Test() called
BaseTest
Using AI Code Generation
1using ExampleTest;2using System;3{4 {5 static void Main(string[] args)6 {7 Console.WriteLine("Hello World!");8 }9 }10}11What is a Parallel LINQ (PLINQ
BaseTest
Using AI Code Generation
1using ExampleTest;2{3 static void Main(string[] args)4 {5 BaseTest obj = new BaseTest();6 obj.Test();7 }8}
BaseTest
Using AI Code Generation
1using ExampleTest;2using System;3{4 {5 public static void Main()6 {7 Console.WriteLine("Hello World!");8 }9 }10}
BaseTest
Using AI Code Generation
1using ExampleTest.BaseTest;2{3 {4 public void TestMethod()5 {6 }7 }8}
BaseTest
Using AI Code Generation
1using ExampleTest;2{3 {4 public void TestMethod()5 {6 Console.WriteLine("Hello World!");7 }8 }9}10using ExampleTest.BaseTest;11using System;12using System.IO;13{14 {15 static void Main(string[] args)16 {17 string path = @"C:\Users\Public\TestFolder\WriteText.txt";18 string text = "A test string.";19 using (StreamWriter sw = File.CreateText(path))20 {21 sw.WriteLine(text);22 }23 using (StreamReader sr = File.OpenText(path))24 {25 string s = "";26 while ((s = sr.ReadLine()) != null)27 {28 Console.WriteLine(s);29 }30 }31 }32 }33}34using System;35using System.IO;36{37 {38 static void Main(string[] args)39 {40 string path = @"C:\Users\Public\TestFolder\WriteText.txt";41 string text = "A test string.";42 using (StreamWriter sw = File.CreateText(path))43 {44 sw.WriteLine(text);45 }46 }47 }48}
BaseTest
Using AI Code Generation
1using ExampleTest;2{3 public static void Main()4 {5 BaseTest bt = new BaseTest();6 bt.TestMethod();7 }8}9BaseTest.TestMethod() called10{11 {12 public void TestMethod()13 {14 System.Console.WriteLine("BaseTest.TestMethod() called");15 }16 }17}18{19 {20 public void TestMethod()21 {22 System.Console.WriteLine("DerivedTest.TestMethod() called");23 }24 }25}26using ExampleTest;27{28 public static void Main()29 {30 BaseTest bt = new BaseTest();31 bt.TestMethod();32 DerivedTest dt = new DerivedTest();33 dt.TestMethod();34 }35}36BaseTest.TestMethod() called37DerivedTest.TestMethod() called38{39 {40 public void TestMethod()41 {42 System.Console.WriteLine("BaseTest.TestMethod() called");43 }44 }45}46{47 {48 public void TestMethod()49 {50 System.Console.WriteLine("DerivedTest.TestMethod() called");51 }52 }53}54using ExampleTest;55{56 public static void Main()57 {58 BaseTest bt = new BaseTest();59 bt.TestMethod();
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!!