Best Xunit code snippet using Namespace2.TestableXunit2.Create
Xunit2AcceptanceTests.cs
Source:Xunit2AcceptanceTests.cs
...11 {12 [Fact]13 public async ValueTask NoTestMethods()14 {15 using var assm = await CSharpAcceptanceTestV2Assembly.Create(code: "");16 var controller = TestableXunit2.Create(assm.FileName, null, true);17 using var sink = SpyMessageSink<_DiscoveryComplete>.Create();18 var settings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());19 controller.Find(sink, settings);20 sink.Finished.WaitOne();21 Assert.IsType<_DiscoveryStarting>(sink.Messages.First());22 Assert.False(sink.Messages.Any(msg => msg is _TestCaseDiscovered));23 }24 public class CSharp25 {26 [Fact]27 public async ValueTask FactAcceptanceTest()28 {29 var code = @"30using System;31using Xunit;32namespace Namespace133{34 public class Class135 {36 [Fact]37 [Trait(""Name!"", ""Value!"")]38 public void Trait() { }39 [Fact(Skip=""Skipping"")]40 public void Skipped() { }41 [Fact(DisplayName=""Custom Test Name"")]42 public void CustomName() { }43 }44}45namespace Namespace246{47 public class OuterClass48 {49 public class Class250 {51 [Fact]52 public void TestMethod() { }53 }54 }55}";56 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);57 var controller = TestableXunit2.Create(assembly.FileName, null, true);58 var sink = new TestDiscoverySink();59 var settings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());60 controller.Find(sink, settings);61 sink.Finished.WaitOne();62 Assert.Collection(63 sink.TestCases.OrderBy(tc => tc.TestCaseDisplayName),64 testCase => Assert.Equal("Custom Test Name", testCase.TestCaseDisplayName),65 testCase =>66 {67 Assert.Equal("Namespace1.Class1.Skipped", testCase.TestCaseDisplayName);68 Assert.Equal("Skipping", testCase.SkipReason);69 },70 testCase =>71 {72 Assert.Equal("Namespace1.Class1.Trait", testCase.TestCaseDisplayName);73 var key = Assert.Single(testCase.Traits.Keys);74 Assert.Equal("Name!", key);75 var value = Assert.Single(testCase.Traits[key]);76 Assert.Equal("Value!", value);77 },78 testCase =>79 {80 Assert.Equal("Namespace2.OuterClass+Class2.TestMethod", testCase.TestCaseDisplayName);81 Assert.StartsWith(":F:Namespace2.OuterClass+Class2:TestMethod:1:0:", testCase.Serialization);82 Assert.Null(testCase.SkipReason);83 Assert.Equal("Class2", testCase.TestClassName);84 Assert.Equal("Namespace2", testCase.TestClassNamespace);85 Assert.Equal("Namespace2.OuterClass+Class2", testCase.TestClassNameWithNamespace);86 Assert.Equal("TestMethod", testCase.TestMethodName);87 }88 );89 }90 [Fact]91 public async ValueTask TheoryAcceptanceTest()92 {93 var code = @"94using System;95using Xunit;96public class TestClass97{98 [Theory]99 [InlineData]100 [InlineData(42)]101 [InlineData(42, ""Hello world"")]102 public void TestMethod(int x) { }103}";104 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);105 var controller = TestableXunit2.Create(assembly.FileName, null, true);106 var sink = new TestDiscoverySink();107 var settings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());108 controller.Find(sink, settings);109 sink.Finished.WaitOne();110 Assert.Collection(111 sink.TestCases.OrderBy(tc => tc.TestCaseDisplayName),112 testCase => Assert.Contains("TestClass.TestMethod(x: ???)", testCase.TestCaseDisplayName),113 testCase => Assert.Contains("TestClass.TestMethod(x: 42)", testCase.TestCaseDisplayName),114 testCase => Assert.Contains("TestClass.TestMethod(x: 42, ???: \"Hello world\")", testCase.TestCaseDisplayName)115 );116 }117 }118 public class FSharp119 {120 [Fact]121 public async ValueTask FactAcceptanceTest()122 {123 var code = @"124module FSharpTests125open Xunit126[<Fact>]127[<Trait(""Name!"", ""Value!"")>]128let Trait() =129 Assert.True(true)130[<Fact(Skip = ""Skipping"")>]131let Skipped() =132 Assert.True(false)133[<Fact(DisplayName=""Custom Test Name"")>]134let CustomName() =135 Assert.True(true)136";137 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));138 var controller = TestableXunit2.Create(assembly.FileName, null, true);139 var sink = new TestDiscoverySink();140 var settings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());141 controller.Find(sink, settings);142 sink.Finished.WaitOne();143 Assert.Collection(144 sink.TestCases.OrderBy(tc => tc.TestCaseDisplayName),145 testCase => Assert.Equal("Custom Test Name", testCase.TestCaseDisplayName),146 testCase =>147 {148 Assert.Equal("FSharpTests.Skipped", testCase.TestCaseDisplayName);149 Assert.Equal("Skipping", testCase.SkipReason);150 },151 testCase =>152 {153 Assert.Equal("FSharpTests.Trait", testCase.TestCaseDisplayName);154 Assert.Collection(testCase.Traits,155 kvp =>156 {157 Assert.Equal("Name!", kvp.Key);158 Assert.Equal("Value!", kvp.Value.Single());159 }160 );161 }162 );163 }164 [Fact]165 public async ValueTask TheoryAcceptanceTest()166 {167 var code = @"168module FSharpTests169open Xunit170[<Theory>]171[<InlineData>]172[<InlineData(42)>]173[<InlineData(42, ""Hello world"")>]174let TestMethod (x:int) =175 Assert.True(true)176";177 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));178 var controller = TestableXunit2.Create(assembly.FileName, null, true);179 var sink = new TestDiscoverySink();180 var settings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());181 controller.Find(sink, settings);182 sink.Finished.WaitOne();183 Assert.Collection(184 sink.TestCases.OrderBy(tc => tc.TestCaseDisplayName),185 testCase => Assert.Equal("FSharpTests.TestMethod(x: ???)", testCase.TestCaseDisplayName),186 testCase => Assert.Equal("FSharpTests.TestMethod(x: 42)", testCase.TestCaseDisplayName),187 testCase => Assert.Equal("FSharpTests.TestMethod(x: 42, ???: \"Hello world\")", testCase.TestCaseDisplayName)188 );189 }190 }191 }192 public class FindAndRun193 {194 [Fact]195 public async ValueTask NoTestMethods()196 {197 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code: "");198 var controller = TestableXunit2.Create(assembly.FileName, null, true);199 var settings = new FrontControllerFindAndRunSettings(_TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());200 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();201 controller.FindAndRun(sink, settings);202 sink.Finished.WaitOne();203 Assert.Empty(sink.Messages.OfType<_TestPassed>());204 Assert.Empty(sink.Messages.OfType<_TestFailed>());205 Assert.Empty(sink.Messages.OfType<_TestSkipped>());206 }207 public class CSharp208 {209 [Fact]210 public async ValueTask FactAcceptanceTest()211 {212 var code = @"213using System;214using Xunit;215public class TestClass216{217 [Fact]218 public void TestMethod() { Assert.True(false); }219}";220 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);221 var controller = TestableXunit2.Create(assembly.FileName, null, true);222 var settings = new FrontControllerFindAndRunSettings(_TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());223 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();224 controller.FindAndRun(sink, settings);225 sink.Finished.WaitOne();226 Assert.Empty(sink.Messages.OfType<_TestPassed>());227 Assert.Empty(sink.Messages.OfType<_TestSkipped>());228 var failedTest = Assert.Single(sink.Messages.OfType<_TestFailed>());229 var failedMetadata = sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);230 Assert.Equal("TestClass.TestMethod", failedMetadata.TestDisplayName);231 }232 [Fact]233 public async ValueTask TheoryAcceptanceTest()234 {235 var code = @"236using System;237using Xunit;238public class TestClass239{240 [Theory]241 [InlineData(42)]242 [InlineData(2112)]243 public void TestMethod(int x) { Assert.Equal(2112, x); }244}";245 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);246 var controller = TestableXunit2.Create(assembly.FileName, null, true);247 var settings = new FrontControllerFindAndRunSettings(_TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());248 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();249 controller.FindAndRun(sink, settings);250 sink.Finished.WaitOne();251 Assert.Empty(sink.Messages.OfType<_TestSkipped>());252 var passedTest = Assert.Single(sink.Messages.OfType<_TestPassed>());253 var passedMetadata = sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == passedTest.TestUniqueID);254 Assert.Equal("TestClass.TestMethod(x: 2112)", passedMetadata.TestDisplayName);255 var failedTest = Assert.Single(sink.Messages.OfType<_TestFailed>());256 var failedMetadata = sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);257 Assert.Equal("TestClass.TestMethod(x: 42)", failedMetadata.TestDisplayName);258 }259 [Fact]260 public async ValueTask AsyncAcceptanceTest()261 {262 var code = @"263using System;264using System.Threading.Tasks;265using Xunit;266public class TestClass267{268 [Fact]269 public async void AsyncVoid()270 {271 await Task.Delay(10);272 Assert.True(false);273 }274 [Fact]275 public async Task AsyncTask()276 {277 await Task.Delay(10);278 Assert.True(false);279 }280}";281 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);282 var controller = TestableXunit2.Create(assembly.FileName, null, true);283 var discoveryOptions = _TestFrameworkOptions.ForDiscovery();284 var executionOptions = _TestFrameworkOptions.ForExecution();285 var settings = new FrontControllerFindAndRunSettings(discoveryOptions, executionOptions);286 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();287 controller.FindAndRun(sink, settings);288 sink.Finished.WaitOne();289 Assert.Empty(sink.Messages.OfType<_TestPassed>());290 Assert.Empty(sink.Messages.OfType<_TestSkipped>());291 var failedTests =292 sink.Messages293 .OfType<_TestFailed>()294 .Select(f => sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == f.TestUniqueID).TestDisplayName);295 Assert.Collection(296 failedTests.OrderBy(name => name),297 name => Assert.Equal("TestClass.AsyncTask", name),298 name => Assert.Equal("TestClass.AsyncVoid", name)299 );300 }301 }302 public class FSharp303 {304 [Fact]305 public async ValueTask FactAcceptanceTest()306 {307 var code = @"308module FSharpTests309open Xunit310[<Fact>]311let TestMethod() =312 Assert.True(false)313";314 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));315 var controller = TestableXunit2.Create(assembly.FileName, null, true);316 var settings = new FrontControllerFindAndRunSettings(_TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());317 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();318 controller.FindAndRun(sink, settings);319 sink.Finished.WaitOne();320 Assert.Empty(sink.Messages.OfType<_TestPassed>());321 Assert.Empty(sink.Messages.OfType<_TestSkipped>());322 var failedTest = Assert.Single(sink.Messages.OfType<_TestFailed>());323 var failedMetadata = sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);324 Assert.Equal("FSharpTests.TestMethod", failedMetadata.TestDisplayName);325 }326 [Fact]327 public async ValueTask TheoryAcceptanceTest()328 {329 var code = @"330module FSharpTests331open Xunit332[<Theory>]333[<InlineData(42)>]334[<InlineData(2112)>]335let TestMethod(x : int) =336 Assert.Equal(2112, x)337";338 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));339 var controller = TestableXunit2.Create(assembly.FileName, null, true);340 var settings = new FrontControllerFindAndRunSettings(_TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());341 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();342 controller.FindAndRun(sink, settings);343 sink.Finished.WaitOne();344 Assert.Empty(sink.Messages.OfType<_TestSkipped>());345 var passedTest = Assert.Single(sink.Messages.OfType<_TestPassed>());346 var passedMetadata = sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == passedTest.TestUniqueID);347 Assert.Equal("FSharpTests.TestMethod(x: 2112)", passedMetadata.TestDisplayName);348 var failedTest = Assert.Single(sink.Messages.OfType<_TestFailed>());349 var failedMetadata = sink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);350 Assert.Equal("FSharpTests.TestMethod(x: 42)", failedMetadata.TestDisplayName);351 }352 [Fact]353 public async ValueTask AsyncAcceptanceTest()354 {355 var code = @"356module FSharpTests357open Xunit358[<Fact>]359let AsyncFailing() =360 async {361 do! Async.Sleep(10)362 Assert.True(false)363 }364";365 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));366 var controller = TestableXunit2.Create(assembly.FileName, null, true);367 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();368 var settings = new FrontControllerFindAndRunSettings(_TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());369 controller.FindAndRun(sink, settings);370 sink.Finished.WaitOne();371 var failures = sink.Messages.OfType<_TestFailed>();372 var failure = Assert.Single(failures);373 var failureStarting = sink.Messages.OfType<_TestStarting>().Single(s => s.TestUniqueID == failure.TestUniqueID);374 Assert.Equal("FSharpTests.AsyncFailing", failureStarting.TestDisplayName);375 }376 }377 }378 public class Run379 {380 [Fact]381 public async ValueTask NoTestMethods()382 {383 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code: "");384 var controller = TestableXunit2.Create(assembly.FileName, null, true);385 var settings = new FrontControllerRunSettings(_TestFrameworkOptions.ForExecution(), new string[0]);386 using var sink = SpyMessageSink<_TestAssemblyFinished>.Create();387 controller.Run(sink, settings);388 sink.Finished.WaitOne();389 Assert.Empty(sink.Messages.OfType<_TestPassed>());390 Assert.Empty(sink.Messages.OfType<_TestFailed>());391 Assert.Empty(sink.Messages.OfType<_TestSkipped>());392 }393 public class CSharp394 {395 [Fact]396 public async ValueTask FactAcceptanceTest()397 {398 var code = @"399using System;400using Xunit;401public class TestClass402{403 [Fact]404 public void TestMethod() { Assert.True(false); }405}";406 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);407 var controller = TestableXunit2.Create(assembly.FileName, null, true);408 var findSettings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());409 using var discoverySink = SpyMessageSink<_DiscoveryComplete>.Create();410 controller.Find(discoverySink, findSettings);411 discoverySink.Finished.WaitOne();412 using var executionSink = SpyMessageSink<_TestAssemblyFinished>.Create();413 var serializedTestCases = discoverySink.Messages.OfType<_TestCaseDiscovered>().Select(tcdm => tcdm.Serialization!).ToArray();414 Assert.All(serializedTestCases, serializedTestCase => Assert.NotNull(serializedTestCase));415 var runSettings = new FrontControllerRunSettings(_TestFrameworkOptions.ForExecution(), serializedTestCases);416 controller.Run(executionSink, runSettings);417 executionSink.Finished.WaitOne();418 Assert.Empty(executionSink.Messages.OfType<_TestPassed>());419 Assert.Empty(executionSink.Messages.OfType<_TestSkipped>());420 var failedTest = Assert.Single(executionSink.Messages.OfType<_TestFailed>());421 var failedMetadata = executionSink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);422 Assert.Equal("TestClass.TestMethod", failedMetadata.TestDisplayName);423 }424 [Fact]425 public async ValueTask TheoryAcceptanceTest()426 {427 var code = @"428using System;429using Xunit;430public class TestClass431{432 [Theory]433 [InlineData(42)]434 [InlineData(2112)]435 public void TestMethod(int x) { Assert.Equal(2112, x); }436}";437 using var assembly = await CSharpAcceptanceTestV2Assembly.Create(code);438 var controller = TestableXunit2.Create(assembly.FileName, null, true);439 var findSettings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());440 using var discoverySink = SpyMessageSink<_DiscoveryComplete>.Create();441 controller.Find(discoverySink, findSettings);442 discoverySink.Finished.WaitOne();443 using var executionSink = SpyMessageSink<_TestAssemblyFinished>.Create();444 var serializedTestCases = discoverySink.Messages.OfType<_TestCaseDiscovered>().Select(tcdm => tcdm.Serialization!).ToArray();445 Assert.All(serializedTestCases, serializedTestCase => Assert.NotNull(serializedTestCase));446 var runSettings = new FrontControllerRunSettings(_TestFrameworkOptions.ForExecution(), serializedTestCases);447 controller.Run(executionSink, runSettings);448 executionSink.Finished.WaitOne();449 Assert.Empty(executionSink.Messages.OfType<_TestSkipped>());450 var passedTest = Assert.Single(executionSink.Messages.OfType<_TestPassed>());451 var passedMetadata = executionSink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == passedTest.TestUniqueID);452 Assert.Equal("TestClass.TestMethod(x: 2112)", passedMetadata.TestDisplayName);453 var failedTest = Assert.Single(executionSink.Messages.OfType<_TestFailed>());454 var failedMetadata = executionSink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);455 Assert.Equal("TestClass.TestMethod(x: 42)", failedMetadata.TestDisplayName);456 }457 }458 public class FSharp459 {460 [Fact]461 public async ValueTask FactAcceptanceTest()462 {463 var code = @"464module FSharpTests465open Xunit466[<Fact>]467let TestMethod() =468 Assert.True(false)469";470 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));471 var controller = TestableXunit2.Create(assembly.FileName, null, true);472 var findSettings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());473 using var discoverySink = SpyMessageSink<_DiscoveryComplete>.Create();474 controller.Find(discoverySink, findSettings);475 discoverySink.Finished.WaitOne();476 using var executionSink = SpyMessageSink<_TestAssemblyFinished>.Create();477 var serializedTestCases = discoverySink.Messages.OfType<_TestCaseDiscovered>().Select(tcdm => tcdm.Serialization!).ToArray();478 Assert.All(serializedTestCases, serializedTestCase => Assert.NotNull(serializedTestCase));479 var runSettings = new FrontControllerRunSettings(_TestFrameworkOptions.ForExecution(), serializedTestCases);480 controller.Run(executionSink, runSettings);481 executionSink.Finished.WaitOne();482 Assert.Empty(executionSink.Messages.OfType<_TestPassed>());483 Assert.Empty(executionSink.Messages.OfType<_TestSkipped>());484 var failedTest = Assert.Single(executionSink.Messages.OfType<_TestFailed>());485 var failedMetadata = executionSink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);486 Assert.Equal("FSharpTests.TestMethod", failedMetadata.TestDisplayName);487 }488 [Fact]489 public async ValueTask TheoryAcceptanceTest()490 {491 var code = @"492module FSharpTests493open Xunit494[<Theory>]495[<InlineData(42)>]496[<InlineData(2112)>]497let TestMethod(x : int) =498 Assert.Equal(2112, x)499";500 using var assembly = await FSharpAcceptanceTestV2Assembly.Create(code.Replace("\t", " "));501 var controller = TestableXunit2.Create(assembly.FileName, null, true);502 var findSettings = new FrontControllerFindSettings(_TestFrameworkOptions.ForDiscovery());503 using var discoverySink = SpyMessageSink<_DiscoveryComplete>.Create();504 controller.Find(discoverySink, findSettings);505 discoverySink.Finished.WaitOne();506 using var executionSink = SpyMessageSink<_TestAssemblyFinished>.Create();507 var serializedTestCases = discoverySink.Messages.OfType<_TestCaseDiscovered>().Select(tcdm => tcdm.Serialization!).ToArray();508 Assert.All(serializedTestCases, serializedTestCase => Assert.NotNull(serializedTestCase));509 var runSettings = new FrontControllerRunSettings(_TestFrameworkOptions.ForExecution(), serializedTestCases);510 controller.Run(executionSink, runSettings);511 executionSink.Finished.WaitOne();512 Assert.Empty(executionSink.Messages.OfType<_TestSkipped>());513 var passedTest = Assert.Single(executionSink.Messages.OfType<_TestPassed>());514 var passedMetadata = executionSink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == passedTest.TestUniqueID);515 Assert.Equal("FSharpTests.TestMethod(x: 2112)", passedMetadata.TestDisplayName);516 var failedTest = Assert.Single(executionSink.Messages.OfType<_TestFailed>());517 var failedMetadata = executionSink.Messages.OfType<_TestStarting>().Single(ts => ts.TestUniqueID == failedTest.TestUniqueID);518 Assert.Equal("FSharpTests.TestMethod(x: 42)", failedMetadata.TestDisplayName);519 }520 }521 }522 class TestableXunit2523 {524 public static IFrontController Create(525 string assemblyFileName,526 string? configFileName = null,527 bool shadowCopy = true,528 AppDomainSupport appDomainSupport = AppDomainSupport.Required)529 {530 var project = new XunitProject();531 var projectAssembly = new XunitProjectAssembly(project)532 {533 AssemblyFileName = assemblyFileName,534 ConfigFileName = configFileName,535 };536 projectAssembly.Configuration.AppDomain = appDomainSupport;537 projectAssembly.Configuration.ShadowCopy = shadowCopy;538 return Xunit2.ForDiscoveryAndExecution(projectAssembly, diagnosticMessageSink: _NullMessageSink.Instance);...
Xunit2Tests.cs
Source:Xunit2Tests.cs
...8 {9 [Fact]10 public void NoTestMethods()11 {12 using (var assm = CSharpAcceptanceTestV2Assembly.Create(code: ""))13 using (var controller = new TestableXunit2(assm.FileName, null, true))14 {15 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();16 controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());17 sink.Finished.WaitOne();18 Assert.False(sink.Messages.Any(msg => msg is ITestCaseDiscoveryMessage));19 }20 }21 [Fact]22 public void SingleTestMethod()23 {24 string code = @"25using Xunit;26public class Foo27{28 [Fact]29 public void Bar() { }30}";31 using (var assm = CSharpAcceptanceTestV2Assembly.Create(code))32 using (var controller = new TestableXunit2(assm.FileName, null, true))33 {34 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();35 controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());36 sink.Finished.WaitOne();37 ITestCase testCase = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Single().TestCase;38 Assert.Equal("Foo.Bar", testCase.DisplayName);39 }40 }41 }42 public class CSharp43 {44 [Fact]45 public void FactAcceptanceTest()46 {47 string code = @"48using System;49using Xunit;50namespace Namespace151{52 public class Class153 {54 [Fact]55 [Trait(""Name!"", ""Value!"")]56 public void Trait() { }57 [Fact(Skip=""Skipping"")]58 public void Skipped() { }59 [Fact(DisplayName=""Custom Test Name"")]60 public void CustomName() { }61 }62}63namespace Namespace264{65 public class OuterClass66 {67 public class Class268 {69 [Fact]70 public void TestMethod() { }71 }72 }73}";74 using (var assembly = CSharpAcceptanceTestV2Assembly.Create(code))75 using (var controller = new TestableXunit2(assembly.FileName, null, true))76 {77 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();78 controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());79 sink.Finished.WaitOne();80 ITestCase[] testCases = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(tcdm => tcdm.TestCase).ToArray();81 Assert.Equal(4, testCases.Length);82 ITestCase traitTest = Assert.Single(testCases, tc => tc.DisplayName == "Namespace1.Class1.Trait");83 string key = Assert.Single(traitTest.Traits.Keys);84 Assert.Equal("Name!", key);85 string value = Assert.Single(traitTest.Traits[key]);86 Assert.Equal("Value!", value);87 ITestCase skipped = Assert.Single(testCases, tc => tc.DisplayName == "Namespace1.Class1.Skipped");88 Assert.Equal("Skipping", skipped.SkipReason);89 Assert.Single(testCases, tc => tc.DisplayName == "Custom Test Name");90 Assert.Single(testCases, tc => tc.DisplayName == "Namespace2.OuterClass+Class2.TestMethod");91 }92 }93 [Fact]94 public void TheoryWithInlineData()95 {96 string code = @"97using System;98using Xunit;99public class TestClass100{101 [Theory]102 [InlineData]103 [InlineData(42)]104 [InlineData(42, 21.12)]105 public void TestMethod(int x) { }106}";107 using (var assembly = CSharpAcceptanceTestV2Assembly.Create(code))108 using (var controller = new TestableXunit2(assembly.FileName, null, true))109 {110 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();111 controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());112 sink.Finished.WaitOne();113 string[] testCaseNames = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(tcdm => tcdm.TestCase.DisplayName).ToArray();114 Assert.Equal(3, testCaseNames.Length);115 Assert.Contains("TestClass.TestMethod(x: ???)", testCaseNames);116 Assert.Contains("TestClass.TestMethod(x: 42)", testCaseNames);117 Assert.Contains($"TestClass.TestMethod(x: 42, ???: {21.12})", testCaseNames);118 }119 }120 }121 public class FSharp122 {123 [Fact]124 public void FactAcceptanceTest()125 {126 string code = @"127module FSharpTests128open Xunit129[<Fact>]130[<Trait(""Name!"", ""Value!"")>]131let Trait() =132 Assert.True(true)133[<Fact(Skip = ""Skipping"")>]134let Skipped() =135 Assert.True(false)136[<Fact(DisplayName=""Custom Test Name"")>]137let CustomName() =138 Assert.True(true)139";140 using (var assembly = FSharpAcceptanceTestV2Assembly.Create(code))141 using (var controller = new TestableXunit2(assembly.FileName, null, true))142 {143 var sink = new TestDiscoverySink();144 controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());145 sink.Finished.WaitOne();146 Assert.Collection(sink.TestCases.OrderBy(tc => tc.DisplayName),147 testCase => Assert.Equal("Custom Test Name", testCase.DisplayName),148 testCase =>149 {150 Assert.Equal("FSharpTests.Skipped", testCase.DisplayName);151 Assert.Equal("Skipping", testCase.SkipReason);152 },153 testCase =>154 {155 Assert.Equal("FSharpTests.Trait", testCase.DisplayName);156 Assert.Collection(testCase.Traits,157 kvp =>158 {159 Assert.Equal("Name!", kvp.Key);160 Assert.Equal("Value!", kvp.Value.Single());161 }162 );163 }164 );165 }166 }167 [Fact]168 public void TheoryWithInlineData()169 {170 string code = @"171module FSharpTests172open Xunit173[<Theory>]174[<InlineData>]175[<InlineData(42)>]176[<InlineData(42, 21.12)>]177let TestMethod (x:int) =178 Assert.True(true)179";180 using (var assembly = FSharpAcceptanceTestV2Assembly.Create(code))181 using (var controller = new TestableXunit2(assembly.FileName, null, true))182 {183 var sink = new TestDiscoverySink();184 controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());185 sink.Finished.WaitOne();186 Assert.Collection(sink.TestCases.OrderBy(tc => tc.DisplayName),187 testCase => Assert.Equal("FSharpTests.TestMethod(x: ???)", testCase.DisplayName),188 testCase => Assert.Equal("FSharpTests.TestMethod(x: 42)", testCase.DisplayName),189 testCase => Assert.Equal("FSharpTests.TestMethod(x: 42, ???: 21.12)", testCase.DisplayName)190 );191 }192 }193 [Fact]194 public void SupportsAsyncReturningMethods()195 {196 string code = @"197module FSharpTests198open Xunit199[<Fact>]200let AsyncFailing() =201 async {202 do! Async.Sleep(10)203 Assert.True(false)204 }205";206 using (var assembly = FSharpAcceptanceTestV2Assembly.Create(code))207 using (var controller = new TestableXunit2(assembly.FileName, null, true))208 {209 var sink = new SpyMessageSink<ITestAssemblyFinished>();210 controller.RunAll(sink, discoveryOptions: TestFrameworkOptions.ForDiscovery(), executionOptions: TestFrameworkOptions.ForExecution());211 sink.Finished.WaitOne();212 var failures = sink.Messages.OfType<ITestFailed>();213 var failure = Assert.Single(failures);214 Assert.Equal("FSharpTests.AsyncFailing", failure.TestCase.DisplayName);215 }216 }217 }218 class TestableXunit2 : Xunit2219 {220 public TestableXunit2(string assemblyFileName, string configFileName = null, bool shadowCopy = true, AppDomainSupport appDomainSupport = AppDomainSupport.Required)...
Create
Using AI Code Generation
1var xunit2 = new Namespace2.TestableXunit2();2xunit2.Create();3var xunit3 = new Namespace3.TestableXunit3();4xunit3.Create();5var xunit4 = new Namespace4.TestableXunit4();6xunit4.Create();7var xunit5 = new Namespace5.TestableXunit5();8xunit5.Create();9var xunit6 = new Namespace6.TestableXunit6();10xunit6.Create();11var xunit7 = new Namespace7.TestableXunit7();12xunit7.Create();13var xunit8 = new Namespace8.TestableXunit8();14xunit8.Create();15var xunit9 = new Namespace9.TestableXunit9();16xunit9.Create();17var xunit10 = new Namespace10.TestableXunit10();18xunit10.Create();19var xunit11 = new Namespace11.TestableXunit11();20xunit11.Create();21var xunit12 = new Namespace12.TestableXunit12();22xunit12.Create();23var xunit13 = new Namespace13.TestableXunit13();24xunit13.Create();
Create
Using AI Code Generation
1using Xunit;2using Namespace2;3{4 {5 public void Test1()6 {7 TestableXunit2.Create();8 }9 }10}
Create
Using AI Code Generation
1using Xunit;2{3 {4 public void Test1()5 {6 var x = new Namespace2.TestableXunit2();7 x.Create();8 }9 }10}11using Xunit;12{13 {14 public void Test1()15 {16 var x = new Namespace2.TestableXunit2();17 x.Create();18 }19 }20}21using Xunit;22{23 {24 public void Test1()25 {26 var x = new Namespace2.TestableXunit2();27 x.Create();28 }29 }30}31using Xunit;32{33 {34 public void Test1()35 {36 var x = new Namespace2.TestableXunit2();37 x.Create();38 }39 }40}41using Xunit;42{43 {44 public void Test1()45 {46 var x = new Namespace2.TestableXunit2();47 x.Create();48 }49 }50}51using Xunit;52{53 {54 public void Test1()55 {56 var x = new Namespace2.TestableXunit2();57 x.Create();58 }59 }60}61using Xunit;62{63 {64 public void Test1()65 {
Create
Using AI Code Generation
1using Xunit;2using Namespace2;3{4 {5 public void Test1()6 {7 var x = new TestableXunit2();8 x.Create("123");9 }10 }11}
Create
Using AI Code Generation
1using Xunit;2{3 {4 public void Test()5 {6 var testableXunit = new Namespace2.TestableXunit2();7 testableXunit.Create();8 }9 }10}11using Xunit;12{13 {14 public void Test()15 {16 var testableXunit = new Namespace2.TestableXunit2();17 testableXunit.Create();18 }19 }20}21using Xunit;22{23 {24 public void Test()25 {26 var testableXunit = new Namespace2.TestableXunit2();27 testableXunit.Create();28 }29 }30}31using Xunit;32{33 {34 public void Test()35 {36 var testableXunit = new Namespace2.TestableXunit2();37 testableXunit.Create();38 }39 }40}41using Xunit;42{43 {44 public void Test()45 {46 var testableXunit = new Namespace2.TestableXunit2();47 testableXunit.Create();48 }49 }50}51using Xunit;52{53 {54 public void Test()55 {56 var testableXunit = new Namespace2.TestableXunit2();57 testableXunit.Create();58 }59 }60}61using Xunit;62{
Create
Using AI Code Generation
1{2 {3 public void Create()4 {5 var testable = new Namespace2.TestableXunit2();6 testable.Create();7 }8 }9}10{11 {12 public void Create()13 {14 var testable = new Namespace3.TestableXunit3();15 testable.Create();16 }17 }18}19{20 {21 public void Create()22 {23 var testable = new Namespace4.TestableXunit4();24 testable.Create();25 }26 }27}28{29 {30 public void Create()31 {32 var testable = new Namespace5.TestableXunit5();33 testable.Create();34 }35 }36}37{38 {39 public void Create()40 {41 var testable = new Namespace6.TestableXunit6();42 testable.Create();43 }44 }45}46{47 {48 public void Create()49 {50 var testable = new Namespace7.TestableXunit7();51 testable.Create();52 }53 }54}55{56 {57 public void Create()58 {59 var testable = new Namespace8.TestableXunit8();60 testable.Create();61 }62 }63}64{65 {
Create
Using AI Code Generation
1{2 {3 public void Create()4 {5 Console.WriteLine("TestableXunit2 Create");6 }7 }8}9{10 {11 public void Create()12 {13 Console.WriteLine("TestableXunit2 Create");14 }15 }16}17{18 {19 public void Create()20 {21 Console.WriteLine("TestableXunit2 Create");22 }23 }24}25{26 {27 public void Create()28 {29 Console.WriteLine("TestableXunit2 Create");30 }31 }32}33{34 {35 public void Create()36 {37 Console.WriteLine("TestableXunit2 Create");38 }39 }40}41{42 {43 public void Create()44 {45 Console.WriteLine("TestableXunit2 Create");46 }47 }48}49{50 {51 public void Create()52 {53 Console.WriteLine("TestableXunit2 Create");54 }55 }56}57{58 {59 public void Create()60 {61 Console.WriteLine("TestableXunit2 Create");62 }63 }64}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!