Best Nunit code snippet using NUnit.Framework.Internal.Execution.FakeWorkItem
WorkItemTests.cs
Source:WorkItemTests.cs
...63 }64 [TestCaseSource(nameof(GetTargetApartmentTestData))]65 public void GetsTargetApartmentFromParentTests(Test test, ApartmentState expected)66 {67 var work = new FakeWorkItem(test, TestFilter.Empty);68 Assert.That(work.TargetApartment, Is.EqualTo(expected));69 }70 [TestCaseSource(nameof(GetTargetApartmentTestData))]71 public void GetsTargetApartmentFromParentTestsInWrappedTests(Test test, ApartmentState expected)72 {73 var work = new FakeWorkItem(test, TestFilter.Empty);74 var wrapped = new FakeWorkItem(work);75 Assert.That(wrapped.TargetApartment, Is.EqualTo(expected));76 }77 public static IEnumerable<TestCaseData> GetTargetApartmentTestData()78 {79 yield return new TestCaseData(CreateFakeTests(ApartmentState.Unknown, ApartmentState.Unknown, ApartmentState.Unknown), ApartmentState.Unknown);80 yield return new TestCaseData(CreateFakeTests(ApartmentState.Unknown, ApartmentState.Unknown, ApartmentState.STA), ApartmentState.STA);81 yield return new TestCaseData(CreateFakeTests(ApartmentState.Unknown, ApartmentState.STA, ApartmentState.Unknown), ApartmentState.STA);82 yield return new TestCaseData(CreateFakeTests(ApartmentState.STA, ApartmentState.Unknown, ApartmentState.Unknown), ApartmentState.STA);83 yield return new TestCaseData(CreateFakeTests(ApartmentState.Unknown, ApartmentState.Unknown, ApartmentState.MTA), ApartmentState.MTA);84 yield return new TestCaseData(CreateFakeTests(ApartmentState.Unknown, ApartmentState.MTA, ApartmentState.Unknown), ApartmentState.MTA);85 yield return new TestCaseData(CreateFakeTests(ApartmentState.MTA, ApartmentState.Unknown, ApartmentState.Unknown), ApartmentState.MTA);86 yield return new TestCaseData(CreateFakeTests(ApartmentState.STA, ApartmentState.MTA, ApartmentState.Unknown), ApartmentState.MTA);87 yield return new TestCaseData(CreateFakeTests(ApartmentState.MTA, ApartmentState.STA, ApartmentState.Unknown), ApartmentState.STA);88 }89 static ITest CreateFakeTests(ApartmentState assemblyApartment, ApartmentState fixtureApartment, ApartmentState methodApartment) =>90 new FakeTest("Method", methodApartment)91 {92 Parent = new FakeTest("Fixture", fixtureApartment)93 {94 Parent = new FakeTest("Assembly", assemblyApartment)95 }96 };97 class FakeTest : Test98 {99 public FakeTest(string name, ApartmentState apartmentState) : base(name)100 {101 if (apartmentState != ApartmentState.Unknown)102 Properties.Add(PropertyNames.ApartmentState, apartmentState);103 }104 public override object[] Arguments105 {106 get { throw new System.NotImplementedException(); }107 }108 public override string XmlElementName => "MockTest";109 public override bool HasChildren => Tests.Count > 0;110 public override IList<ITest> Tests => new List<ITest>();111 public override TNode AddToXml(TNode parentNode, bool recursive)112 {113 throw new System.NotImplementedException();114 }115 public override TestResult MakeTestResult() => new FakeTestResult(this);116 }117 class FakeTestResult : TestResult118 {119 public FakeTestResult(ITest test) : base(test)120 {121 }122 public override int TotalCount => 1;123 public override int FailCount => 0;124 public override int WarningCount => 0;125 public override int PassCount => 1;126 public override int SkipCount => 0;127 public override int InconclusiveCount => 0;128 public override bool HasChildren => false;129 public override IEnumerable<ITestResult> Children => null;130 }131 class FakeWorkItem : WorkItem132 {133 public FakeWorkItem(WorkItem wrappedItem) : base(wrappedItem)134 {135 }136 public FakeWorkItem(Test test, ITestFilter filter) : base(test, filter)137 {138 }139 protected override void PerformWork()140 {141 throw new System.NotImplementedException();142 }143 }144 }145}...
Fakes.cs
Source:Fakes.cs
...46 return new FakeTestMethod(obj, name);47 }48 #endregion49 #region GetWorkItem50 public static FakeWorkItem GetWorkItem(Test test, TestExecutionContext context)51 {52 return new FakeWorkItem(test, context);53 }54 public static FakeWorkItem GetWorkItem(Test test)55 {56 return GetWorkItem(test, new TestExecutionContext());57 }58 public static FakeWorkItem GetWorkItem(Type type, string name, TestExecutionContext context)59 {60 return GetWorkItem(GetTestMethod(type, name), context);61 }62 public static FakeWorkItem GetWorkItem(Type type, string name)63 {64 return GetWorkItem(GetTestMethod(type, name));65 }66 public static FakeWorkItem GetWorkItem(object obj, string name, TestExecutionContext context)67 {68 return GetWorkItem(obj.GetType(), name, context);69 }70 public static FakeWorkItem GetWorkItem(object obj, string name)71 {72 return GetWorkItem(obj.GetType(), name);73 }74 #endregion75 }76 #region FakeTestMethod Class77 /// <summary>78 /// FakeTestMethod is used in tests to simulate an actual TestMethod79 /// </summary>80 public class FakeTestMethod : TestMethod81 {82 public FakeTestMethod(object obj, string name)83 : this(obj.GetType(), name) { }84 public FakeTestMethod(Type type, string name)85 : base(new MethodWrapper(type, type.GetMethod(name, BF.Public | BF.NonPublic | BF.Static | BF.Instance))) { }86 }87 #endregion88 #region FakeWorkItem Class89 /// <summary>90 /// FakeWorkItem is used in tests to simulate an actual WorkItem91 /// </summary>92 public class FakeWorkItem : WorkItem93 {94 public event System.EventHandler Executed;95 public FakeWorkItem(Test test, TestExecutionContext context)96 : base(test) 97 {98 InitializeContext(context);99 }100 public override void Execute()101 {102 if (Executed != null)103 Executed(this, System.EventArgs.Empty);104 }105 protected override void PerformWork() { }106 }107 #endregion108}...
TestWorkerTests.cs
Source:TestWorkerTests.cs
...22 [Test]23 public void BusyExecuteIdleEventsCalledInSequence()24 {25 StringBuilder sb = new StringBuilder();26 FakeWorkItem work = Fakes.GetWorkItem(this, "FakeMethod");27 _worker.Busy += (s, ea) => { sb.Append("Busy"); };28 work.Executed += (s, ea) => { sb.Append("Exec"); };29 _worker.Idle += (s, ea) => { sb.Append ("Idle"); };30 _queue.Enqueue(work);31 _worker.Start();32 _queue.Start();33 Assert.That(() => sb.ToString(), Is.EqualTo("BusyExecIdle").After(34 delayInMilliseconds: 10_000, pollingInterval: 200));35 }36 private void FakeMethod()37 {38 }39 }40}...
FakeWorkItem
Using AI Code Generation
1using NUnit.Framework;2using NUnit.Framework.Internal.Execution;3{4 {5 public void Test1()6 {7 var workItem = new FakeWorkItem();8 workItem.InitializeContext(null);9 workItem.Execute();10 }11 }12}
FakeWorkItem
Using AI Code Generation
1using NUnit.Framework.Internal.Execution;2using NUnit.Framework.Internal;3using System.Threading;4using System.Threading.Tasks;5using System;6using NUnit.Framework;7{8 {9 public void Test()10 {11 var workItem = new FakeWorkItem();12 workItem.Initialize(new TestSuite("test"));13 var task = workItem.ExecuteAsync(new CancellationTokenSource().Token);14 Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);15 }16 }17}18using NUnit.Framework.Internal.Execution;19using NUnit.Framework.Internal;20using System.Threading;21using System.Threading.Tasks;22using System;23using NUnit.Framework;24{25 {26 public void Test()27 {28 var workItem = new FakeWorkItem();29 workItem.Initialize(new TestSuite("test"));30 var task = workItem.ExecuteAsync(new CancellationTokenSource().Token);31 Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);32 }33 }34}35using NUnit.Framework.Internal.Execution;36using NUnit.Framework.Internal;37using System.Threading;38using System.Threading.Tasks;39using System;40using NUnit.Framework;41{42 {43 public void Test()44 {45 var workItem = new FakeWorkItem();46 workItem.Initialize(new TestSuite("test"));47 var task = workItem.ExecuteAsync(new CancellationTokenSource().Token);48 Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);49 }50 }51}52using NUnit.Framework.Internal.Execution;53using NUnit.Framework.Internal;54using System.Threading;55using System.Threading.Tasks;56using System;57using NUnit.Framework;58{59 {60 public void Test()61 {62 var workItem = new FakeWorkItem();63 workItem.Initialize(new TestSuite("test"));64 var task = workItem.ExecuteAsync(new CancellationTokenSource().Token);65 Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);66 }67 }68}
FakeWorkItem
Using AI Code Generation
1using NUnit.Framework.Internal.Execution;2using NUnit.Framework.Internal.Execution.WorkItemFactories;3using NUnit.Framework;4using System.Collections.Generic;5using NUnit.Framework.Interfaces;6using NUnit.Framework.Internal;7using System;8{9 {10 public FakeWorkItem(ITest test, WorkItem parent) : base(test, parent) { }11 protected override void PerformWork()12 {13 throw new NotImplementedException();14 }15 }16 {17 public WorkItem CreateWorkItem(ITest test, WorkItem parent)18 {19 return new FakeWorkItem(test, parent);20 }21 }22 {23 public void Test1()24 {25 var testAssembly = new TestAssembly(TestContext.CurrentContext.TestDirectory + "\\NUnitTests.dll", new Dictionary<string, object>());26 var fakeWorkItemFactory = new FakeWorkItemFactory();27 var testEngine = new TestEngine();28 var testRunner = testEngine.GetRunner(testAssembly);29 var testPackage = new TestPackage(testAssembly.AssemblyPath);30 testPackage.TestName = "NUnitTests.Tests";31 testPackage.Settings["WorkItemFactory"] = fakeWorkItemFactory;32 testRunner.Load(testPackage);33 var result = testRunner.Run(null, TestFilter.Empty);34 Assert.Pass();35 }36 }37}38using NUnit.Framework.Internal.Execution;39using NUnit.Framework.Internal.Execution.WorkItemFactories;40using NUnit.Framework;41using System.Collections.Generic;42using NUnit.Framework.Interfaces;43using NUnit.Framework.Internal;44using System;45{46 {47 public FakeWorkItem(ITest test, WorkItem parent) : base(test, parent) { }48 protected override void PerformWork()49 {50 throw new NotImplementedException();51 }52 }53 {54 public WorkItem CreateWorkItem(ITest test, WorkItem parent)55 {56 return new FakeWorkItem(test, parent);57 }58 }59 {60 public void Test1()61 {62 var testAssembly = new TestAssembly(TestContext.CurrentContext.TestDirectory + "\\NUnit
FakeWorkItem
Using AI Code Generation
1using System;2using System.Reflection;3using NUnit.Framework.Internal.Execution;4using NUnit.Framework.Internal.Commands;5using NUnit.Framework.Internal;6{7 {8 static void Main(string[] args)9 {10 var test = TestBuilder.MakeTestCase(typeof(Program).GetMethod("Test1"), null);11 var workItem = new FakeWorkItem(test);12 workItem.Execute();13 Console.WriteLine(workItem.Result.Message);14 }15 public static void Test1()16 {17 Console.WriteLine("Test1");18 }19 }20}
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!!