Best Xunit code snippet using Namespace2.TestableXunit2
Xunit2AcceptanceTests.cs
Source:Xunit2AcceptanceTests.cs
...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;...
Xunit2Tests.cs
Source:Xunit2Tests.cs
...18 [Fact]19 public void NoTestMethods()20 {21 using (var assm = new AcceptanceTestAssembly(code: ""))22 using (var controller = new TestableXunit2(assm.FileName, null, true))23 {24 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();25 controller.Find(includeSourceInformation: false, messageSink: sink, options: new XunitDiscoveryOptions());26 sink.Finished.WaitOne();27 Assert.False(sink.Messages.Any(msg => msg is ITestCaseDiscoveryMessage));28 }29 }30 [Fact]31 public void SingleTestMethod()32 {33 string code = @"34 using Xunit;35 36 public class Foo37 {38 [Fact]39 public void Bar() { }40 }41 ";42 using (var assm = new AcceptanceTestAssembly(code))43 using (var controller = new TestableXunit2(assm.FileName, null, true))44 {45 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();46 controller.Find(includeSourceInformation: false, messageSink: sink, options: new XunitDiscoveryOptions());47 sink.Finished.WaitOne();48 ITestCase testCase = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Single().TestCase;49 Assert.Equal("Foo.Bar", testCase.DisplayName);50 }51 }52 [Fact]53 public void FactAcceptanceTest()54 {55 string code = @"56 using System;57 using Xunit;58 59 namespace Namespace160 {61 public class Class162 {63 [Fact]64 [Trait(""Name!"", ""Value!"")]65 public void Trait() { }66 67 [Fact(Skip=""Skipping"")]68 public void Skipped() { }69 70 [Fact(DisplayName=""Custom Test Name"")]71 public void CustomName() { }72 }73 }74 75 namespace Namespace276 {77 public class OuterClass78 {79 public class Class280 {81 [Fact]82 public void TestMethod() { }83 }84 }85 }86 ";87 using (var assembly = new AcceptanceTestAssembly(code))88 using (var controller = new TestableXunit2(assembly.FileName, null, true))89 {90 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();91 controller.Find(includeSourceInformation: false, messageSink: sink, options: new XunitDiscoveryOptions());92 sink.Finished.WaitOne();93 ITestCase[] testCases = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(tcdm => tcdm.TestCase).ToArray();94 Assert.Equal(4, testCases.Length);95 ITestCase traitTest = Assert.Single(testCases, tc => tc.DisplayName == "Namespace1.Class1.Trait");96 string key = Assert.Single(traitTest.Traits.Keys);97 Assert.Equal("Name!", key);98 string value = Assert.Single(traitTest.Traits[key]);99 Assert.Equal("Value!", value);100 ITestCase skipped = Assert.Single(testCases, tc => tc.DisplayName == "Namespace1.Class1.Skipped");101 Assert.Equal("Skipping", skipped.SkipReason);102 Assert.Single(testCases, tc => tc.DisplayName == "Custom Test Name");103 Assert.Single(testCases, tc => tc.DisplayName == "Namespace2.OuterClass+Class2.TestMethod");104 }105 }106 [Fact]107 public void TheoryWithInlineData()108 {109 string code = @"110 using System;111 using Xunit;112 113 public class TestClass114 {115 [Theory]116 [InlineData]117 [InlineData(42)]118 [InlineData(42, 21.12)]119 public void TestMethod(int x) { }120 }121 ";122 using (var assembly = new AcceptanceTestAssembly(code))123 using (var controller = new TestableXunit2(assembly.FileName, null, true))124 {125 var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();126 controller.Find(includeSourceInformation: false, messageSink: sink, options: new XunitDiscoveryOptions());127 sink.Finished.WaitOne();128 string[] testCaseNames = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(tcdm => tcdm.TestCase.DisplayName).ToArray();129 Assert.Equal(3, testCaseNames.Length);130 Assert.Contains("TestClass.TestMethod(x: ???)", testCaseNames);131 Assert.Contains("TestClass.TestMethod(x: 42)", testCaseNames);132 Assert.Contains(String.Format("TestClass.TestMethod(x: 42, ???: {0})", 21.12), testCaseNames);133 }134 }135 }136 class TestableXunit2 : Xunit2137 {138 public TestableXunit2(string assemblyFileName, string configFileName = null, bool shadowCopy = true)139 : base(new NullSourceInformationProvider(), assemblyFileName, configFileName, shadowCopy)140 {141 }142 }143}...
TestableXunit2
Using AI Code Generation
1var x = new TestableXunit2();2x.Test();3var x = new TestableXunit3();4x.Test();5var x = new TestableXunit4();6x.Test();7var x = new TestableXunit5();8x.Test();9var x = new TestableXunit6();10x.Test();11var x = new TestableXunit7();12x.Test();13var x = new TestableXunit8();14x.Test();15var x = new TestableXunit9();16x.Test();17var x = new TestableXunit10();18x.Test();19var x = new TestableXunit11();20x.Test();21var x = new TestableXunit12();22x.Test();23var x = new TestableXunit13();24x.Test();25var x = new TestableXunit14();26x.Test();27var x = new TestableXunit15();28x.Test();
TestableXunit2
Using AI Code Generation
1var t = new TestableXunit2();2t.Test();3var t = new TestableXunit3();4t.Test();5var t = new TestableXunit4();6t.Test();7var t = new TestableXunit5();8t.Test();9var t = new TestableXunit6();10t.Test();11var t = new TestableXunit7();12t.Test();13var t = new TestableXunit8();14t.Test();15var t = new TestableXunit9();16t.Test();17var t = new TestableXunit10();18t.Test();19var t = new TestableXunit11();20t.Test();21var t = new TestableXunit12();22t.Test();23var t = new TestableXunit13();24t.Test();25var t = new TestableXunit14();26t.Test();27var t = new TestableXunit15();28t.Test();
TestableXunit2
Using AI Code Generation
1{2 {3 public void Test()4 {5 var testableXunit = new Namespace2.TestableXunit();6 testableXunit.Test();7 }8 }9}10{11 {12 public void Test()13 {14 var testableXunit = new Namespace3.TestableXunit();15 testableXunit.Test();16 }17 }18}19{20 {21 public void Test()22 {23 var testableXunit = new Namespace4.TestableXunit();24 testableXunit.Test();25 }26 }27}28{29 {30 public void Test()31 {32 var testableXunit = new Namespace5.TestableXunit();33 testableXunit.Test();34 }35 }36}37{38 {39 public void Test()40 {41 var testableXunit = new Namespace6.TestableXunit();42 testableXunit.Test();43 }44 }45}46{47 {48 public void Test()49 {50 var testableXunit = new Namespace7.TestableXunit();51 testableXunit.Test();52 }53 }54}55{56 {57 public void Test()58 {59 var testableXunit = new Namespace8.TestableXunit();60 testableXunit.Test();61 }62 }63}
TestableXunit2
Using AI Code Generation
1{2 {3 public void TestMethod1()4 {5 var testableXunit2 = new Namespace2.TestableXunit2();6 testableXunit2.TestMethod1();7 }8 }9}10{11 {12 public void TestMethod1()13 {14 var testableXunit2 = new Namespace1.TestableXunit2();15 testableXunit2.TestMethod1();16 }17 }18}
TestableXunit2
Using AI Code Generation
1var testable = new TestableXunit2();2testable.DoSomething();3var testable2 = new TestableXunit2();4testable2.DoSomethingElse();5var testable = new TestableXunit2();6testable.DoSomething();7var testable2 = new TestableXunit2();8testable2.DoSomethingElse();9var testable = new TestableXunit2();10testable.DoSomething();11var testable2 = new TestableXunit2();12testable2.DoSomethingElse();13var testable = new TestableXunit2();14testable.DoSomething();15var testable2 = new TestableXunit2();16testable2.DoSomethingElse();17var testable = new TestableXunit2();18testable.DoSomething();19var testable2 = new TestableXunit2();20testable2.DoSomethingElse();21var testable = new TestableXunit2();22testable.DoSomething();23var testable2 = new TestableXunit2();24testable2.DoSomethingElse();25var testable = new TestableXunit2();26testable.DoSomething();27var testable2 = new TestableXunit2();28testable2.DoSomethingElse();
TestableXunit2
Using AI Code Generation
1using Namespace2;2using Xunit;3{4 {5 public void Test1()6 {7 var x = new TestableXunit();8 x.Test2();9 }10 }11}
TestableXunit2
Using AI Code Generation
1using Namespace2;2{3 {4 public TestableXunit1()5 {6 }7 }8}9using Namespace2;10{11 {12 public TestableXunit2()13 {14 }15 }16}17using Namespace2;18{19 {20 }21}
TestableXunit2
Using AI Code Generation
1using Namespace2;2{3 public void Test()4 {5 var x = new TestableXunit();6 x.Test();7 }8}9using Namespace3;10{11 public void Test()12 {13 var x = new TestableXunit();14 x.Test();15 }16}17[assembly: InternalsVisibleTo("Namespace1")]18[assembly: InternalsVisibleTo("Namespace2")]19[assembly: InternalsVisibleTo("Namespace3")]
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!!